在C#中控制ListBox某一行的字体颜色
2016-12-02 16:33
253 查看
例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的内容变为的绿色,输入“你坏”的时候变为了红色,达到了我们的要求目的:
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 一种动态渲染颜色的方式
相关文章推荐
- 在C#中控制ListBox某一行的字体颜色
- 在C#中控制ListBox某一行的字体颜色
- 控制listbox的字体颜色
- ALV 输出控制字体颜色 例程
- 如何让DBGrid中每一行颜色按照用户自己的意愿控制
- c# 如何调整 Button 控件在 Enable = false 时的字体颜色
- 改变ListBox的条目颜色或字体颜色(2)
- ALV 输出控制字体颜色 例程
- vc修改列表框LISTBOX的背景颜色及字体颜色及其使用方法
- 改变ListBox的条目颜色或字体颜色
- 改变Listbox中的字体颜色
- C# javascript 查找字体突出显示(改变大小、颜色)
- C# winform中,简单实现Listview.Subitem.BackColor.ForeColor改变字体颜色,Listview.Subitem.BackColor 改变背景
- ALV 输出控制字体颜色
- 用代码控制richtextbox的部份字体大小和颜色
- 在DBGRID的指定单元格中,自由控制画上下方向箭头,指定字体颜色
- VC++中控制控件台字体颜色(转)
- 怎么改变listbox的item颜色(C#)
- C#设计带图标和自定义颜色的ListBox
- C#里面设置ListView单列(某个单元格的样式:颜色和字体等)。