加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何更改ListBox选择背景颜色?

发布时间:2020-12-15 06:44:18 所属栏目:百科 来源:网络整理
导读:默认情况下,使用默认颜色的 Windows设置是蓝色的. 假设我想把它永久改成红色.我使用Winforms. 提前致谢. 解决方法 您必须覆盖 Drawitem 事件并将 DrawMode 属性设置为 DrawMode.OwnerDrawFixed 检查这个样本 private void listBox1_DrawItem(object sender,D
默认情况下,使用默认颜色的 Windows设置是蓝色的.
假设我想把它永久改成红色.我使用Winforms.

提前致谢.

解决方法

您必须覆盖 Drawitem事件并将 DrawMode属性设置为 DrawMode.OwnerDrawFixed

检查这个样本

private void listBox1_DrawItem(object sender,DrawItemEventArgs e)
{
    if (e.Index<0) return;
    //if the item state is selected them change the back color 
    if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
        e = new DrawItemEventArgs(e.Graphics,e.Font,e.Bounds,e.Index,e.State ^ DrawItemState.Selected,e.ForeColor,Color.Yellow);//Choose the color

    // Draw the background of the ListBox control for each item.
    e.DrawBackground();
    // Draw the current item text
    e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),Brushes.Black,StringFormat.GenericDefault);
    // If the ListBox has focus,draw a focus rectangle around the selected item.
    e.DrawFocusRectangle();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读