4.1.2 Using the Color Dialog Box to Display Basic Colors

An application can display the Color dialog box so that a user can select one color from a list of basic screen colors. This section describes how you can provide code and structures in your application that make this possible.

4.1.2.1 Initializing the CHOOSECOLOR Structure

Before you display the Color dialog box you need to initialize a CHOOSECOLOR structure. This structure should be global or declared as a static vari-able. The members of this structure contain information about such items as the following:

Structure size

Which window owns the dialog box

Whether the application is customizing the common dialog box

The hook function and custom dialog box template to use for a customized version of the Color dialog box

RGB values for the selected basic color

If your application does not customize the dialog box and you want the user to be able to select a single color from the basic colors, you should initialize the CHOOSECOLOR structure in the following manner:

/* 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. */
    .

In the previous example, the array to which the lpCustColors member points contains 16 doubleword RGB values that specify the color white, and the CC_PREVENTFULLOPEN flag is set in the Flags member to disable the Define Custom Colors button and prevent the user from selecting a custom color.

4.1.2.2 Calling the ChooseColor Function

After you initialize the structure, you should call the ChooseColor function. If the function is successful and the user chooses the OK button to close the dialog box, the rgbResult member contains the RGB values for the basic color that the user selected.