How to change the background color of a dialog or form window

Source: Internet
Author: User

Call CWinApp: SetDialogBkColor to change the background color of all applications. The first parameter specifies the background color, and the second parameter specifies the text color. In the following example, set the application dialog to blue background and yellow text.

BOOL CSampleApp: InitInstance ()

{

...

// Use blue dialog with yellow text.

SetDialogBkColor (RGB (0, 0,255), RGB (255,255, 0 ));

...

}

When you need to re-draw a dialog (or a sub-control of the dialog), Windows sends the message WM_CTLCOLOR to the dialog. Generally, you can ask Windows to select a paint brush for the background or reset the specified brush for the message. The following example describes how to create a red background dialog.

First, add the one-person member variable CBursh to the dialog base class:

Class CMyFormView: public CFormView

{

...

Private:

CBrush m _ brush; // background brush

...

};

Second, initialize the brush to the desired background color in the class constructor.

CMyFormView: CMyFormView ()

{

// Initialize background brush.

M_brush. CreateSolidBrush (RGB (0, 0,255 ))

}

Finally, use ClassWizard to process the WM_CTLCOLOR message and return a brush handle used to paint the conversation background. Note: The nCtlColor parameter must be checked because this function is also called when you redraw the dialog control.

HBRUSH CMyFormView: OnCtlColor (CDC * pDC, CWnd * pWnd, UINT nCtlColor)

{

// Determine if drawing a dialog box. If we are, return + handle

// Our own background brush. Otherwise let windows handle it.

If (nCtlColor = CTLCOLOR _ DLG)

Return (HBRUSH) m_brush. GetSafeHandle ();

Return CFormView: OnCtlColor (pDC, pWnd, nCtlColor );

}

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.