新建一个winform项目,拖一个ListBox控件listBox1
- public Form1()
- {
- InitializeComponent();
- listBox1.DrawMode = DrawMode.OwnerDrawFixed;
- }
首先需要设置DrawMode为DrawMode.OwnerDrawFixed 或 DrawMode.OwnerDrawVariable 时,才触发该事件(DrawItem事件).,也可以通过设计器在属性面板里设置哈。
- private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
- {
- e.DrawBackground();//绘制背景
- Brush myBrush = Brushes.Black;
- switch (e.Index)
- {
- case 0:
- myBrush = Brushes.Red;
- break;
- case 1:
- myBrush = Brushes.Orange;
- break;
- case 2:
- myBrush = Brushes.Purple;
- break;
- }
- e.DrawFocusRectangle();//焦点框
- //文本
- e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
- }
这是采用附加委托的方式处理,可以查看设计器产生的代码:
- this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem);
事件由ListBox触发.
下面,再举一反三下,实现交替颜色的列表框:
- public partial class Form1 : Form
- {
- private Color RowBackColorAlt=Color.FromArgb(200,200,200);//交替色
- private Color RowBackColorSel = Color.FromArgb(150, 200, 250);//选择项目颜色
- public Form1()
- {
- InitializeComponent();
- listBox1.DrawMode = DrawMode.OwnerDrawFixed;
- }
- private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
- {
- Brush myBrush = Brushes.Black;
- if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
- {
- myBrush = new SolidBrush(RowBackColorSel);
- }
- else if (e.Index % 2 == 0)
- {
- myBrush = new SolidBrush(RowBackColorAlt);
- }
- else
- {
- myBrush = new SolidBrush(Color.White);
- }
- e.Graphics.FillRectangle(myBrush, e.Bounds);
- e.DrawFocusRectangle();//焦点框
- //文本
- e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
- }
- }
上图:
接下来,设置ItemHeight,每一项的高度:
- public Form1()
- {
- InitializeComponent();
- listBox1.DrawMode = DrawMode.OwnerDrawFixed;
- listBox1.ItemHeight = 24;
- }
文字,剧中:
- //文本
- StringFormat strFormat = new StringFormat();
- strFormat.Alignment = StringAlignment.Center;
- strFormat.LineAlignment = StringAlignment.Center;
- e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, Brushes.Black, e.Bounds,strFormat);
上图片:
下面看看MeasureItem 事件:
仅当 DrawMode 属性被设置为 OwnerDrawVariable 时,才引发该事件。
- public partial class Form1 : Form
- {
- private Color RowBackColorAlt=Color.FromArgb(200,200,200);//交替色
- private Color RowBackColorSel = Color.FromArgb(150, 200, 250);//选择项目颜色
- public Form1()
- {
- InitializeComponent();
- listBox1.DrawMode = DrawMode.OwnerDrawVariable;
- }
- private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
- {
- Brush myBrush = Brushes.Black;
- if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
- {
- myBrush = new SolidBrush(RowBackColorSel);
- }
- else if (e.Index % 2 == 0)
- {
- myBrush = new SolidBrush(RowBackColorAlt);
- }
- else
- {
- myBrush = new SolidBrush(Color.White);
- }
- e.Graphics.FillRectangle(myBrush, e.Bounds);
- e.DrawFocusRectangle();//焦点框
- //文本
- StringFormat strFormat = new StringFormat();
- strFormat.Alignment = StringAlignment.Center;
- strFormat.LineAlignment = StringAlignment.Center;
- e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds,strFormat);
- }
- private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
- {
- e.ItemHeight = (1+e.Index)*12;
- }
- }