Home

Windows Controls: The Color Dialog Box

  

Introduction to the Color Dialog Box

 

Description

To provide the selection of colors on Microsoft Windows applications, the operating system provides a common dialog box appropriate for such tasks. The Color dialog box is used by various reasons to let the user set or change a color of an object such as the background color of a control or the color used to paint an object.

When it displays, by default, the dialog box appears as follows:

The Color Dialog Box

This displays a constant list of colors to the user. If none of the available colors is appropriate for the task at hand, the user can click the Define Custom Colors button to expand the dialog box:

The Color Dialog Box

The expanded Color dialog box allows the user to either select one of the preset colors or to custom create a color by specifying its red, green, and blue values.

The user can change the color in four different areas. The top left section displays a list of 48 predefined colors. If the desired color is not in that section, the user can click and drag the mouse in the multi-colored palette. The user can also drag the right bar that displays a range based on the color of the palette; the user can scroll up and down by dragging the arrow. For more precision, the user can type the Red, Green and Blue values. Each uses a integral value that ranges from 1 to 255.

Creating a Color Dialog Box

To support the color dialog box, the Win32 library provides a structure named CHOOSECOLOR and defined as follows:

typedef struct {
  DWORD        lStructSize;
  HWND         hwndOwner;
  HWND         hInstance;
  COLORREF     rgbResult;
  COLORREF     *lpCustColors;
  DWORD        Flags;
  LPARAM       lCustData;
  LPCCHOOKPROC lpfnHook;
  LPCTSTR      lpTemplateName;
} CHOOSECOLOR, *LPCHOOSECOLOR;

To build a color dialog box, you must declare a variable of this structure and appropriately initialize it. To actually display the dialog box, you must call the ChooseColor() function. Its syntax is:

BOOL WINAPI ChooseColor(__inout  LPCHOOSECOLOR lpcc);

To support the color dialog box, the MFC library provides the CColorDialog class that is derived from a class named CCommonDialog. The CCommonDialog class is derived from CDialog.

To get a color dialog box, declare a variable of type CColorDialog. To display the dialog box, call the DoModal() member function the class inherits from CDialog. Here is an example:

void CExerciseDlg::OnBnClickedColorBtn()
{
    // TODO: Add your control notification handler code here
    CColorDialog dlg;

    dlg.DoModal();
}

Characteristics of the Color Dialog Box

 

Introduction to Using the Color Dialog Box

The CColorDialog class has one constructor whose syntax is:

CColorDialog(
   COLORREF clrInit = 0,
   DWORD dwFlags = 0,
   CWnd* pParentWnd = NULL 
);

As mentioned already, to display a color dialog box, you can call the CColorDialog::DoModal() member function. After a person has used the dialog box, if he or she clicks OK or presses Enter, the dialog box returns the IDOK value. If the user clicks Cancel or presses Esc, the DoModal() member function returns IDCANCEL.

 
 
 

The Win32 Dialog Box

To represent the Win32's color dialog box, the CColorDialog class is equipped with a member variable named m_cc:

CHOOSECOLOR m_cc;

This member variable allows you to manipulate the characteristics of a color dialog box using the members of the CHOOSECOLOR structure.

The Color

The most important and most obvious property of the color dialog box is its color. When the dialog box displays, you may want it to show a certain color as a suggestion or it can be a color you specify for any reason. To specify the default color, when declaring the CColorDialog box, pass the first argument to the constructor. Here is an example:

void CExerciseDlg::OnBnClickedColorBtn()
{
    // TODO: Add your control notification handler code here
    CColorDialog dlg(RGB(25, 125, 155));
}

After using the dialog box, you may want to get the color the user selected. To provide this information, the CColorDialog class is equipped with the GetColor() member function. Its syntax is:

COLORREF GetColor()const;

Here is an example of calling this member function:

void CExerciseDlg::OnBnClickedColorBtn()
{
	// TODO: Add your control notification handler code here
	CColorDialog dlg;

	if( dlg.DoModal() == IDOK )
		SetBackgroundColor(dlg.GetColor());
}

The Size of the Dialog Box

You can control the regular or full size of the dialog box. To specify the default size when the color dialog box comes up, pass the second argument. For example, to display the dialog box in its full size, pass the second argument with a CC_FULLOPEN value. Here is an example:

void CExerciseDlg::OnBnClickedColorBtn()
{
    // TODO: Add your control notification handler code here
    CColorDialog dlg(208468, CC_FULLOPEN);

    if( dlg.DoModal() == IDOK )
	SetBackgroundColor(dlg.GetColor());
}

Custom Colors

If you want to supply the user with a set of colors of your choice, you can do this using a list of custom colors. To support this, the CHOOSECOLOR structure is equipped with a member named lpCustColors. The COLORREF::lpCustColors member is an array of 16 COLORREF values. After creating that array, assign it to this member variable. Here is an example:

void CExerciseDlg::OnBnClickedColorBtn()
{
	// TODO: Add your control notification handler code here
	CColorDialog dlg;
	
	COLORREF cRef[] = { RGB(0,     5,   5),
		            RGB(0,    15,  55),
			    RGB(0,    25, 155),
			    RGB(0,    35, 255),
		            RGB(10,    0,   5),
			    RGB(10,   20,  55),
			    RGB(10,   40, 155),
		            RGB(10,   60, 255),
			    RGB(100,   5,   5),
			    RGB(100,  25,  55),
		            RGB(100,  50, 155),
			    RGB(100, 125, 255),
			    RGB(200, 120,   5),
		            RGB(200, 150,  55),
			    RGB(200, 200, 155),
			    RGB(200, 250, 255) };
	dlg.m_cc.lpCustColors = cRef;
	dlg.m_cc.Flags |= CC_FULLOPEN;

	if( dlg.DoModal() == IDOK )
		SetBackgroundColor(dlg.GetColor());
}
The Color Dialog Box
 
 
   
 

Home Copyright © 2010-2016, FunctionX