在C#中控制ListBox某一行的字体颜色

例1

private void Form1_Load(object sender, EventArgs e)
{
    listBox1.Items.Add("红色");
    listBox1.Items.Add("黄色");
    listBox1.Items.Add("蓝色");
    listBox1.DrawMode = DrawMode.OwnerDrawFixed; // 属性里设置
}

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    Color vColor = e.ForeColor;
    switch (e.Index)
    {
        case 0: vColor = Color.Red; break;
        case 1: vColor = Color.Yellow; break;
        case 2: vColor = Color.Blue; break;
    }
    e.Graphics.FillRectangle(new SolidBrush(vColor), e.Bounds);
    e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(), e.Font, 
        new SolidBrush(e.ForeColor), e.Bounds);
    e.DrawFocusRectangle();
}

例2  一种动态渲染颜色的方式

根据字符串的前缀,分别对文字颜色进行渲染。

// ListBox DrawItem事件响应函数
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{            
    if (e.Index >= 0)
    {
        e.DrawBackground();
        Brush mybsh = Brushes.Black;
        // 判断是什么类型的标签
        if (listBox1.Items[e.Index].ToString().IndexOf("你好") != -1)
        {
            mybsh = Brushes.Green;
        }
        else if (listBox1.Items[e.Index].ToString().IndexOf("你坏") != -1)
        {
            mybsh = Brushes.Red;
        }
        // 焦点框
        e.DrawFocusRectangle();
        //文本 
        e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, mybsh, e.Bounds, StringFormat.GenericDefault);
    }     
}

效果如下,当输入“你好”并按添加按钮的时候相应的ListBox的内容变为的绿色,输入“你坏”的时候变为了红色,达到了我们的要求目的:

例3 隔行显示不同的颜色

要实现这个效果很简单,只需自定义一个类继承ListBox,然后重写OnDrawItem事件就可以了,下面看代码

class CListbox:ListBox
{
    
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    static extern IntPtr GetWindowDC(IntPtr hWnd);
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        
        Graphics g = e.Graphics;
        
        if (e.Index % 2 == 0)
        {
            
            g.FillRectangle(new SolidBrush(Color.WhiteSmoke), e.Bounds);
            g.DrawString(Items[e.Index].ToString(), e.Font, new SolidBrush(Color.Blue), e.Bounds);
            
        }
        else
        {
            
            g.FillRectangle(new SolidBrush(Color.WhiteSmoke), e.Bounds);
            g.DrawString(Items[e.Index].ToString(), e.Font, new SolidBrush(Color.Red), e.Bounds);

        }
        e.DrawFocusRectangle();
        g.Dispose();

        base.OnDrawItem(e);
    }
    
//捕获消息,画listbox边框
 protected override void WndProc(ref Message m)
 {
        if (m.Msg == 0xf || m.Msg == 0x133)
        {
            IntPtr hDC = GetWindowDC(m.HWnd);
            System.Drawing.Pen pen = new Pen(Color.YellowGreen, 1);
            System.Drawing.Graphics g = Graphics.FromHdc(hDC);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            g.DrawRectangle(pen, 0, 0, this.Width - 1, this.Height - 1);
            pen.Dispose();
            g.Dispose();
            ReleaseDC(m.HWnd, hDC);
        }
        base.WndProc(ref m);
    }
}

// 应用如下:
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private CListbox mylistbox;

    private void Form1_Load(object sender, EventArgs e)
    {
        mylistbox = new CListbox();
        mylistbox.ItemHeight = 32;
        mylistbox.DrawMode = DrawMode.OwnerDrawVariable;
        mylistbox.Height = 150;
        mylistbox.Width = 250;
        mylistbox.Items.Add("第一行显示!");
        mylistbox.Items.Add("第二行显示!");
        mylistbox.Items.Add("第三行显示!");
        mylistbox.Items.Add("第四行显示!");
        mylistbox.Items.Add("第五行显示!");
        mylistbox.Items.Add("第六行显示!");
        mylistbox.Items.Add("第七行显示!");
        mylistbox.Items.Add("第八行显示!");
        mylistbox.Items.Add("第九行显示!");
        mylistbox.Items.Add("第十行显示!");
        mylistbox.Items.Add("第十一行显示!");
        mylistbox.Items.Add("第十二行显示!");
        this.Controls.Add(mylistbox);
    }
}
View Code

 效果如下:

参考文章

顺德早茶在C#中控制ListBox某一行的字体颜色

C#中动态修改ListBox的Item的颜色的方法

gyzsky, listbox隔行显示不同颜色

2 一种动态渲染颜色的方式
原文地址:https://www.cnblogs.com/arxive/p/6126295.html