The first method (it should be the best way, you can modify the color of a single control, maybe other types of controls can also be used ):
First, we add the onctlcolor message ing function in the dialog box. For function functions, function prototypes, and parameter explanations, see the msdn description ~
Quote:
Cwnd: onctlcolor See also
Cwnd overview | class members | hierarchy chart | CDC: setbkcolor
The framework callthis member function when a child control is about to be drawn.
Afx_msg hbrush onctlcolor (
CDC * PDC,
Cwnd * pwnd,
Uint nctlcolor
);
Parameters
PDC
Contains a pointer to the display context for the Child Window. may be temporary.
Pwnd
Contains a pointer to the Control asking for the color. may be temporary.
Nctlcolor
Contains one of the following values, specifying the type of control:
Ctlcolor_btn button control
Ctlcolor_dlg dialog box
Ctlcolor_edit Edit Control
Ctlcolor_listbox list-box Control
Ctlcolor_msgbox message box
Ctlcolor_scrollbar scroll-bar Control
Ctlcolor_static Static Control
Return Value
Onctlcolor must return a handle to the brush that is to be used for painting the control background.
Then, we place two static controls in the dialog box, with IDs: idc_stccolor and idc_stctwo. Then add member variables to the class in the dialog box (don't tell me you don't know how to add ......), Two variables are required. Each member variable corresponds to one control ~
PS: select the control variable for the variable type ~ The type of the required control variable is generally cstatic ~
Then we write the following code in the message ing function just now:
Quote:
Hbrush cmfcdialogdlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
Hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor );
If (getdlgitem (idc_stccolor) = pwnd)
{
PDC-> settextcolor (# ff0000 );
}
Else if (getdlgitem (idc_stctwo) = pwnd)
{
PDC-> settextcolor (# 0000ff );
}
Return HBr;
}
The reason for judging the handle is that we need to set a control. If you directly use PDC-> settextcolor to set the color, MFC traverses all controls of the form and sets the color to the same.
As to why you want to add control member variables, we can find this code in the CPP file of the dialog box:
Quote:
Void cmfcdialogdlg: dodataexchange (cdataexchange * PDX)
{
Cdialog: dodataexchange (PDX );
Ddx_control (PDX, idc_stccolor, m_cstccolor );
Ddx_control (PDX, idc_stctwo, m_cstctwo );
}
Method 2:
In actual application, you can use the wm_ctlcolor message to change the color of the control in the MFC. For example, you can change the color of a static text
Background Color and font
1. Add two variables to the class in the dialog box:
Cbrush m_brush;
Cfont m_font;
Add the following to the oninitdialog () function:
// Todo: add additional initialization code here
M_font.createpointfont (150, "文 ");
M_brush.createsolidbrush (#00ff00 );
2. Add the wm_ctlcolor message response:
Declare in the dialog box class: afx_msg hbrush onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor );
Add on_wm_ctlcolor () to message ing ()
For example:
Begin_message_map (ctestenvdlg, cdialog)
On_wm_ctlcolor ()
//} Afx_msg_map
End_message_map ()
3. Add a response function:
Hbrush cyourdlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
Hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor );
If (m_yourstatic.m_hwnd = pwnd-> m_hwnd)
{
PDC-> setbkcolor (#00ff00 );
PDC-> SelectObject (& m_font );
Return m_brush;
}
Return HBr;
}
In this way, the color and font of static text can be changed.