MFC sets static text boxes, edit boxes, and other control backgrounds and font colors

Source: Internet
Author: User
Tags set background

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

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.