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: 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:
 

1 bool cexampledlgapp: initinstance () 2 {3... Cexampledlgdlg DLG; 4 m_pmainwnd = & DLG; 5 // call before domodal (), set the dialog box to blue background, red text 6 setdialogbkcolor (RGB (255 ), RGB (255, 0, 0); 7 int nresponse = DLG. domodal (); 8 ...}
 
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 ):
1 void cexampledlgdlg: onpaint () 2 {3 if (isiconic () 4... 5 else 6 {7 crect rect; 8 cpaintdc DC (this); 9 getclientrect (rect); 10 DC. fillsolidrect (rect, RGB (0,255, 0); // set to green background 11 cdialog: onpaint (); 12} 13}

 

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:

1 class   CExampleDlgDlg   :   public   CDialog    2   {...    3     protected:    4     CBrush   m_brush;      5     ...    6   };   

 

② Add the following code to the oninitdialog () function:
1 bool cexampledlgdlg: oninitdialog () 2 {3... 4 // todo: add extra initialization here 5 m_brush.createsolidbrush (RGB (0,255, 0); // generate a green brush 6... 7}

③ Use classwizard to overload onctlcolor (...), That is, wm_ctlcolor message:
 
1 hbrush cexampledlgdlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor) 2 {3/* 4 ** no code is required here! 5 ** the downstream code should comment out 6 ** hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor); 7 */8 return m_brush; // Add a green brush 9}

 



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:
 
1 hbrush cexampledlgdlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor) 2 {3 hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor ); 4 // Add a confirmation Statement 5 If (nctlcolor = ctlcolor_dlg) 6 RETURN m_brush; // Add a green brush 7 return HBr; 8} 9

 

Based on the bitmap button in the previous article, check whether it is in harmony 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:

 

1 hbrush ctestdlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor) 2 {3 hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor ); 4 If (nctlcolor = ctlcolor_dlg) // dialog box color 5 return m_brush; // Add a green brush 6 if (nctlcolor = ctlcolor_static & pwnd-> getdlgctrlid () = idc_static) // static text color, and specify the Control ID 7 {8 PDC-> settextcolor (RGB (, 0, 0 )); 9 PDC-> setbkcolor (RGB (191,219,255); // set the background color of the static control, which is consistent with the background color, you can avoid setting transparent 10 // PDC-> setbkmode (transparent); // If the mode is set transparent, the background color settings of the static control are ignored, color fusion with dialog box 11 HBr = (hbrush) m_brush; 12} 13 if (nctlcolor = ctlcolor_edit) // text edit box color 14 {15 PDC-> settextcolor (RGB, 255); 16 // PDC-> setbkcolor (RGB (191,219,255); // set the background color of the static control to 17 PDC-> setbkmode (transparent ); 18 HBr = (hbrush) m_brush; 19} 20 return HBr; 21}

 

 

The effect is as follows:

For the nctlcolor type, that is, its value:

1 ctlcolor_dlg Dialog Box 2 ctlcolor_edit edit box 3 ctlcolor_listbox list box 4 ctlcolor_msgbox message box 5 ctlcolor_scrollbar slide bar 6 ctlcolor_static static text box, rectangle, etc.

From: http://blog.csdn.net/akof1314/article/details/4952372

 

Dialog Box background color and control 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.