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