- 积分
- 8835
- 明经币
- 个
- 注册时间
- 2004-6-10
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
发表于 2025-2-2 15:59:22
|
显示全部楼层
Private Sub Zsbd_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' 设置 ComboBox 的 DrawMode 为 OwnerDrawFixed
cmbColors.DrawMode = DrawMode.OwnerDrawFixed
' 加载颜色到下拉框
LoadColors()
End Sub
Private Sub LoadColors()
' 示例颜色以及对应的中文名称
Dim colorItems As (Color, String)() = {
(Color.Red, "红"),
(Color.Yellow, "黄"),
(Color.Green, "绿"),
(Color.Cyan, "青"),
(Color.Blue, "蓝"),
(Color.Magenta, "洋红"),
(Color.Black, "黑白")
}
' 添加颜色项到下拉框
For Each item In colorItems
cmbColors.Items.Add(New ColorItem(item.Item1, item.Item2))
Next
' 设置下拉框默认选择项
If cmbColors.Items.Count > 0 Then
cmbColors.SelectedIndex = 0
End If
End Sub
Private Sub CmbColors_DrawItem(sender As Object, e As DrawItemEventArgs) Handles cmbColors.DrawItem
' 确保索引是有效的
If e.Index < 0 Then Return
' 获取当前项
Dim item As ColorItem = CType(cmbColors.Items(e.Index), ColorItem)
' 绘制背景
e.DrawBackground()
' 绘制颜色矩形
Dim colorRect As Rectangle = New Rectangle(e.Bounds.Left + 2, e.Bounds.Top + 2, 20, e.Bounds.Height - 4)
Using brush As New SolidBrush(item.Color)
e.Graphics.FillRectangle(brush, colorRect)
End Using
' 绘制颜色矩形的边框
Using pen As New Pen(Color.Black)
e.Graphics.DrawRectangle(pen, colorRect)
End Using
' 绘制颜色名称
Using brush As New SolidBrush(e.ForeColor)
e.Graphics.DrawString(item.ColorName, e.Font, brush, e.Bounds.Left + 26, e.Bounds.Top + 2)
End Using
' 绘制焦点矩形
e.DrawFocusRectangle()
End Sub
' 自定义颜色项类
Private Class ColorItem
Public Property Color As Color
Public Property ColorName As String
Public Sub New(color As Color, colorName As String)
Me.Color = color
Me.ColorName = colorName
End Sub
Public Overrides Function ToString() As String
Return ColorName
End Function
End Class
|
|