An application can display the Color dialog box so that the user can create and select a custom color. This section describes how you can provide code and structures in your application that make this possible.
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 custom color control
If your application does not customize the dialog box and you want the user to be able to create and select custom colors, you should initialize the CHOOSECOLOR structure in the following manner:
/* Color Variables */
CHOOSECOLOR chsclr;
DWORD dwCustClrs[16] = { RGB(255, 255, 255), RGB(239, 239, 239),
RGB(223, 223, 223), RGB(207, 207, 207),
RGB(191, 191, 191), RGB(175, 175, 175),
RGB(159, 159, 159), RGB(143, 143, 143),
RGB(127, 127, 127), RGB(111, 111, 111),
RGB(95, 95, 95), RGB(79, 79, 79),
RGB(63, 63, 63), RGB(47, 47, 47),
RGB(31, 31, 31), RGB(15, 15, 15)
};
BOOL fSetColor = FALSE;
int i;
chsclr.lStructSize = sizeof (CHOOSECOLOR);
chsclr.hwndOwner = hwnd;
chsclr.hInstance = NULL;
chsclr.rgbResult = 0L;
chsclr.lpCustColors = (LPDWORD) dwCustClrs;
chsclr.Flags = CC_FULLOPEN;
chsclr.lCustData = 0L;
chsclr.lpfnHook = (FARPROC) NULL;
chsclr.lpTemplateName = (LPSTR)NULL;
In the previous example, the array to which lpCustColors points contains sixteen 32-bit RGB values that specify 16 scales of gray, and the CC_FULLOPEN flag is set in the Flags member to display the complete Color dialog box.
After you initialize the structure, you should call the ChooseColor function as shown in the following code fragment:
if (fSetColor = ChooseColor(&chsclr))
.
. /* Use chsclr.lpCustColors to select user specified colors*/
.
If the function is successful and the user chooses the OK button to close the dialog box, the lpCustColors member points to an array that contains the RGB values for the custom colors requested by the application's user.
Applications can exercise more control over custom colors by creating a new
message identifier for the string defined by the COLOROKSTRING constant. The
application creates the new message identifier by calling the RegisterWindowMessage function and passing this constant as the single parameter. After calling RegisterWindowMessage, the application receives a message immediately prior to the dismissal of the dialog box. The lParam parameter of this message contains a pointer to the CHOOSECOLOR structure. The application can use the lpCustColors member of this structure to check the current color. If the application returns a nonzero value when it processes this message, the dialog box is not dismissed.
Similarly, applications can create a new message identifier for the string defined by the SETRGBSTRING constant. The application's hook function can use the message identifier returned by calling RegisterWindowMessage with the SETRGBSTRING constant to set a color in the dialog box. For example, the following line of code sets the color selection to blue:
SendMessage
(hwhndDlg, wSetRGBMsg, 0, (LPARAM) RGB(0, 0, 255));
In this example, wSetRGBMsg is the message identifier returned by the RegisterWindowMessage function. The lParam parameter of the SendMessage function is set to the RGB values of the desired color. The wParam parameter is not used.
The application can specify any valid RGB values in this call to SendMessage. If the RGB values match one of the basic colors, the system selects the basic color and updates the spectrum and luminosity controls. If the RGB values do not match one of the basic colors, the system updates the spectrum and luminosity controls, but the basic color selection remains unchanged.
Note that if the Color dialog box is not fully open and the application sends RGB values that do not match one of the basic colors, the system does not update the dialog box. Updates are unnecessary because the spectrum and luminosity controls are not visible when the dialog box is only partially open.
For more information about processing registered window messages, see Section 4.5, “Using Find and Replace Dialog Boxes.”