In MFC, the static text control can easily display text, but what if you want to change the font style size and color?
1. To modify the font, you need to use the CFont class, which can modify various properties of the font
1 CFont M_font; 2 M_font. CreateFont (0,0,0, Fw_bold,false,false,false,3 Ansi_charset,out_default_precis,clip_default_precis, 4 Default_quality,default_pitch | ff_swiss,_t (" song body ")); ///width of 14, bold italicized word characters
2. Overload the response function of the WM_CTLCOLOR message, the framework calls this function when it is about to draw the child control, and we can add code to the response function of the message to draw our control
1 if (Pwnd->getdlgctrlid () = = idc_static) 2 3 pdc->selectobject (&m_font); Select Font 4 }
3. Change the color, also in the OnCtlColor function
1 if (Pwnd->getdlgctrlid () = = idc_static) 2 {3 pdc->settextcolor (RGB (25500)); Change colors with RGB macros
4 }
4. Final code
1Hbrush Cxxdlg::onctlcolor (cdc* PDC, cwnd*pWnd, UINT nCtlColor)2 {3Hbrush HBR =Cdialogex::onctlcolor (PDC, PWnd, nCtlColor);4 5 //TODO: Change any of the properties of the DC here6 if(Pwnd->getdlgctrlid () = =idc_static)7 { 8Pdc->settextcolor (RGB (255,0,0));//change colors with RGB macros9Pdc->selectobject (&M_font);Ten } One //TODO: If the default is not the desired brush, another brush is returned 1 A returnHBR; -}
5.
MFC static text boxes set fonts and colors