MFC static text boxes set fonts and colors

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.