ChooseColor

3.1

  #include <commdlg.h>    

  BOOL ChooseColor(lpcc)    
  CHOOSECOLOR FAR* lpcc; /* address of structure with initialization data */

The ChooseColor function creates a system-defined dialog box from which the user can select a color.

Parameters

lpcc

Points to a CHOOSECOLOR structure that initially contains information necessary to initialize the dialog box. When the ChooseColor function returns, this structure contains information about the user's color selection. The CHOOSECOLOR structure has the following form:

#include <commdlg.h>

typedef struct tagCHOOSECOLOR {    /* cc */
    DWORD   lStructSize;
    HWND    hwndOwner;
    HWND    hInstance;
    COLORREF rgbResult;
    COLORREF FAR* lpCustColors;
    DWORD   Flags;
    LPARAM  lCustData;
    UINT    (CALLBACK* lpfnHook)(HWND, UINT, WPARAM, LPARAM);
    LPCSTR  lpTemplateName;
} CHOOSECOLOR;

For a full description of this structure, see the Microsoft Windows Programmer's Reference, Volume 3.

Return Value

The return value is nonzero if the function is successful. It is zero if an error occurs, if the user chooses the Cancel button, or if the user chooses the Close command on the System menu (often called the Control menu) to close the dialog box.

Errors

Use the CommDlgExtendedError function to retrieve the error value, which may be one of the following:

CDERR_FINDRESFAILURE
CDERR_INITIALIZATION
CDERR_LOCKRESFAILURE
CDERR_LOADRESFAILURE
CDERR_LOADSTRFAILURE
CDERR_MEMALLOCFAILURE
CDERR_MEMLOCKFAILURE
CDERR_NOHINSTANCE
CDERR_NOHOOK
CDERR_NOTEMPLATE
CDERR_STRUCTSIZE

Comments

The dialog box does not support color palettes. The color choices offered by the dialog box are limited to the system colors and dithered versions of those colors.

If the hook function (to which the lpfnHook member of the CHOOSECOLOR structure points) processes the WM_CTLCOLOR message, this function must return a handle for the brush that should be used to paint the control background.

Example

The following example initializes a CHOOSECOLOR structure and then creates a color-selection dialog box:

/* Color variables */

CHOOSECOLOR cc;
COLORREF clr;
COLORREF aclrCust[16];
int i;

/* Set the custom-color controls to white. */

for (i = 0; i < 16; i++)
    aclrCust[i] = RGB(255, 255, 255);

/* Initialize clr to black. */

clr = RGB(0, 0, 0);

/* Set all structure fields to zero. */

memset(&cc, 0, sizeof(CHOOSECOLOR));

/* Initialize the necessary CHOOSECOLOR members. */

cc.lStructSize = sizeof(CHOOSECOLOR);
cc.hwndOwner = hwnd;
cc.rgbResult = clr;
cc.lpCustColors = aclrCust;
cc.Flags = CC_PREVENTFULLOPEN;


if (ChooseColor(&cc))
    .
    . /* Use cc.rgbResult to select the user-requested color. */
    .