Vc6.0How to change the background color of a dialog box
Method 1: Call the member function setdialogbkcolor of the cwinapp class.
The first parameter of the function specifies the background color, and the second parameter specifies the text color. The following example shows how to set the application dialog box to blue background and red text:
① Create a dialog-based MFC Appwizard application exampledlg.
② Add the following code to cexampledlgapp: initinstance:
Bool cexampledlgapp: initinstance ()
{
...
Cexampledlgdlg DLG;
M_pmainwnd = & DLG;
// Call domodal () to set the dialog box to blue background and red text
Setdialogbkcolor (RGB (255,), RGB (, 0 ));
Int nresponse = DLG. domodal ();
...
}
Compile and run. The background color and text color of the dialog box have changed. It is worth noting that setdialogbkcolor must be called before domodal () is called, and this method will change the color of all the dialogs in the application and cannot target a specified dialog box.
Method 2: Reload onpaint (), that is, the message wm_paint. The Code is as follows (the above example project prevails ):
Void cexampledlgdlg: onpaint ()
{
If (isiconic ())
...
Else
{
Crect rect;
Cpaintdc DC (this );
Getclientrect (rect );
DC. fillsolidrect (rect, RGB (0,255, 0); // set it to a green background
Cdialog: onpaint ();
}
Method 3: overload onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor), that is, wm_ctlcolor message. The specific steps are as follows (the above project prevails ):
① In the header file of cexampledlgdlg, add a member variable of cbrush:
Class cexampledlgdlg: Public cdialog
{
...
Protected:
Cbrush m_brush;
...
};
② Add the following code to 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 onctlcolor (...), That is, wm_ctlcolor message:
Hbrush cexampledlgdlg: onctlcolor
(CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
Return m_brush; // Add a green brush
}
Method 4: Reload the onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor) message, that is, the wm_ctlcolor message. The specific steps are as follows (the above project prevails ):
Steps ① and ② are described in steps ① and ② above.
Step ③ use classwizard to overload onctlcolor (...) (Wm_ctlcolor message) is somewhat different:
Hbrush cexampledlgdlg: onctlcolor
(CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
Hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor );
// Add a judgment statement in the dialog box
If (nctlcolor = ctlcolor_dlg)
Return m_brush; // Add a green brush
Return HBr;
}
Compile and run.