Defining specific custom colors that appear in colordialog?

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

+3
source share
4 answers

In short, yes. MSDN covers here . The problem is that this is not done using Color- you need to process the value in the form of BGR sets, i.e. Each integer consists of colors like 00BBGGRR, so you shift the blue color to 16, the green color to 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 it looks like:

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
+5
source

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()
+5
source

The existing example contains an error.

purple.B is a byte, not an integer, so changing it to 8 or 16 bits will do nothing for the value. Each byte must first be discarded to an integer before being transferred. 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
+2
source

SIMPLFIED (based on Gabby)

If you know the ARGB of the target custom colors, 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]
0
source

Source: https://habr.com/ru/post/1714550/


All Articles