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

认情况下,使用认颜色的 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();
}

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...