2009-08-06 44 views

回答

4

總之,是的。 MSDN覆蓋它here。問題是,它不是通過Color - 你需要處理的價值作爲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(); 
    } 

反射向我保證,這是類似於:

Using dlg As ColorDialog = New ColorDialog 
    Dim purple As Color = Color.Purple 
    Dim i As Integer = (((purple.B << &H10) Or (purple.G << 8)) Or purple.R) 
    dlg.CustomColors = New Integer() { i } 
    dlg.ShowDialog 
End Using 
+0

+1爲按位操作。我真的希望MS能更好地記錄這些東西。 – 2010-10-04 14:40:25

2

現有的示例包含一個錯誤。

purple.B是一個字節不是一個整數,所以移動它的8位或16位將不會對該值做任何事情。在移位之前,每個字節必須首先被轉換爲整數。像這樣的東西(VB.NET):

Dim CurrentColor As Color = Color.Purple 
Using dlg As ColorDialog = New ColorDialog 
    Dim colourBlue As Integer = CurrentColor.B 
    Dim colourGreen As Integer = CurrentColor.G 
    Dim colourRed As Integer = CurrentColor.R 
    Dim newCustomColour as Integer = colourBlue << 16 Or colourGreen << 8 Or colourRed 
    dlg.CustomColors = New Integer() { newCustomColour } 
    dlg.ShowDialog 
End Using 
4

如果你想有超過1個自定義顏色,你可以這樣做:

  '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() 
0

SIMPLFIED(基於愛說話)

如果你知道目標自定義顏色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]