The MFC Class Library provides the Cwnd::onctlcolor function, which is called when the child window of the working frame is redrawn. Therefore, you can overload the response function of the wm_ctlcolor message. The prototype of this function:
afx_msg hbrush OnCtlColor (CDC *pdc,cwnd *pwnd,uint nCtlColor);
The parameter nctlcolor is used to specify the type of control, which can be:
. ctlcolor_btn Button controls
. ctlcolor_dlg dialog box
. Ctlcolor_ edit box
. ctlcolor_listbox List control
. ctlcolor_msgbox Message Control
. ctlcolor_scrollbar scroll bar control
. ctlcolor_static Static controls
[Program Implementation]
Suppose you already have a dialog box named My Project. You have a STATIC control with ID idc_static1.
Hbrush Cmydlg::onctlcolor (cdc* PDC, cwnd* pWnd, UINT nctlcolor) {hbrush HBR = Cdialog::onctlcolor (PDC, pWnd, nCtlColor);// Todo:change any attributes of the DC HEREif (nctlcolor==ctlcolor_static) {pdc-> SetTextColor (RGB (255,0,0));//Font Color PDC -SetBkColor (RGB (0, 0, 255)); Font background color}//Todo:return a different brush if the default is not Desiredreturn HBR;}
If you want to specify that a particular control can write:ID idc_static1
if (pwnd-> getdlgctrlid () ==idc_static1) {pdc-> SetTextColor (RGB (255,0,0)); Set Font color pdc-> setbkmode (TRANSPARENT); Set font background to transparent//Todo:return a different brush if the default is not Desiredreturn (hbrush):: Getstockobject (Black_brush);
//Set background color}elsereturn HBR;
Note
Black_brush: Black
White_brush: White
Gray_brush: Grey
Null_brush: Transparent
Hollow_brush : Transparent
MFC sets static text boxes, edit boxes, and other control backgrounds and font colors