MFC series MFC quick Set control text font, size, color, background

Source: Internet
Author: User

Take static text for example to share how to modify text font, size, color, background and other parameters. Other text, controls, etc. can be modified by reference.

1. Modify font, size

This is simple, first declare a member variable of type CFont in the Dlg class:

Then add the following two lines of code to the class's initialization function, OnInitDialog ():

1     // set the static text font size 2     M_editfont.createpointfont (_t (" song body ")); 3     M_static.setfont (&m_editfont);

2. Change edit box text color, background color

Right-click the dialog box, class-guided message, double-click Add wm_ctlcolor, you can see the 3 function OnCtlColor in the Dlg class.

1Hbrush Ctimerdlg::onctlcolor (cdc* PDC, cwnd*pWnd, UINT nCtlColor)2 3 {4 5Hbrush HBR =Cdialogex::onctlcolor (PDC, PWnd, nCtlColor);6 7  8 9     //TODO: Change any of the properties of the DC hereTen  One     if(Pwnd->getdlgctrlid () = = Idc_display)//Idc_display for the selected text box ID A  -     { -  thePdc->settextcolor (RGB (255,0,0));//Set Font Color -  -Pdc->setbkcolor (RGB (0,255,0));//Set Background color -  +         //Pdc->setbkmode (TRANSPARENT);//Set Background Transparency -  +     } A  at     //TODO: If the default is not the desired brush, another brush is returned -  -     returnHBR; -  -}

In addition to setting the color and background of a static control, the OnCtlColor function applies to several types:

Ctlcolor_btn Button controls

Ctlcolor_dlg dialog box

Ctlcolor_edit edit Box

Ctlcolor_listbox List control

Ctlcolor_msgbox Message Control

Ctlcolor_scrollbar scroll bar control

Ctlcolor_static Static controls

In addition to setting the property with the ID specified above, you can also specify a control type to set the property .

1Hbrush Ctimerdlg::onctlcolor (cdc* PDC, cwnd*pWnd, UINT nCtlColor)2 3 {4 5Hbrush HBR =Cdialogex::onctlcolor (PDC, PWnd, nCtlColor);6 7  8 9     //TODO: Change any of the properties of the DC hereTen  One     if(nCtlColor = =ctlcolor_static) A  -     { -  thePdc->settextcolor (RGB (0,255,0));//Set Font Color -  -Pdc->setbkcolor (RGB (255,0,0));//Set Background color -  +     } -  +     //TODO: If the default is not the desired brush, another brush is returned A  at     returnHBR; -  -}

End:)

MFC series MFC quick Set control text font, size, color, background

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.