Jef*_*ffH 17
本文介绍如何使用ListBox的DrawItem,并将DrawMode设置为OwnerDraw值之一.基本上,你做这样的事情:
listBox1.DrawMode = OwnerDrawFixed;
listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem);
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
// TODO: Split listBox1.Items[e.Index].ToString() and then draw each separately in a different color
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold),new SolidBrush(color[e.Index]),e.Bounds);
}
Run Code Online (Sandbox Code Playgroud)
而不是单个DrawString调用,将listBox1.Items [e.Index] .ToString()拆分为单词,并为每个单词单独调用DrawString.你必须用每个单词的x,y位置或边界矩形替换e.bounds.
相同的方法应该适用于ListView.