【发布时间】:2017-10-23 20:25:36
【问题描述】:
我用我在这个网站上找到的一些代码创建了一个颜色选择器组合框,虽然我现在找不到代码。
绘制颜色的代码:
Private Sub cboColorPicker_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs) Handles cboColor.DrawItem, cboBackground.DrawItem
Dim g As Graphics
Dim ItemBrush As Brush
Dim ItemColor As Color
Dim ItemFont As Font
Dim ItemName As String
Dim rect As Rectangle
g = e.Graphics
rect = e.Bounds
If e.Index >= 0 Then
'Get item color name
ItemName = CType(sender, ComboBox).Items(e.Index).ToString
'Get instance a font to draw item name with this style
ItemFont = New Font("Arial", 9, FontStyle.Regular)
'Get instance color from item name
ItemColor = Color.FromName(ItemName)
'Get instance brush with Solid style to draw background
ItemBrush = New SolidBrush(ItemColor)
'Draw the item name
g.DrawString(ItemName, ItemFont, Brushes.Black, rect.X, rect.Top)
'Draw the background with my brush style and rectangle of item
g.FillRectangle(ItemBrush, rect.X, rect.Y, rect.Width, rect.Height)
End If
End Sub
我这样填充组合框:
Private Sub PopulateColorCombo(ByVal cbo As ComboBox)
cbo.Items.Add("Black")
cbo.Items.Add("Blue")
cbo.Items.Add("Lime")
cbo.Items.Add("Cyan")
cbo.Items.Add("Red")
cbo.Items.Add("Fuchsia")
cbo.Items.Add("Yellow")
cbo.Items.Add("White")
cbo.Items.Add("Navy")
cbo.Items.Add("Green")
cbo.Items.Add("Teal")
cbo.Items.Add("Maroon")
cbo.Items.Add("Purple")
cbo.Items.Add("Olive")
cbo.Items.Add("Gray")
End Sub
我将值作为 int 存储在数据库中,例如黑色将是 -16777216。
这是允许用户自定义报告的表单的一部分。我想在加载数据时更新组合以根据数据库中存储的内容显示颜色。我似乎无法做到这一点。
所以说我加载为 HDI.Color = -16777216 的数据
Color.FromArgb(HDI.Color).ToString = "Color [A=255, R=0, G=0, B=0]"
Color.FromArgb(HDI.Color).Name = "ff000000"
但我无法获得实际的颜色名称。以下都不起作用。
cboColor.SelectedIndex = cboColor.FindStringExact(Color.FromArgb(HDI.Color).ToKnownColor.ToString)
cboColor.SelectedIndex = cboColor.FindStringExact(CType(Color.FromArgb(HDI.Color), Color).ToString)
cboColor.SelectedIndex = cboColor.FindStringExact(System.Drawing.ColorTranslator.FromHtml(Color.FromArgb(HDI.Color).Name).ToString)
我是否为此任务使用了错误的组合框?有没有办法让它工作?
我不想提供完整的颜色选择器,因为它们不会使用很多颜色,所以我试图为用户保持简单。我什至可以将颜色减半。
编辑:修复泄漏问题。
Private Sub cboColorPicker_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs) 处理 cboColor.DrawItem, cboBackground.DrawItem
Dim g As Graphics
Dim ItemColor As Color
Dim ItemName As String
Dim rect As Rectangle
g = e.Graphics
rect = e.Bounds
If e.Index >= 0 Then
'Get item color name
ItemName = CType(sender, ComboBox).Items(e.Index).ToString
'Get instance color from item name
ItemColor = Color.FromName(ItemName)
'Get instance brush with Solid style to draw background
Using ItemBrush = New SolidBrush(ItemColor)
'Draw the item name
g.DrawString(ItemName, DirectCast(sender, ComboBox).Font, Brushes.Black, rect.X, rect.Top)
'Draw the background with my brush style and rectangle of item
g.FillRectangle(ItemBrush, rect.X, rect.Y, rect.Width, rect.Height)
End Using
End If
End Sub
【问题讨论】:
-
我猜你从Color Picker Combo Box 得到了代码。您正在向组合框中添加“字符串”,所以我认为您只需将“字符串”存储在数据库中。
-
您的代码正在泄漏。您不需要为每个项目使用新字体。将 cbo 设置为所需的字体并使用它。刷子也应该扔掉
-
不是将
Color.ToArgb值存储在数据库中,而是存储 KnownColor 枚举值。这样您就可以重新创建 KnownColor 并访问其名称。 -
@LarsTech - 你是对的,这就是我从那里得到的。这种类型的更改需要更改六张表、六张对象和报告编写器。也许我会等着看是否有人真的需要任何颜色,然后再进行所有这些修改,除非您有任何其他想法。
-
@Plutonix - 太糟糕了,我会修复它,但我怀疑其他人看到的代码与我从 Lars 指出的链接中得到的代码相同。