Vb.net 定义将显示在“颜色”对话框中的特定自定义颜色?
在winforms、vb.net中,是否可以定义将显示在colordialog的自定义颜色框中的特定自定义颜色?简而言之,是的。MSDN涵盖了它。问题在于,这不是通过Vb.net 定义将显示在“颜色”对话框中的特定自定义颜色?,vb.net,winforms,Vb.net,Winforms,在winforms、vb.net中,是否可以定义将显示在colordialog的自定义颜色框中的特定自定义颜色?简而言之,是的。MSDN涵盖了它。问题在于,这不是通过颜色来完成的-您需要将值作为BGR集来处理-即,每个整数由00BBGGRR的颜色组成,因此您将蓝移16,绿移8,并使用红色“原样” 我的VB很烂,但在C#中,要添加紫色: using (ColorDialog dlg = new ColorDialog()) { Color purple = Colo
颜色来完成的-您需要将值作为BGR集来处理-即,每个整数由00BBGGRR的颜色组成,因此您将蓝移16,绿移8,并使用红色“原样”
我的VB很烂,但在C#中,要添加紫色:
using (ColorDialog dlg = new ColorDialog())
{
Color purple = Color.Purple;
int i = (purple.B << 16) | (purple.G << 8) | purple.R;
dlg.CustomColors = new[] { i };
dlg.ShowDialog();
}
使用(ColorDialog dlg=new ColorDialog())
{
紫色=颜色。紫色;
inti=(purple.B现有示例包含一个错误
紫色.B是一个字节而不是整数,因此将其移位8或16位不会对值产生任何影响。在移位之前,必须先将每个字节转换为整数。类似于(VB.NET):
Dim CurrentColor As Color=Color.Purple
使用dlg作为ColorDialog=新建ColorDialog
Dim colorBlue作为整数=CurrentColor.B
Dim colorGreen作为整数=CurrentColor.G
Dim colored为整数=CurrentColor.R
Dim NewCustomColor as Integer=colorBlue如果您想拥有多个自定义颜色,可以执行以下操作:
'Define custom colors
Dim cMyCustomColors(1) As Color
cMyCustomColors(0) = Color.FromArgb(0, 255, 255) 'aqua
cMyCustomColors(1) = Color.FromArgb(230, 104, 220) 'bright pink
'Convert colors to integers
Dim colorBlue As Integer
Dim colorGreen As Integer
Dim colorRed As Integer
Dim iMyCustomColor As Integer
Dim iMyCustomColors(cMyCustomColors.Length - 1) As Integer
For index = 0 To cMyCustomColors.Length - 1
'cast to integer
colorBlue = cMyCustomColors(index).B
colorGreen = cMyCustomColors(index).G
colorRed = cMyCustomColors(index).R
'shift the bits
iMyCustomColor = colorBlue << 16 Or colorGreen << 8 Or colorRed
iMyCustomColors(index) = iMyCustomColor
Next
ColorDialog1.CustomColors = iMyCustomColors
ColorDialog1.ShowDialog()
定义自定义颜色
暗淡CMYCustomColor(1)作为颜色
cMyCustomColors(0)=颜色。来自argb(0,255,255)'aqua
cMyCustomColors(1)=颜色。来自argb(230104220)“亮粉色”
'将颜色转换为整数
暗蓝色为整数
暗绿色为整数
暗红色为整数
Dim iMyCustomColor为整数
将iMyCustomColors(cMyCustomColors.Length-1)变暗为整数
对于索引=0到cMyCustomColors.Length-1
'强制转换为整数
colorBlue=cMyCustomColors(索引).B
colorGreen=cMyCustomColors(索引).G
colorRed=cMyCustomColors(索引).R
"换位子"
iMyCustomColor=colorBlue简化(基于Gabby)
如果您知道目标自定义颜色的ARGB,请使用:
' Define custom colors
ColorDialog1.CustomColors = New Integer() {(255 << 16 Or 255 << 8 Or 0), _
(220 << 16 Or 104 << 8 Or 230), _
(255 << 16 Or 214 << 8 Or 177)}
ColorDialog1.ShowDialog()
'where colors are (arbg) 1: 0,255,255 [aqua]
' 2: 230,104,220 [bright pink]
' 3: 177,214,255 [whatever]
定义自定义颜色
ColorDialog1.CustomColors=New Integer(){(255+1)用于按位操作。我真希望MS能够更好地记录这些内容。
'Define custom colors
Dim cMyCustomColors(1) As Color
cMyCustomColors(0) = Color.FromArgb(0, 255, 255) 'aqua
cMyCustomColors(1) = Color.FromArgb(230, 104, 220) 'bright pink
'Convert colors to integers
Dim colorBlue As Integer
Dim colorGreen As Integer
Dim colorRed As Integer
Dim iMyCustomColor As Integer
Dim iMyCustomColors(cMyCustomColors.Length - 1) As Integer
For index = 0 To cMyCustomColors.Length - 1
'cast to integer
colorBlue = cMyCustomColors(index).B
colorGreen = cMyCustomColors(index).G
colorRed = cMyCustomColors(index).R
'shift the bits
iMyCustomColor = colorBlue << 16 Or colorGreen << 8 Or colorRed
iMyCustomColors(index) = iMyCustomColor
Next
ColorDialog1.CustomColors = iMyCustomColors
ColorDialog1.ShowDialog()
' Define custom colors
ColorDialog1.CustomColors = New Integer() {(255 << 16 Or 255 << 8 Or 0), _
(220 << 16 Or 104 << 8 Or 230), _
(255 << 16 Or 214 << 8 Or 177)}
ColorDialog1.ShowDialog()
'where colors are (arbg) 1: 0,255,255 [aqua]
' 2: 230,104,220 [bright pink]
' 3: 177,214,255 [whatever]