VC uses the OnCtlColor function to change the control color (reference), vconctlcolor

Source: Internet
Author: User

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

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.