views:

113

answers:

2

is it possible in winforms, vb.net, to define the specific custom colors that will appear in the custom color boxes in colordialog?

+2  A: 

In short, yes. MSDN covers it here. The problem is that it isn't done via Color - you need to handle the value as BGR sets - i.e. each integer is composed of the colors as 00BBGGRR, so you left-shift blue by 16, green by 8, and use red "as is".

My VB sucks, but in C#, to add purple:

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

reflector assures me that this is similar to:

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
Marc Gravell
+1 for the bitwise manipulation. I really wish that MS would document this stuff better.
Repo Man
A: 

The existing example contains an error.

purple.B is a byte not an integer, so shifting it 8 or 16 bits will do nothing to the value. Each byte first has to be cast to an integer before shifting it. Something like this (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
axio