VC uses the OnCtlColor function to change the control color (reference), vconctlcolor
In the MFC class libraryCWnd: OnCtlColorFunction. This member function is called when the child window of the working framework is re-painted.WM_CTLCOLORMessage response function. prototype of this function:
Afx_msg HBRUSH OnCtlColor (CDC * pDC, CWnd * pWnd, UINT nCtlColor );
ParametersNCtlColorUsed to specify the control type, which can be:
.CTLCOLOR_BTNButton Control
.CTLCOLOR_DLGDialog Box
.CTLCOLOR_EDITEdit box
.CTLCOLOR_LISTBOXList controls
.CTLCOLOR_MSGBOXMessage Control
.CTLCOLOR_SCROLLBARScroll bar Control
.CTLCOLOR_STATICStatic controls
[Program Implementation]
Suppose you already have a name namedMyDialog Box project. You haveSTATICControl, ID isIDC_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); // the 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 a specific control, you can write as follows:
ID: IDC_STATIC1
If (pWnd-> GetDlgCtrlID () = IDC_STATIC1) {pDC-> SetTextColor (RGB (255,0, 0); // set the font color pDC-> SetBkMode (TRANSPARENT ); // set the font background to transparent // TODO: Return a different brush if the default is not desiredreturn (HBRUSH): GetStockObject (BLACK_BRUSH); // set the background color} elsereturn hbr;
[Note]
BLACK_BRUSH:Black
WHITE_BRUSH:White
GRAY_BRUSH:Gray
NULL_BRUSH:Transparent
HOLLOW_BRUSH: Transparent