Alex Gordon
Alex Gordon

Reputation: 60912

defining specific custom colors that will appear in the colordialog?

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

Upvotes: 3

Views: 14274

Answers (4)

Vinelander
Vinelander

Reputation: 1

SIMPLFIED (based on Gabby)

If you know the ARGB of your target custom colors then use:

 '                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]

Upvotes: 0

Gabby
Gabby

Reputation: 51

If you want to have more than 1 custom color, you can do this:

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

Upvotes: 5

user157418
user157418

Reputation:

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

Upvotes: 2

Marc Gravell
Marc Gravell

Reputation: 1064244

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

Upvotes: 5

Related Questions