cDialogColor class
A cDialogColor shows the Windows Color dialog, in which the user picks a color from the basic colors, from 16 custom colors or, in the expanded dialog, by typing its red, green and blue parts. It holds the chosen color as a Long, in the same layout as the RGB function and the vb color constants (&H00BBGGRR), and converts it to and from the HTML form #RRGGBB.
Properties: AnyColor, Blue, Color, FullOpen, Green, OwnerHwnd, PreventFullOpen, Red, SolidColor. Methods: FromHtmlColor, GetCustomColor, Reset, SetCustomColor, SetRgb, ShowDialog, ToHtmlColor, ToRgbString.
Create an object with Dim dlg As New cDialogColor. The class is declared [COMCreatable(False)], so CreateObject cannot make one, and New can.
Dim dlg As New cDialogColor
dlg.Color = RGB(255, 128, 0)
dlg.FullOpen = True
If dlg.ShowDialog() Then
Debug.Print dlg.ToHtmlColor()
Debug.Print dlg.ToRgbString()
Else
Debug.Print "Cancelled"
End If
How the dialog is shown
ShowDialog calls ChooseColorW with the current Color as the starting color (flag CC_RGBINIT, always set) and the 16 custom colors of the object. The properties FullOpen, PreventFullOpen, SolidColor and AnyColor set the flags CC_FULLOPEN, CC_PREVENTFULLOPEN, CC_SOLIDCOLOR and CC_ANYCOLOR when they are True. The class sets no hook or template, no help button and no owner other than OwnerHwnd. The dialog is modal and has the title Color.
When the user chooses OK, ShowDialog stores the color in Color and returns True. When the user chooses Cancel, or closes the dialog, it returns False and leaves Color as it was. No error is raised in either case, and no value tells a cancel from a failure of the call.
The color is a plain number. A system color constant such as vbButtonFace (&H8000000F) is not turned into the color it names: it is passed to the dialog as it is, and Red, Green and Blue do not read a value with the high bit set in a meaningful way (see Color).
Custom colors
The object keeps 16 custom colors, numbered 0 to 15, for the 16 custom boxes of the dialog. They start as the colors of QBColor(0) to QBColor(15) and last as long as the object does, so a second dialog of the same object shows the custom colors that the first one left. Nothing is saved to the registry or to a file.
Note
The Add to Custom Colors button of the expanded dialog writes into the custom colors of the object at once. Choosing Cancel afterwards leaves Color unchanged but keeps the added custom color. Reset restores the 16 starting colors.
Properties
AnyColor
Whether the dialog shows all the colors of the basic set.
Syntax: object.AnyColor [ = value ]
A Boolean, True by default. When it is True, ShowDialog sets CC_ANYCOLOR. Windows decides what the flag changes on the screen in use.
Dim dlg As New cDialogColor
dlg.AnyColor = False
dlg.SolidColor = True
Debug.Print dlg.AnyColor
Blue
The blue part of the color.
Syntax: object.Blue [ = value ]
A Long from 0 to 255. Setting a value below 0 stores 0, and above 255 stores 255, without an error. Reading returns the third byte of Color. Setting Blue changes the other two parts not at all.
Dim dlg As New cDialogColor
dlg.Color = RGB(10, 20, 30)
dlg.Blue = 300
Debug.Print dlg.Blue
Debug.Print dlg.ToRgbString()
' Output:
' 255
' RGB(10, 20, 255)
Color
The selected color.
Syntax: object.Color [ = value ]
A Long in the layout &H00BBGGRR: red in the lowest byte, then green, then blue. Its default is vbWhite (&HFFFFFF). It is the color that the dialog shows when it opens, and the color that the user chose after a ShowDialog that returned True.
Any Long is accepted and stored as it is. For a negative value, such as a system color constant or -1, Red reads the lowest byte, but Green and Blue divide the negative number, which rounds toward zero, before they take a byte, and so read other bytes than the ones of the color: -1 reads as red 255, green 0, blue 0, and &H80000005 as red 5, green 1, blue 1.
Dim dlg As New cDialogColor
dlg.Color = vbWhite
Debug.Print Hex$(dlg.Color)
dlg.Color = RGB(10, 20, 30)
Debug.Print Hex$(dlg.Color)
Debug.Print dlg.Red
Debug.Print dlg.Green
Debug.Print dlg.Blue
' Output:
' FFFFFF
' 1E140A
' 10
' 20
' 30
FullOpen
Whether the dialog opens with the part for defining custom colors already shown.
Syntax: object.FullOpen [ = value ]
A Boolean, False by default. When it is True, ShowDialog sets CC_FULLOPEN: the dialog opens expanded, with the Red, Green and Blue boxes filled with the parts of Color and enabled, and the Define Custom Colors button disabled. When it is False, those boxes are empty and disabled until the user chooses the button.
With FullOpen and PreventFullOpen both True, the dialog opens expanded and the button stays disabled.
Dim dlg As New cDialogColor
dlg.FullOpen = True
dlg.Color = RGB(10, 20, 30)
Debug.Print dlg.ShowDialog()
Green
The green part of the color.
Syntax: object.Green [ = value ]
A Long from 0 to 255, clamped as Blue is. Reading returns the second byte of Color.
Dim dlg As New cDialogColor
dlg.Color = RGB(10, 20, 30)
dlg.Green = -4
Debug.Print dlg.Green
Debug.Print dlg.ToHtmlColor()
' Output:
' 0
' #0A001E
OwnerHwnd
The window that owns the dialog.
Syntax: object.OwnerHwnd [ = value ]
A LongPtr window handle, 0 by default, which gives the dialog no owner. ShowDialog passes it as the owner window of the call.
Private Sub PickColor(ByVal ownerWindow As LongPtr)
Dim dlg As New cDialogColor
dlg.OwnerHwnd = ownerWindow ' the handle of a window of the program
Debug.Print dlg.OwnerHwnd
Debug.Print dlg.ShowDialog()
End Sub
PreventFullOpen
Whether the user is kept from defining custom colors.
Syntax: object.PreventFullOpen [ = value ]
A Boolean, False by default. When it is True, ShowDialog sets CC_PREVENTFULLOPEN, which disables the Define Custom Colors button. A dialog that is not expanded then stays small. With FullOpen also True it opens expanded, and the user can still type values.
Dim dlg As New cDialogColor
dlg.PreventFullOpen = True
Debug.Print dlg.ShowDialog()
Red
The red part of the color.
Syntax: object.Red [ = value ]
A Long from 0 to 255, clamped as Blue is. Reading returns the lowest byte of Color.
Dim dlg As New cDialogColor
dlg.Red = 200
dlg.Green = 100
dlg.Blue = 50
Debug.Print Hex$(dlg.Color)
' Output:
' 3264C8
SolidColor
Whether the dialog shows only solid colors.
Syntax: object.SolidColor [ = value ]
A Boolean, False by default. When it is True, ShowDialog sets CC_SOLIDCOLOR. Windows decides what the flag changes on the screen in use.
Dim dlg As New cDialogColor
dlg.SolidColor = True
Debug.Print dlg.SolidColor
Methods
FromHtmlColor
Sets the color from text of the form #RRGGBB.
Syntax: object.FromHtmlColor ( htmlColor )
- htmlColor
- required A String of six hexadecimal digits, with or without a leading
#. Spaces before and after are ignored, and the letters may be of either case.
The method sets Red, Green and Blue from the three pairs of digits. It raises no error for text it cannot read, and leaves the color as it was when the text, without its #, is not exactly 6 characters long: #abc, an empty string, a seventh digit and &HFF0000 are all ignored. A pair that is not hexadecimal ends the method at that pair, with the parts before it already set: #12345G sets red to &H12 and green to &H34 and leaves blue as it was.
Dim dlg As New cDialogColor
dlg.FromHtmlColor "#00FF7F"
Debug.Print dlg.ToRgbString()
dlg.FromHtmlColor " ff8000 "
Debug.Print dlg.ToHtmlColor()
dlg.FromHtmlColor "#abc" ' ignored
Debug.Print dlg.ToHtmlColor()
' Output:
' RGB(0, 255, 127)
' #FF8000
' #FF8000
GetCustomColor
Returns one of the 16 custom colors.
Syntax: object.GetCustomColor ( index ) As Long
- index
- required A Long from 0 to 15.
Returns the custom color as a Long in the layout of Color. An index outside 0 to 15 gives 0, which is black, without an error.
Dim dlg As New cDialogColor
Debug.Print Hex$(dlg.GetCustomColor(1))
Debug.Print Hex$(dlg.GetCustomColor(15))
Debug.Print dlg.GetCustomColor(16)
' Output:
' 800000
' FFFFFF
' 0
Reset
Restores every property and the 16 custom colors to their starting values.
Syntax: object.Reset
Color becomes white, FullOpen, PreventFullOpen and SolidColor False, AnyColor True, OwnerHwnd 0, and the custom colors the colors of QBColor(0) to QBColor(15).
Dim dlg As New cDialogColor
dlg.Color = vbRed
dlg.SetCustomColor 0, vbRed
dlg.Reset
Debug.Print Hex$(dlg.Color)
Debug.Print Hex$(dlg.GetCustomColor(0))
' Output:
' FFFFFF
' 0
SetCustomColor
Sets one of the 16 custom colors.
Syntax: object.SetCustomColor ( index, color )
- index
- required A Long from 0 to 15. Any other value is ignored, without an error.
- color
- required A Long in the layout of Color.
The next ShowDialog shows the color in the custom box of that number.
Dim dlg As New cDialogColor
dlg.SetCustomColor 3, RGB(17, 34, 51)
dlg.SetCustomColor 99, vbRed ' ignored
Debug.Print Hex$(dlg.GetCustomColor(3))
' Output:
' 332211
SetRgb
Sets the color from its three parts.
Syntax: object.SetRgb ( red, green, blue )
- red, green, blue
- required A Long for each part. A value outside 0 to 255 is clamped, as the properties Red, Green and Blue clamp it.
Dim dlg As New cDialogColor
dlg.SetRgb 255, 128, 0
Debug.Print dlg.ToHtmlColor()
dlg.SetRgb 300, -5, 7
Debug.Print dlg.ToRgbString()
' Output:
' #FF8000
' RGB(255, 0, 7)
ShowDialog
Shows the Color dialog.
Syntax: object.ShowDialog ( ) As Boolean
Returns True when the user chose OK, and then Color holds the chosen color. Returns False when the user cancelled, and Color is unchanged. See How the dialog is shown.
Dim dlg As New cDialogColor
dlg.Color = vbBlue
dlg.SetCustomColor 0, RGB(255, 128, 0)
If dlg.ShowDialog() Then
Debug.Print "Chosen: " & dlg.ToHtmlColor()
End If
ToHtmlColor
Returns the color as text of the form #RRGGBB.
Syntax: object.ToHtmlColor ( ) As String
The result has seven characters: a # and two upper-case hexadecimal digits each for red, green and blue.
Dim dlg As New cDialogColor
dlg.Color = RGB(160, 176, 192)
Debug.Print dlg.ToHtmlColor()
' Output:
' #A0B0C0
ToRgbString
Returns the color as text of the form RGB(r, g, b).
Syntax: object.ToRgbString ( ) As String
The three parts are decimal numbers, separated by a comma and a space.
Dim dlg As New cDialogColor
dlg.Color = RGB(160, 176, 192)
Debug.Print dlg.ToRgbString()
' Output:
' RGB(160, 176, 192)
See Also
- cDialogFont class – shows the Font dialog
- cDialogPrint class – shows the Print dialog
- cDialog class – shows the Open, Save As and Browse For Folder dialogs
- TBMANLIB_Dialog package – overview