'DrawItem事件处理器
'项目描绘
Private Sub ComboBox1_DrawItem(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DrawItemEventArgs)
'背景描绘
'强调表示被选择的项目
e.DrawBackground()
Dim cmb As Windows.Forms.ComboBox = CType(sender, Windows.Forms.ComboBox)
'表示项目的字符串
Dim txt As String
If (e.Index > -1) Then
txt = cmb.Items(e.Index).ToString()
Else
txt = cmb.Text
End If
'使用格式刷
Dim b As Brush = New SolidBrush(e.ForeColor)
'字符串描绘
Dim ym As Single = (e.Bounds.Height - _
e.Graphics.MeasureString(txt, cmb.Font).Height) / 2
e.Graphics.DrawString(txt, cmb.Font, b, _
e.Bounds.X, e.Bounds.Y + ym)
b.Dispose()
'描绘四角表示焦点的形
e.DrawFocusRectangle()
End Sub
'该过程可独立
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ComboBox2.DrawMode = DrawMode.OwnerDrawFixed '绘制模式为手动调整
ComboBox2.ItemHeight = 20 '您设定的项高度
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'限制控件进行自我描绘
ComboBox1.DrawMode = DrawMode.OwnerDrawFixed
'设定项目的高度
ComboBox1.ItemHeight = 20
'追加DrawItem事件处理器
AddHandler ComboBox1.DrawItem, AddressOf ComboBox1_DrawItem
'追加ComboBox1的项目
ComboBox1.Items.Add("早上好。")
ComboBox1.Items.Add("中午好。")
ComboBox1.Items.Add("晚上好。")
End Sub