在 ComboBox 中显示来自 RGB 值项的颜色

Display Colors in ComboBox from aRGB Value item

我的问题如下:

我有一个组合框,里面装满了 aRGB 代码(从 excel 文件中提取),如下所示:

255, 149, 55, 39
255, 0, 176, 80
255, 0, 112, 192
...

我的目标是显示颜色列表而不是它们的 rgb 代码。 所以,我尝试这样做,但没有成功:

 Private Sub CB_Color_DrawItem(ByVal sender As System.Object, ByVal e As DrawItemEventArgs) Handles CB_Color.DrawItem

    If e.Index = -1 Then
        Exit Sub
    End If

    Dim colBrush As Brush = New SolidBrush(Color.FromArgb(CB_Color.Items(e.Index)))
    'Drawing rectangles for the color values
    e.Graphics.DrawRectangle(New Pen(Brushes.Black), e.Bounds.Left + 2, 
                       e.Bounds.Top + 2, 30, e.Bounds.Height - 5)
    e.Graphics.FillRectangle(colBrush, e.Bounds.Left + 3, e.Bounds.Top + 3, 
                       29, e.Bounds.Height - 6)

End Sub

这段代码没有任何改变。我的组合框列表中仍然有 rbg 代码。谁能告诉我这段代码有什么问题吗?

首先在组合框属性中找到名为 "DrawMode" 的 属性。将此值更改为 'OwnerDrawFixed'。这是指示代码或操作系统是否将处理绘图的值。

然后您需要添加和更改以下代码:

Dim colorArray() As String = ComboBox1.Items(e.Index).ToString.Split(",")

Dim colBrush As Brush = New SolidBrush(Color.FromArgb(CInt(colorArray(0)), CInt(colorArray(1)), CInt(colorArray(2)), CInt(colorArray(3))))

我们这样做是因为 FromARGB 只接受整数值。

你有几个问题。如前所述,如果正在绘制文本而您的 DrawItem 代码未绘制它,则 DrawMode 可能未设置为 OwnderDrawFixed.

然后,一旦您说您将处理绘制项目,您就必须处理所有 绘图。这包括所选项目突出显示、背景和焦点矩形。您绘制的小颜色框留出空间也可以显示文本,因此这将展示如何同时执行这两种操作。

Private Sub cbox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles cbox1.DrawItem
    If e.Index = -1 Then Return

    Dim thisText As String = cbox1.Items(e.Index).ToString()
    Dim thisColor As Color = CType(TypeDescriptor.GetConverter(GetType(Color)).
                                            ConvertFromInvariantString(thisText), 
                                            Color)
    ' use HeighLight when needed
    Dim foreclr As Color = If(e.State.HasFlag(DrawItemState.Selected),
                              SystemColors.HighlightText,
                              cbox1.ForeColor)

    e.DrawBackground()
    Using br As New SolidBrush(thisColor)
        e.Graphics.DrawRectangle(New Pen(Brushes.Black),
                                 e.Bounds.Left + 2, e.Bounds.Top + 2, 30,
                                 e.Bounds.Height - 5)
        e.Graphics.FillRectangle(br, e.Bounds.Left + 3, e.Bounds.Top + 3,
                     29, e.Bounds.Height - 6)

        Dim tRect = New Rectangle(e.Bounds.Left + 32, e.Bounds.Top + 2,
                                  e.Bounds.Width - 32, e.Bounds.Height - 4)
        TextRenderer.DrawText(e.Graphics, String.Format("255, {0:000}, {1:000}, {2:000}",
                                              thisColor.R, thisColor.G, thisColor.B),
                                              cbox1.Font, tRect, foreclr)
    End Using

    e.DrawFocusRectangle()

End Sub

ARGB 字符串的格式似乎是用于各种导出和序列化的 InvariantString 格式。该代码显示了如何使用它进行转换,但 String.Split 也可以。当他们做出选择以实际从文本中创建颜色时,您将必须执行相同的操作(或者预先执行所有操作,然后 运行 关闭 List(Of Color)

重要的是检查该项目是否为所选项目,并为您绘制的任何文本使用正确的前景色。还显示了FocusRectangle

文本和颜色样本都有足够的空间,但如果您真的不想要 ARGB 文本,只需跳过 DrawText 代码,并考虑用颜色填充整个矩形而不是绘制样本: