VC dialog box background color and control color

Source: Internet
Author: User

System Environment: Windows 7
Software environment: Visual C ++ 2008 SP1
Purpose: To set the background color and control color for the dialog box

Since the MFC dialog box is not easy to develop, let's start to beautify our dialog box. Set the background color and control color for the dialog box.

 

Dialog Box background color:

 

There are four methods available on the Internet (probably more than one). After the VC ++ 2008sp1 test, only three methods can be used, and the first one is discarded. There are four methods:

Method 1
(Invalid): 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 conversation box of the application to blue background and red text. The procedure is as follows:
① Create a dialog-based MFC Appwizard application exampledlg.
② Add the following code to cexampledlgapp: initinstance:
Bool cexampledlgapp: initinstance () <br/>{< br/>... Cexampledlgdlg DLG; <br/> m_pmainwnd = & DLG; <br/> // call before domodal, set the dialog box to blue background, red text <br/> setdialogbkcolor (RGB (255,), RGB (, 0); <br/> int nresponse = DLG. domodal (); <br/> ...}
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 () <br/>{< br/> If (isiconic () <br/>... <Br/> else <br/> {<br/> crect rect; <br/> cpaintdc DC (this); <br/> getclientrect (rect ); <br/> DC. fillsolidrect (rect, RGB (0,255, 0); // set it to a green background <br/> cdialog: onpaint (); <br/>}< br/>}

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 ):
① Add a member variable of cbrush to the header file of cexampledlgdlg:
Class cexampledlgdlg: Public cdialog <br/> {... <br/> protected: <br/> cbrush m_brush; <br/>... <br/> };
② Add the following code to the oninitdialog () function:
Bool cexampledlgdlg: oninitdialog () <br/>{< br/>... <br/> // todo: add extra initialization here <br/> m_brush.createsolidbrush (RGB (0,255, 0); // generate a green brush <br/>... <br/>}
③ Use classwizard to overload onctlcolor (...), That is, wm_ctlcolor message:
Hbrush cexampledlgdlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor) <br/>{< br/>/* <br/> ** no code is required here! <Br/> ** the downstream code should be commented out <br/> ** hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor ); <br/> */<br/> return m_brush; // Add a green brush. <br/>}
Method 4
: Reload the onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor) message, that is, wm_ctlcolor. 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) <br/>{< br/> hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor); <br/> // Add a judgment statement for the dialog box <br/> If (nctlcolor = ctlcolor_dlg) <br/> return m_brush; // Add a green brush <br/> return HBr; <br/>}

, In the bitmap button Based on the previous
To see if it is quite harmonious with the background:

 

Control color:

For controls distributed on the dialog box, we can modify their color and background color, such as static text, text edit box, and list box.

Steps ① and ② are described in steps ① and ② above.
Step 3: Use classwizard to reload onctlcolor (...) (Wm_ctlcolor message) is somewhat different:

Hbrush ctestdlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor) <br/>{< br/> hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor); <br/> If (nctlcolor = ctlcolor_dlg) // dialog box color <br/> return m_brush; // Add a green brush <br/> If (nctlcolor = ctlcolor_static & pwnd-> getdlgctrlid () = idc_static) // The static text color, and specify the Control ID <br/>{< br/> PDC-> settextcolor (RGB (, 0 )); <br/> PDC-> setbkcolor (RGB (191,219,255); // set the background color of the static control, which is consistent with the background color, you can avoid transparent Settings <br/> // PDC-> setbkmode (transparent); // If the mode is transparent, the background color settings of the static control are ignored, color fusion with the dialog box <br/> HBr = (hbrush) m_brush; <br/>}< br/> If (nctlcolor = ctlcolor_edit) // text edit box color <br/>{< br/> PDC-> settextcolor (RGB (255 )); <br/> // PDC-> setbkcolor (RGB (191,219,255); // set the background color of the Static Control <br/> PDC-> setbkmode (transparent ); <br/> HBr = (hbrush) m_brush; <br/>}< br/> return HBr; <br/>}

The effect is as follows:

For the nctlcolor type, that is, its value:

Ctlcolor_dlg dialog box
Ctlcolor_edit edit box
Ctlcolor_listbox list box
Ctlcolor_msgbox message box
Ctlcolor_scrollbar Slider
Ctlcolor_static static text box, rectangle, etc.

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.