Vc/mfc How to set the background color of a dialog box

Source: Internet
Author: User

Method One: Call the member function of the CWinApp class SetDialogBkColor to implement. (This function is deprecated)

1 voidSetDialogBkColor(COLORREFclrCtlBk = RGB(192, 192, 192), COLORREFclrCtlText = RGB(0, 0, 0));

Where the first parameter of the function specifies the background color, the second parameter specifies the text color.

Add the following code in InitInstance ():

1 SetDialogBkColor(RGB(0,0,255),RGB(255,0,0));

It is important to note that SetDialogBkColor must be called before calling DoModal ().

This method has been verified by the author and cannot change the background of the dialog box

Method Two: Overload OnPaint (), which is the WM_PAINT message

1234 CRect   rect;   CPaintDC   dc(this);  GetClientRect(rect);  dc.FillSolidRect(rect,RGB(0,255,0));  //设置为绿色背景

Method Three: Overload OnCtlColor (cdc* PDC, cwnd* pWnd, UINT nCtlColor), which is the WM_CTLCOLOR message

. h header File Additions CBrush M_brush;

. cpp source file Additions M_brush. CreateSolidBrush (RGB (255,0,0));

1234567 onctlcolor (cdc*   pdc,   cwnd*   pwnd,   Code class= "CPP Color1 bold" >uint    nctlcolor)     Code class= "CPP Plain" >{  /*  downlink code to comment out         hbrush   hbr   =   Cdialog::onctlcolor (pDC,   pwnd,   nCtlColor);   */   return   m_brush;   Code class= "CPP Comments" >//back add red brush   }

Method Four: or overload OnCtlColor (cdc* PDC, cwnd* pWnd, UINT nCtlColor), which is the WM_CTLCOLOR message

. h header File Additions CBrush M_brush;

. cpp source file Additions M_brush. CreateSolidBrush (RGB (255,0,0));

12345678 OnCtlColor(CDC* pDC, CWnd* pWnd,UINT nCtlColor)    HBRUSH hbr   =   CDialog::OnCtlColor(pDC,   pWnd,   nCtlColor);   //在这加一条是否为对话框的判断语句      if(nCtlColor   ==CTLCOLOR_DLG)         returnm_brush;   //返加红色刷子    returnhbr;    }

Different paint brushes can be returned depending on the type of control to achieve different color settings for controls

1234567 CTLCOLOR_BTN                按钮控件 CTLCOLOR_DLG                对话框 CTLCOLOR_EDIT               编辑框 CTLCOLOR_LISTBOX            列表控件 CTLCOLOR_MSGBOX             消息控件 CTLCOLOR_SCROLLBAR          滚动条控件 CTLCOLOR_STATIC             静态控件
1 这里大家要注意,OnCtlColor能改变Static等子控件的颜色,对于Button必须设置其属性Owner Draw为True,才能改变Button按钮背景色(CButton 文本的字体颜色并不能通过SetBkColor来改变,需要自己重绘CButton,在DrawItem中进行实现。
123456789101112131415161718192021222324252627282930313233343536373839404142434445 HBRUSHCXXXDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINTnCtlColor){ HBRUSHhbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); // TODO:  在此更改 DC 的任何属性 if(nCtlColor==CTLCOLOR_BTN)         //更改按钮颜色 {  //pDC->SetBkMode(TRANSPARENT);  pDC->SetTextColor(RGB(0,0,0));  pDC->SetBkColor(RGB(121,121,255));    HBRUSHb=CreateSolidBrush(RGB(121,121,255));  returnb; } elseif(nCtlColor==CTLCOLOR_SCROLLBAR) // {  //pDC->SetBkMode(TRANSPARENT);  pDC->SetTextColor(RGB(0,0,0));  pDC->SetBkColor(RGB(233,233,220));  HBRUSHb=CreateSolidBrush(RGB(233,233,220));  returnb; } elseif(nCtlColor==CTLCOLOR_EDIT)  //更改编辑框 {  //pDC->SetBkMode(TRANSPARENT);  pDC->SetTextColor(RGB(0,0,0));  pDC->SetBkColor(RGB(165,254,236));  HBRUSHb=CreateSolidBrush(RGB(165,254,236));  returnb; } elseif(nCtlColor==CTLCOLOR_STATIC) //更改静态文本 {  pDC->SetTextColor(RGB(0,0,0));  pDC->SetBkColor(RGB(166,254,1));  HBRUSHb=CreateSolidBrush(RGB(166,254,1));  returnb; } elseif(nCtlColor==CTLCOLOR_DLG)  //更改对话框背景色 {  pDC->SetTextColor(RGB(0,0,0));  pDC->SetBkColor(RGB(166,254,1));  HBRUSHb=CreateSolidBrush(RGB(166,254,1));  returnb; } // TODO:  如果默认的不是所需画笔,则返回另一个画笔 returnhbr;}

Vc/mfc How to set the background color of a dialog box

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.