Home

Windows Controls: The MFC Color Dialog Box

 

Introduction to the MFC Color Dialog Box

  

Description

To provide a modern object for color selection, the MFC library now has a new color dialog box. The MFC color dialog box is a property sheet made of two property pages, three buttons, and a preview area:

 
The Color Dialog Box

 

The Standard property page allows a user to select a color by clicking one of the hexagonal colored shapes. To create or specify a color based on its red-green-blue components, the user can click the Custom tab:

The Color Dialog Box

In the Custom property page, the user has many options. To select a color, the user can click or browse the Colors panel. To specify the numeric values of the desired color, the user can use the spin buttons in the bottom section.

One of the options that the MFC colors dialog box has over its Win32 counterpart is the ability to select a color from anywhere on the screen. To proceed, the user can click the Select button, then move the mouse to the object or area that has the desired color, and click.

After selecting or specifying a color, the user can click OK or press Enter. To dismiss the dialog box regardless of what was done on it, the user can click Cancel or press Esc.

Creating an MFC Colors Dialog Box

To support the MFC colors dialog box, the MFC library provides a class named CMFCColorDialog. Therefore, to create colors dialog box, declare a variable of this class. This can be done as follows:

void CExerciseDlg::OnBnClickedColors()
{
    // TODO: Add your control notification handler code here
    CMFCColorDialog dlgColors;
}
 
 
 

Characteristics of the MFC Colors Dialog Box

 

Introduction

The CMFCColorDialog class is derived from CDialogEx. Based on this, to present the dialog box to the user, call its DoModal() member function. Here is an example:

void CExerciseDlg::OnBnClickedColors()
{
    // TODO: Add your control notification handler code here
    CMFCColorDialog dlgColors;

    dlgColors.DoModal();
}

After using the dialog box, if the user clicks OK, the DoModal() member function returns IDOK. In this case, you can get the values from the dialog box and use it as you see fit.

The Initial Color of the Dialog Box

Before displaying the colors dialog box, you can specify what primary color the dialog box should select. To assist you with this, the CMFCColorDialog class has the following construction:

CMFCColorDialog(
   COLORREF clrInit=0,
   DWORD dwFlags=0,
   CWnd* pParentWnd=NULL,
   HPALETTE hPal=NULL 
);

As you can see, all arguments are optional. The clrInit argument allows you to specify the initial color that the dialog box would select. Here is an example:

void CExerciseDlg::OnBnClickedColors()
{
    // TODO: Add your control notification handler code here
    CMFCColorDialog dlgColors(268405);

    dlgColors.DoModal();
}

The Current Color of the Dialog Box

Besides the constructor, to let you set the initial or current color of the dialog box, the CMFCColorDialog class is equipped with a member function named SetCurrentColor. Its syntax is:

void SetCurrentColor(COLORREF rgb);

Here is an example of calling it:

void CExerciseDlg::OnBnClickedColors()
{
    // TODO: Add your control notification handler code here
    CMFCColorDialog dlgColors;;

    dlgColors.SetCurrentColor(702416);

    dlgColors.DoModal();
}

Getting the Color of the Dialog Box

After using the dialog box, the user can click OK, in which case DoModal() would return IDOK. In this case, to let you identify the color the user would have selected, the CMFCColorDialog class provides the Color() member function. Its syntax is:

COLORREF GetColor() const;

Here is an example of calling that member function and using the color the user selected:

void CExerciseDlg::OnBnClickedColors()
{
    // TODO: Add your control notification handler code here
    CMFCColorDialog dlgColors;

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

Home Copyright © 2010-2016, FunctionX