Use the wm_ctlcolor message to change the text and background color of the Edit Control.

Source: Internet
Author: User

(Abbey was published at 1:48:45)

First, you must understand that wm_ctlcolor is a notification message sent to its parent window by control ).

Steps:
Generate a standard single document applicationProgramFramework. Assume that the Application name is color. I will use its about dialog box for demonstration. Add two edit controls to the about dialog file and set the ID to idc_edit1 and idc_edit2.

Method 1 (corresponding to idc_edit1): according to standard windows programming, the message processing function of its parent window is responsible for processing wm_ctlcolor messages.

1. Add a data member in caboutdlg: hbrush m_brmine;
2. Map the wm_ctlcolor message of aboutdlg using the Wizard to generate the function: hbrush caboutdlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor );
PDC is the device context of aboutdlg, pwnd is the control pointer for sending the message in aboutdlg, And the type encoding of control in nctlcolor. Modify it as follows:

Hbrush caboutdlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
If (pwnd-> getdlgctrlid () = idc_edit1) & (nctlcolor = ctlcolor_edit ))
{
Colorref CLR = RGB (255, 0, 0 );
PDC-> settextcolor (CLR); // sets the red text.
CLR = RGB (0, 0 );
PDC-> setbkcolor (CLR); // sets the black background.
M_brmine =: createsolidbrush (CLR );
Return m_brmine; // return the brush handle corresponding to the background color.
}
Else
{
Hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor );
Return HBr;
}
}

Method 2 (corresponding to idc_edit2 ):
Use Message reflection, a new feature of MFC 4.0.

1. Use the Wizard to add a new class: ccoloredit. The base class is cedit;
2. Add a data member hbrush m_bkbrush in ccoloredit;
3. Use the Wizard to map the "= wm_ctlcolor" message of ccoloredit to generate the function:

Hbrush ccoloredit: ctlcolor (CDC * PDC, uint nctlcolor );

Modify it as follows:

Hbrush ccoloredit: ctlcolor (CDC * PDC, uint nctlcolor)
{
Colorref CLR = RGB (0, 0 );
PDC-> settextcolor (CLR); // sets the black text.
CLR = RGB (255, 0, 0 );
PDC-> setbkcolor (CLR); // sets the red background.
M_bkbrush =: createsolidbrush (CLR );
Return m_bkbrush; // return the brush handle corresponding to the background color.
}

4. Use the Wizard to generate a data member ccoloredit m_coloredit for idc_edit2;
5. Add: # include "coloredit. H" to the color. cpp file that defines caboutdlg"

Related Article

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.