ChooseColorW Function

The ChooseColorW function displays a system-defined color-selection dialog box that allows the user to choose a color. This function is the Unicode version of the ChooseColor function.

BOOL ChooseColorW(
    LPCHOOSECOLORW lpccw
);

Parameters

lpccw
A pointer to a CHOOSECOLORW structure that contains information used to initialize the dialog box and receive information about the user's selection.

Return Value

BOOL

  • If the user clicks the OK button, the return value is nonzero.
  • If the user clicks the Cancel button or an error occurs, the return value is zero. To get extended error information, call GetLastError.

Remarks

About the CHOOSECOLORW Structure

The CHOOSECOLORW structure contains information about the dialog box. Its members can be set before calling ChooseColorW to initialize the dialog box, and the function fills in members upon returning.

The rgbResult member of the CHOOSECOLORW structure receives the color chosen by the user.

The dialog box supports custom colors. The lpCustColors member points to an array of 16 COLORREF values that represent the custom colors currently selected by the user.

The Flags member can include the following values:

  • CC_FULLOPEN: Displays the dialog box with the expanded color palette.
  • CC_ANYCOLOR: Enables the user to choose any color.
  • CC_SOLIDCOLOR: Enables the user to choose only solid colors.
  • CC_PREVENTFULLOPEN: Prevents the user from opening the expanded color palette.

Example Usage

#include <windows.h>
#include <commdlg.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    COLORREF   rgbCurrent;
    COLORREF   CustColors[16]; // Array for custom colors
    CHOOSECOLORW cc;

    // Initialize CHOOSECOLORW structure
    cc.lStructSize    = sizeof(cc);
    cc.hwndOwner      = NULL;
    cc.hInstance      = NULL;
    cc.rgbResult      = rgbCurrent;     // Initial color (can be obtained from somewhere)
    cc.lpCustColors   = CustColors;
    cc.Flags          = CC_FULLOPEN | CC_RGBINIT; // Set flags
    cc.lCustData      = 0;
    cc.lpfnHook       = NULL;
    cc.lpTemplateName = NULL;
    cc.pvInitExtra    = NULL;
    cc.dwReserved     = 0;
    cc.iCustColors    = 16;

    if (ChooseColorW(&cc) == TRUE) {
        // User chose a color and clicked OK
        rgbCurrent = cc.rgbResult;
        // Use the selected color (rgbCurrent)
    } else {
        // User canceled or an error occurred
        // Handle error or cancellation
    }

    return 0;
}

Requirements

Header:
commdlg.h (include windows.h)
Library:
Comdlg32.lib