新建一个winform项目,拖一个ListBox控件listBox1

 

 

  1. public Form1() 
  2.  
  3.  
  4.     InitializeComponent();             
  5.  
  6.     listBox1.DrawMode = DrawMode.OwnerDrawFixed; 
  7.  

首先需要设置DrawMode为DrawMode.OwnerDrawFixedDrawMode.OwnerDrawVariable 时,才触发该事件(DrawItem事件).,也可以通过设计器在属性面板里设置哈。

 

  1. private void listBox1_DrawItem(object sender, DrawItemEventArgs e) 
  2.     e.DrawBackground();//绘制背景 
  3.     Brush myBrush = Brushes.Black; 
  4.  
  5.     switch (e.Index) 
  6.     { 
  7.         case 0: 
  8.             myBrush = Brushes.Red; 
  9.             break
  10.         case 1: 
  11.             myBrush = Brushes.Orange; 
  12.             break
  13.         case 2: 
  14.             myBrush = Brushes.Purple; 
  15.             break
  16.     } 
  17.     e.DrawFocusRectangle();//焦点框 
  18.  
  19.     //文本 
  20.     e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, myBrush, e.Bounds, StringFormat.GenericDefault); 

这是采用附加委托的方式处理,可以查看设计器产生的代码:

 

  1. this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem); 

事件由ListBox触发.

下面,再举一反三下,实现交替颜色的列表框:

 

  1. public partial class Form1 : Form 
  2.     private Color RowBackColorAlt=Color.FromArgb(200,200,200);//交替色 
  3.     private Color RowBackColorSel = Color.FromArgb(150, 200, 250);//选择项目颜色 
  4.  
  5.     public Form1() 
  6.     { 
  7.         InitializeComponent();             
  8.         listBox1.DrawMode = DrawMode.OwnerDrawFixed; 
  9.     } 
  10.  
  11.     private void listBox1_DrawItem(object sender, DrawItemEventArgs e) 
  12.     { 
  13.         Brush myBrush = Brushes.Black; 
  14.  
  15.         if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) 
  16.         { 
  17.             myBrush = new SolidBrush(RowBackColorSel); 
  18.         } 
  19.         else if (e.Index % 2 == 0) 
  20.         { 
  21.             myBrush = new SolidBrush(RowBackColorAlt); 
  22.         } 
  23.         else 
  24.         { 
  25.             myBrush = new SolidBrush(Color.White); 
  26.         } 
  27.         e.Graphics.FillRectangle(myBrush, e.Bounds); 
  28.         e.DrawFocusRectangle();//焦点框 
  29.  
  30.         //文本 
  31.         e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault); 
  32.     } 
  33.  

上图:

 

 

C# Winform编程ListBox之DrawItem事件_休闲

 接下来,设置ItemHeight,每一项的高度:

 

  1. public Form1() 
  2.     InitializeComponent();             
  3.     listBox1.DrawMode = DrawMode.OwnerDrawFixed; 
  4.     listBox1.ItemHeight = 24; 

文字,剧中:

 

  1. //文本 
  2. StringFormat strFormat = new StringFormat(); 
  3. strFormat.Alignment = StringAlignment.Center; 
  4. strFormat.LineAlignment = StringAlignment.Center; 
  5. e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, Brushes.Black, e.Bounds,strFormat); 

上图片:

 

C# Winform编程ListBox之DrawItem事件_休闲_02

 下面看看MeasureItem 事件:

仅当 DrawMode 属性被设置为 OwnerDrawVariable 时,才引发该事件。

  1. public partial class Form1 : Form 
  2.     private Color RowBackColorAlt=Color.FromArgb(200,200,200);//交替色 
  3.     private Color RowBackColorSel = Color.FromArgb(150, 200, 250);//选择项目颜色 
  4.  
  5.     public Form1() 
  6.     { 
  7.         InitializeComponent();             
  8.         listBox1.DrawMode = DrawMode.OwnerDrawVariable; 
  9.     } 
  10.  
  11.     private void listBox1_DrawItem(object sender, DrawItemEventArgs e) 
  12.     { 
  13.         Brush myBrush = Brushes.Black; 
  14.  
  15.         if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) 
  16.         { 
  17.             myBrush = new SolidBrush(RowBackColorSel); 
  18.         } 
  19.         else if (e.Index % 2 == 0) 
  20.         { 
  21.             myBrush = new SolidBrush(RowBackColorAlt); 
  22.         } 
  23.         else 
  24.         { 
  25.             myBrush = new SolidBrush(Color.White); 
  26.         } 
  27.         e.Graphics.FillRectangle(myBrush, e.Bounds); 
  28.         e.DrawFocusRectangle();//焦点框 
  29.  
  30.         //文本 
  31.         StringFormat strFormat = new StringFormat(); 
  32.         strFormat.Alignment = StringAlignment.Center; 
  33.         strFormat.LineAlignment = StringAlignment.Center; 
  34.         e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds,strFormat); 
  35.     } 
  36.  
  37.     private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e) 
  38.     { 
  39.         e.ItemHeight = (1+e.Index)*12; 
  40.     } 
  41.