从aRGB值项目显示组合框中的颜色(Display Colors in ComboBox from aRGB Value item)

我的问题如下:

我有一个装有aRGB代码(从excel文件中提取)的Combobox,如下所示:

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

此代码不会改变任何内容。 我的combobox列表中仍然有rbg代码。 任何人都可以告诉我这个代码有什么不对吗?

My problem is the following:

I have a Combobox filled with aRGB codes (extracted from an excel file), like this:

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

My goal is to display a list of colors instead of their rgb code. So, I tried to do this, unsuccessfully:

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

This code doesn't change anything. I still have rbg codes in my combobox's list. Can anyone please tell me what's wrong whith this code?

最满意答案

你有几个问题。 如上所述,如果正在绘制文本并且您的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) ),您将不得不做同样的事情List(Of Color)

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

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

在这里输入图像描述

You have several problems. As noted, if the Text is being drawn and your DrawItem code isnt drawing it, then DrawMode is probably not set to OwnderDrawFixed.

Then, once you say you will handle drawing the items, you have to handle all the drawing. That includes the Selected item highlighting, the background and the focus rectangle. The little color boxes you draw leave room to also display the text, so this will show how to do both.

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

The format of the ARGB string appears to be the InvariantString format which is used in all sorts of export and serialization. The code shows how to convert using it, but String.Split will work too. You will have to do the same when they make a selection to actually create a color from the text (or do all that up front and run off a List(Of Color))

The important thing is to check to see if the item is the Selected item and use the correct forecolor for any text you do draw. TheFocusRectangle is also shown.

There is plenty of room for both the text and a color swatch, but if you really do not want the ARGB text, just skip the DrawText code, and consider filling the entire rectangle with color rather than drawing a swatch:

enter image description here

更多推荐

代码,Sub,code,电脑培训,计算机培训,IT培训"/> <meta name="description"