2009-08-06 21 views

答えて

4

要するに、はい。 MSDNはそれをカバーしますhere。問題はそれがColor経由で行われていないことです - あなたはBGRセットとして値を扱う必要があります。つまり、各整数は00BBGGRRという色で構成されているので、青で16、青で8、そのまま"。

私のVBは吸うが、C#で、紫追加する:

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がこのことをより良く記録することを望む。 –

2

既存の例では、エラーが含まれています

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(); 
    } 

リフレクターが、これはと類似していることを私に保証します。

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] 
関連する問題