Introduction to MFC Programming 19 (Dialog box: Color dialog box)

Source: Internet
Author: User
Tags getcolor

In the previous section to explain the use of the Font dialog box, familiar with the Font dialog box, this section continues to talk about another common dialog-the Color dialog box.

Color dialog box Everyone is certainly not unfamiliar, we can open it to select the desired color, simply said, it is used to choose the color. MFC provides all the actions of the CColorDialog class that encapsulate the Color dialog box, where we can display the Color dialog box and get the color selected in the Color dialog box. The color dialog box, like the Font dialog box, is also a modal dialog box.

  CColorDialog Constructors for class

CColorDialog (

COLORREF clrinit = 0,

DWORD dwFlags = 0,

cwnd* pParentWnd = NULL

);

Parameter description:

Clrinit: The color value of the default selection color, type COLORREF, is actually the unsigned long type. If its value is not set, the default value is RGB (0,0,0), which is black.

Note: RGB (R, G, b) is a macro and can be used to calculate color values. The three values in parentheses are the values of the red, green, and blue components, respectively.

DwFlags: A property value that customizes the function and appearance of the color dialog box. Details can be found in MSDN.

pParentWnd: Pointer to the parent window of the Color dialog box.

  Gets the color value selected in the Color dialog box

The ultimate goal of using the Color dialog box is to get the color value selected in the Color dialog box. The member function GetColor () for this ccolordialog class is good enough to fulfill our requirements. The prototype of the GetColor () function is:

COLORREF GetColor () const;

It returns the COLORREF value of the selected color.

What if we want to get the values of the components of R, G, and B? The COLORREF color values that can be obtained from GetColor are obtained by using Getrvalue, Getgvalue, and getbvalue three macros. The grammatical form of getrvalue is:

BYTE Getrvalue (DWORD RGB);

The parameter RGB is the COLORREF color value, and the return value is the R component value. The other two macros are in a similar form. For example, the GetColor () function returns a COLORREF of 10000, and the R component value is Getrvalue (10000).

  Color dialog Box Application instance

Here is a small example of a color dialog box for you. This example provides a simple introduction to the following: Generates a dialog box with a color selection button, four static text boxes, and four edit boxes. Four static text boxes display color:, R:, G:, B:, followed by an edit box for each static text box to display the color value selected in the Color dialog box and the red component, green component, and blue component of the selected color value.

Here are the steps for instance creation:

1, create a dialog-based MFC project, named "Example19".

2. In the Idd_example19_dialog template of the auto-generated main dialog box, delete the Todo:place dialog controls here. Static text box, add a button, the ID is set to idc_color_button,caption set to "color selection" to display the Color dialog box to select a color. Add four more static text boxes, with IDs idc_color_static, Idc_r_static, idc_g_static, and idc_b_static,caption respectively set to "COLOR:", "R:", "G:" and "B:", Then add an edit box after each static text box, and the IDs of the four edit boxes are Idc_color_edit, Idc_r_edit, Idc_g_edit, and Idc_b_edit, respectively, to display the color values selected in the Color dialog box and the red component values of the selected color, The green component value and the Blue component value.

3. Add the message handler function Cexample19dlg::onbnclickedcolorbutton () for the button Idc_color_button click Message.

4. Modify the message handler function Cexample19dlg::onbnclickedcolorbutton () function as follows:

C + + code:

5, the final compilation run the program, in the Results dialog box click on the "Color Selection" button, pop-up color dialog box. In the initial state, the selection box is on red, we choose another color, at this time the Color dialog box is as follows:

Click the OK button, and the four edit boxes on the dialog show the selected color value, r component, G component, and B component, respectively:

In the actual development, we can use the obtained color value to set the color of other objects, it is very convenient.

Introduction to MFC Programming 19 (Dialog box: Color dialog box)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.