SetDialogBkColor ()
Cwinapp::setdialogbkcolor
This function is obsolete.
This function has been deprecated and I can actually set the background of the window at the time of initialization vc++6.o
BOOL ctest_colorapp::initinstance () {/*ccolordialog cdlg;if (IDCANCEL! = Cdlg. DoModal ()) {COLORREF cl = Cdlg. GetColor (); SetDialogBkColor (cl, RGB (0, 0, 0));} */setdialogbkcolor (RGB (255,255,0), RGB (0,0,0)); Ctest_colordlg Dlg;m_pmainwnd = &dlg;int Nresponse = dlg. DoModal (); if (nresponse = = IDOK) {//Todo:place code here to handle when the dialog is// dismissed with Ok}else if (NR Esponse = = IDCANCEL) {//Todo:place code here to handle when the dialog is//dismissed with cancel}//Since the dialog Have been closed, return FALSE so, we exit the// application, rather than start the application ' s message pump.ret Urn FALSE;}
Operating effect:
Later I tested it in VS2012:
BOOL ccolordlgapp::initinstance () {SetDialogBkColor (RGB (255,255,0), RGB (0,0,0)); CShellManager *pshellmanager = new CShellManager; Cmfcvisualmanager::setdefaultmanager (Runtime_class (cmfcvisualmanagerwindows)); Setregistrykey (_t ("native application generated by Application Wizard"); Ccolordlgdlg Dlg;m_pmainwnd = &dlg;int_ptr Nresponse = dlg. DoModal (); if (nresponse = = IDOK) {//TODO: The code in this place processing when// "OK" to close the dialog box}else if (nresponse = = IDCANCEL) {//TODO: Where to place processing when Use// "Cancel" to close the dialog box code}else if (nresponse = =-1) {TRACE (traceappmsg, 0, "Warning: dialog box creation fails and the application terminates unexpectedly.) \ n "); TRACE (traceappmsg, 0, "Warning: If you use MFC controls on a dialog box, you cannot #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS. \ n ");} Delete the Shell manager created above. if (Pshellmanager! = NULL) {delete Pshellmanager;} Because the dialog box is closed, FALSE is returned to exit the application,// instead of starting the application's message pump. return FALSE;}
Operating effect:
Note that the above function belongs to CWinApp, not to the cdialog below; If you accidentally write in the init function of your derived class dialog box, an undefined error occurs
Since this function is discarded, what is it instead?
The WM_CTLCOLOR message is used to complete setting the background and font color for controls such as edit, STATIC, button,
Automatic generation of the OnCtlColor () function using the Class wizard
This function sets the desired color and then returns a brush handle to redraw the control background color. The onctlcolor () function handles the control's background color by capturing the corresponding control message. Common types of such messages are:
ctlcolor_dlg dialog box
ctlcolor_edit edit box
ctlcolor_msgbox message box
ctlcolor_scrollbar Slide Bar
.............
Hbrush Ctest_colordlg::onctlcolor (cdc* PDC, cwnd* pWnd, UINT nctlcolor) {hbrush HBR = Cdialog::onctlcolor (PDC, PWnd, NCtlC Olor), if (pwnd->getdlgctrlid () = = idc_static) {pdc->setbkmode (TRANSPARENT);pD C->settextcolor (RGB ( 255,255,46)); Hbrush B = CreateSolidBrush (RGB (0,0,88)); return (hbrush) b;} Switch (nctlcolor) {case CTLCOLOR_BTN://Not used {Pdc->setbkmode (TRANSPARENT); Hbrush B = CreateSolidBrush (RGB (0,0,255)); return (hbrush) b;} Case ctlcolor_dlg:{Pdc->setbkmode (TRANSPARENT); Hbrush B = CreateSolidBrush (RGB (255,0,255)); return (hbrush) b;} Case Ctlcolor_edit: { colorref bkcolor = RGB (255,255,0); CRect rect; Pwnd->getclientrect (&rect); CBrush BR; Br. CreateSolidBrush (Bkcolor); Pdc->fillrect (rect, &br); Pdc->setbkcolor (Bkcolor); Pdc->settextcolor (RGB (255,255,0)); Hbrush B = CreateSolidBrush (RGB (125,125,255));} Default:return HBR;} return HBR;}
Below the VS2012:
Hbrush Ccolordlgdlg::onctlcolor (cdc* PDC, cwnd* pWnd, UINT nctlcolor) {if (pwnd->getdlgctrlid () = = Idc_color_edit) { Pdc->setbkmode (TRANSPARENT);pD C->settextcolor (RGB (0,255,0)); Hbrush B = CreateSolidBrush (RGB (255,255,0)); return (hbrush) b;} Switch (nctlcolor) {case Ctlcolor_btn:{pdc->setbkmode (TRANSPARENT); Hbrush B = CreateSolidBrush (RGB (0,0,255)); return (hbrush) b;} Case ctlcolor_dlg:{Pdc->setbkmode (TRANSPARENT); Hbrush B = CreateSolidBrush (RGB (0,0,255)); return (hbrush) b;} Case Ctlcolor_edit: { colorref bkcolor = RGB (255,0,0); CRect rect; Pwnd->getclientrect (&rect); CBrush BR; Br. CreateSolidBrush (Bkcolor); Pdc->fillrect (rect, &br); Pdc->setbkcolor (Bkcolor); Pdc->settextcolor (RGB (255,255,0)); Hbrush B = CreateSolidBrush (RGB (125,125,255));} Default:return Cdialogex::onctlcolor (PDC, PWnd, nCtlColor);}}
But this message can not be set button buttons; I have tried many times can not, although it has ctlcolor_btn, but no effect!!
Wm_ctlcolor messages are applicable to the following five types of controls:
1.Check box, radio button, push button: Draws the rectangle that the control is placed on with the selected brush, and the shape and text of the control are then drawn on it;
2.Edit control: Draw the editing area;
3.Group box: Draws a rectangular area below the title text;
4.Scroll Bar: Draw the trajectory of the slider;
5.List box: Draw the list area.
Static text, borders, and rectangles are not affected by this message.
It is also important to note that although the Wm-ctlcolor message is applicable to push button, from Windows 3.0 and later versions of the system, the user program cannot modify the button appearance color restrictions, if you want to achieve the desired effect, You need to use the Owner-drawn button.
How to change the color of button buttons have not studied, later need to study, today just want to modify the background of the color, pull out so many knowledge points!
MFC Settings Window background