(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"