Winform ComboBox独立绘制下拉选项的字体颜色

  • A+
所属分类:.NET技术
摘要

众所周知,cbo1.ForeColor会同时改变cbo1.Text和下拉选项字体颜色。独立绘制下拉选项字体颜色,ForeColor 只对 cbo1.Text 颜色有效的办法,示例:

众所周知,cbo1.ForeColor会同时改变cbo1.Text和下拉选项字体颜色。

独立绘制下拉选项字体颜色,ForeColor 只对 cbo1.Text 颜色有效的办法,示例:

private void cb7_DrawItem(object sender, DrawItemEventArgs e) {     DrawCboItems(sender, e); }  private void cb7_SelectedIndexChanged(object sender, EventArgs e) {     SetCboTextColor(sender); }  private void SetCboTextColor(object sender) {     ComboBox cbx = sender as ComboBox;     if (cbx.Text == "+")         cbx.ForeColor = Color.Red;     else         cbx.ForeColor = Color.Black; }  private void DrawCboItems(object sender, DrawItemEventArgs e) {     ComboBox cbo = sender as ComboBox;      //初始化字体和背景色     Pen fColor = new Pen(Color.Black);     Pen bColor = new Pen(Color.White);      switch (e.Index)     {
     //下拉选项的索引   
case 1:   {   fColor = new Pen(Color.Red);   break;   } } e.Graphics.FillRectangle(bColor.Brush, e.Bounds); e.Graphics.DrawString((string)cbo.Items[e.Index], this.Font, fColor.Brush, e.Bounds); }