2011-03-06 54 views

回答

4

可以通过调用组合框的DRAWITEM实现你的目标,更多信息参见下面的方法。

private void comboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) 
    { 
     // Override this function to draw items in the Color comboBox 

     // Get the Graphics Object (aka. CDC or Device Context Object) 
     // passed via the DrawItemEventArgs parameter 
     Graphics g = e.Graphics ; 

     // Get the bounding rectangle of the item currently being painted 
     Rectangle r = e.Bounds ; 

     if (e.Index >= 0) 
     { 
      Rectangle rd = r; 
      r.X = r.Right ; 

      // Get the brush object, at the specifid index in the colorArray 
      SolidBrush b = (SolidBrush)colorArray[e.Index]; 
      // Fill a portion of the rectangle with the selected brush 
      g.FillRectangle(b, rd); 


      // Draw the rectangle 
      e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black), 2), r); 

      if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect)) 
      { 
       // if the item is not selected draw it with a different color 
       e.Graphics.FillRectangle(new SolidBrush(Color.White) , r); 
       e.DrawFocusRectangle(); 
      } 
      else 
      { 
       // if the item is selected draw it with a different color 
       e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue) , r); 
       e.DrawFocusRectangle(); 
      } 
     } 
    } 
+0

我尝试使用DRAWITEM但它不是引发 – kusanagi 2011-03-06 10:42:05

+0

@kusanagi:设置'ComboBox.DrawMode = OwnerDrawFixed或OwnerDrawVariable' – digEmAll 2011-03-06 10:45:37

+0

@kusanagi:在DrawItem中,您必须为comboBox的每个项目绘制字符串,顺便说一下,您是否尝试过使用上述方法发送给您的?对不起,我忘了说, 请设置comboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed – SharpUrBrain 2011-03-06 10:56:34