【问题标题】:Display Colors in ComboBox from aRGB Value item从 aRGB 值项在 ComboBox 中显示颜色
【发布时间】:2016-08-30 13:16:49
【问题描述】:

我的问题如下:

我有一个包含 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 代码。谁能告诉我这段代码有什么问题?

【问题讨论】:

  • 我假设要绘制的每种颜色都由Color.FromArgb(CB_Color.Items(e.Index)) 定义。用户只需从下拉列表中选择一种颜色,我想我可以通过使用所选项目的索引知道他选择了什么。

标签: vb.net colors combobox argb


【解决方案1】:

你有几个问题。如前所述,如果正在绘制文本而您的 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 代码,并考虑用颜色填充整个矩形而不是绘制样本:

【讨论】:

  • 这很好用!我现在只需要显示所选颜色而不是文本。
  • 那只需要设置背景色,但它会破坏这里的解决方案。
  • 我已将 DropDownStyle 属性更改为 DropDownList,并添加了 `Private Sub CB_Color_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CB_Color.SelectedIndexChanged Me.CB_Color.BackColor = Color。 White End Sub` ,它可以在不破坏您的解决方案的情况下正常工作。
【解决方案2】:

首先在组合框属性中找到名为“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 只接受整数值。

【讨论】:

    猜你喜欢
    • 2012-09-30
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    • 2016-05-15
    • 2015-10-11
    • 2012-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多