MFC Changes dialog box background color

Source: Internet
Author: User

Original link: http://blog.sina.com.cn/s/blog_59955afc0100spjz.html

method One: Call the member function of the CWinApp class SetDialogBkColor to implement.

----where the first parameter of the function specifies the background color, the second parameter specifies the text color.
The following example is to set the Application dialog box to a blue background and red text, in the following steps:

----① Create a new dialog-based MFC AppWizard application Exampledlg.

----② Add the following code in Cexampledlgapp:: InitInstance ():

BOOL Cexampledlgapp:: InitInstance ()
{
... ..
Cexampledlgdlg dlg;
m_pMainWnd = &dlg;

///Before DoModal (), Set the dialog box to blue background, red text
SetDialogBkColor (RGB (0,0,255), RGB (255,0,0));
int nresponse = dlg. DoModal ();
... ..
}

----compiled and run, the background color and text color of the dialog box have changed. It is worth noting that
Yes: You must call SetDialogBkColor before calling DoModal (), and this method will change
all dialog boxes in the application are colored and cannot be specified for a specific dialog box.
 
method Two: Overload OnPaint (), which is the WM_PAINT message. The code is as follows (the above example works):

void Cexampledlgdlg::onpaint ()
{
if (Isiconic ())
... ..
Else
{
CRect rect;
CPAINTDC DC (this);
GetClientRect (rect);
DC. Fillsolidrect (Rect,rgb (0,255,0)); Set to green background

cdialog::onpaint ();
}


method Three: Overload OnCtlColor (cdc* PDC, cwnd* pWnd, UINT nctlcolor),
that is, the WM_CTLCOLOR message. The specific steps are as follows (the above example works):
----① in Cexampledlgdlg's header file, add a cbrush member variable:

class Cexampledlgdlg:public CDialog
{
...
protected:
CBrush M_brush;
...
};


----② Add the following code in the OnInitDialog () function:
BOOL Cexampledlgdlg::oninitdialog ()
{
...
//Todo:add extra initialization here
M_brush. CreateSolidBrush (RGB (0, 255, 0)); Generate a green brush
...


----③ Use ClassWizard to overload the OnCtlColor (...), which is the WM_CTLCOLOR message:
Hbrush Cexampledlgdlg::onctlcolor
(cdc* PDC, cwnd* pWnd, UINT nCtlColor)
{
/*
* * Don't write any code here!
* * The downlink code is commented out
* * Hbrush HBR = Cdialog::onctlcolor (PDC, PWnd, nCtlColor);
*/

return M_brush;//Add Green Brush
}


method Four: or overload OnCtlColor (cdc* PDC, cwnd* pWnd, UINT nctlcolor),
that is, the WM_CTLCOLOR message. The specific steps are as follows (the above example works):
----Steps ①, ② the steps in the same method three ①, ②.

----Step ③ using ClassWizard overload onctlcolor (...) (That is, wm_ctlcolor messages), there is
Some differences:

Hbrush Cexampledlgdlg::onctlcolor
(cdc* PDC, cwnd* pWnd, UINT nCtlColor)
{
hbrush HBR = Cdialog::onctlcolor (PDC, PWnd, nCtlColor);

//In this add-on is a dialog box is a judgment statement
if (nCtlColor ==ctlcolor_dlg)
return M_brush;//Add Green Brush
return HBR;
}

MFC Changes dialog box background color

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.