C# 前景列表框颜色

C# 前景列表框颜色,c#,winforms,C#,Winforms,因此,我用这段代码将默认Winforms中listbox项的背景选择颜色更改为红色 if (e.Index < 0) return; // if the item state is selected then change the back color if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) e = new D

因此,我用这段代码将默认Winforms中listbox项的背景选择颜色更改为红色

if (e.Index < 0) return;
            // if the item state is selected then 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.Red); // Choose the color

            // Draw the background of the ListBox control for each item.
            e.DrawBackground();
            // Draw the current item text
            e.Graphics.DrawString(studentsListBox.Items[e.Index].ToString(), e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
            // If the ListBox has focus, draw a focus rectangle around the selected item.
            e.DrawFocusRectangle();
if(e.Index<0)返回;
//如果选择了项目状态,则更改背景色
if((e.State&DrawItemState.Selected)=DrawItemState.Selected)
e=新的DrawItemEventArgs(如图形、,
e、 字体,
e、 界限,
e、 索引,
e、 状态^DrawItemState。已选择,
e、 前景色,
颜色(红色);//选择颜色
//为每个项目绘制ListBox控件的背景。
e、 牵引杆接地();
//绘制当前项文本
e、 Graphics.DrawString(studentsListBox.Items[e.Index].ToString(),e.Font,brush.Black,e.Bounds,StringFormat.GenericDefault);
//如果列表框具有焦点,请围绕选定项绘制一个焦点矩形。
e、 DrawFocusRectangle();

这很好,但我还想更改所选项目的字体颜色。我该怎么做呢?

如果(e.Index<0),你不能只提供一种颜色而不是
e.ForeColor


if (e.Index < 0)
    return;

Brush foreBrush = Brushes.Black; // non-selected text color
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
    foreBrush = Brushes.White; // selected text color
    e = new DrawItemEventArgs(e.Graphics,
                              e.Font,
                              e.Bounds,
                              e.Index,
                              e.State ^ DrawItemState.Selected,
                              e.ForeColor,
                              Color.Red); // Choose the color 
}

// Draw the background of the ListBox control for each item. 
e.DrawBackground();
// Draw the current item text
e.Graphics.DrawString((sender as ListBox).Items[e.Index].ToString(), e.Font, foreBrush, e.Bounds, StringFormat.GenericDefault);
// If the ListBox has focus, draw a focus rectangle around the selected item. 
e.DrawFocusRectangle(); 
返回; 画笔前刷=画笔。黑色;//非选定文本颜色 if((e.State&DrawItemState.Selected)=DrawItemState.Selected) { 前画笔=画笔。白色;//选定的文本颜色 e=新的DrawItemEventArgs(如图形、, e、 字体, e、 界限, e、 索引, e、 状态^DrawItemState。已选择, e、 前景色, 颜色。红色);//选择颜色 } //为每个项目绘制ListBox控件的背景。 e、 牵引杆接地(); //绘制当前项文本 e、 Graphics.DrawString((发送者作为列表框)。Items[e.Index].ToString(),e.Font,foreBrush,e.Bounds,StringFormat.GenericDefault); //如果列表框具有焦点,请围绕选定项绘制一个焦点矩形。 e、 DrawFocusRectangle();
需要处理画笔,如果索引<0,您仍然必须绘制背景。@Hans:处理其中一个System.Drawing.brush(只读静态对象)是一个非常糟糕的主意。如果背景与控件的背景颜色(即WNDCLASS.hbrBackground)不同,则实际上只需绘制背景,因为WM_ERASEBKGND会处理它。