2013-03-08 116 views

回答

6

有一个ItemHeight属性。

您必须将DrawMode财产更改为OwnerDrawFixed才能使用自定义ItemHeight

当您使用DrawMode.OwnerDrawFixed时,您必须“手动”绘制/绘制项目。

下面是一个例子:从上述链路Combobox appearance

代码(写入/由max提供):

public class ComboBoxEx : ComboBox 
{ 
    public ComboBoxEx() 
    { 
     base.DropDownStyle = ComboBoxStyle.DropDownList; 
     base.DrawMode = DrawMode.OwnerDrawFixed; 
    } 

    protected override void OnDrawItem(DrawItemEventArgs e) 
    { 
     e.DrawBackground(); 
     if(e.State == DrawItemState.Focus) 
      e.DrawFocusRectangle(); 
     var index = e.Index; 
     if(index < 0 || index >= Items.Count) return; 
     var item = Items[index]; 
     string text = (item == null)?"(null)":item.ToString(); 
     using(var brush = new SolidBrush(e.ForeColor)) 
     { 
      e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; 
      e.Graphics.DrawString(text, e.Font, brush, e.Bounds); 
     } 
    } 
} 
相关问题