Declare Function ChooseColor Lib "comdlg32.dll" Alias "ChooseColorA" (pChoosecolor As CHOOSECOLORS) As Long
ChooseColor opens a standard Windows choose a color dialog box. All of the information needed to open the dialog box is stored in the various members of pChoosecolor. It will also receive the color the user chooses. You can save the list of custom colors in a byte-sized array converted to Unicode (to see how to do this in VB through the StrConv function, see the example). The return value will be 0 if either the user chooses Cancel or if there is an error. A nonzero value indicated success.
Example:
' ** Place this code in the (declarations) section of your code.**
Dim customcolors() As Byte ' dynamic (resizable) array
' ** Place this code in the Form_Load Sub or similar section. **
ReDim customcolors(0 To 16 * 4 - 1) As Byte 'resize the array
Dim i As Integer
For i = LBound(customcolors) To UBound(customcolors)
customcolors(i) = 0 ' sets all custom colors to black (RGB=0,0,0)
Next i
' ** Place this code wherever to use the dialog box. **
Dim cc As CHOOSECOLORS, x As Long
cc.hwndOwner = Form1.hWnd ' handle of form opening the box
cc.lpCustColors = StrConv(customcolors, vbUnicode) ' convert array
cc.flags = CC_ANYCOLOR ' allow any color to be chosen
cc.lStructSize = Len(cc) ' size of variable
x = ChooseColor(cc) ' open the dialog box
If x = 0 Then Exit Sub ' abort if error or Cancel
Form1.BackColor = cc.rgbResult ' set form background to color chosen
customcolors = StrConv(cc.lpCustColors, vbFromUnicode) ' save custom colors
Category: Common Dialog
Back to the index.