Declare Function ChooseColor Lib "comdlg32.dll" Alias "ChooseColorA" (lpcc As CHOOSECOLOR_TYPE) As Long
ChooseColor opens a Choose Color common dialog box. All the information needed to create the dialog box, as well as the data returned from it, is stored in the structure passed as lpcc.
If an error occured or the user pressed the Cancel button, the function returns 0 (use CommDlgExtendedError to get the error code). If the user successfully selected a color, the function returns a non-zero value.
None.
' This code is licensed according to the terms and conditions listed here.
' Display a Choose Color common dialog box. The background
' color of Form1 will be set to the color the user selects. Although the entire
' list of custom colors is initialized to black, this example stores the
' colors into an array which can be used again to save the user's custom
' color selections.
Dim cc As CHOOSECOLOR_TYPE ' structure to pass data
Dim hMem As Long ' handle to the memory block to store the custom color list
Dim pMem As Long ' pointer to the memory block to store the custom color list
Dim custcols(0 To 15) As Long ' holds list of the 16 custom colors
Dim c As Integer ' counter variable
Dim retval As Long ' return value
' Initialize the list of custom colors to black.
For c = 0 To 15 ' loop through each element
custcols(c) = 0 ' set each element to RGB color 0 (black)
Next c
' Create a memory block and get a pointer to it.
hMem = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, 64) ' allocate sufficient memory block
pMem = GlobalLock(hMem) ' get a pointer to the block
' Copy the data inside the array into the memory block.
CopyMemory ByVal pMem, custcols(0), 64 ' 16 elements * 4 bytes
' Store the initial settings of the Choose Color box.
cc.lStructSize = Len(cc) ' size of the structure
cc.hwndOwner = Form1.hWnd ' Form1 is opening the Choose Color box
cc.hInstance = 0 ' not needed
cc.rgbResult = Form1.BackColor ' set default selected color to Form1's background color
cc.lpCustColors = pMem ' pointer to list of custom colors
cc.Flags = CC_ANYCOLOR Or CC_RGBINIT ' allow any color, use rgbResult as default selection
cc.lCustData = 0 ' not needed
cc.lpfnHook = 0 ' not needed
cc.lpTemplateName = "" ' not needed
' Open the Choose Color box. If the user chooses a color, set Form1's
' background color to that color.
retval = ChooseColor(cc)
If retval <> 0 Then ' success
' Copy the possibly altered contents of the custom color list
' back into the array.
CopyMemory custcols(0), ByVal pMem, 64
' Set Form1's background color.
Form1.BackColor = cc.rgbResult
End If
' Deallocate the memory blocks to free up resources.
retval = GlobalUnlock(hMem)
retval = GlobalFree(pMem)
Go back to the alphabetical Function listing.
Go back to the Reference section index.
Last Modified: August 14, 1999
This page is copyright © 1999 Paul Kuliniewicz.
Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/c/choosecolor.html