Changing the background color of a dialog

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

If you want to change the background color of your dialog box, it is a very simple.

In your CTestDlg header file, declare a member variable from CBrush:


class CTestDlg : public CDialog
{
...
protected:
 CBrush m_brush; 
...
};
Then, add this line in the OnInitDialog function:

BOOL CTestDlg::OnInitDialog() 
{
 ...
 m_brush.CreateSolidBrush(RGB(255, 255, 255)); // color white brush 
 ...
} 
Finally do this on the ID_CTLCOLOR handle:

HBRUSH CAboutDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
/*
** No need to do this!
**
** HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
*/

/*
** Return the white brush.
*/
return m_brush;
}
Hope this could help any fellow programmer!

Date Last Updated: February 8, 1999



Comments

  • What about ::AfxMessageBox or CWnd::MessageBox

    Posted by Legacy on 02/24/2004 12:00am

    Originally posted by: Its not a toomah!

    I want to also change the background of ::AfxMessageBox or CWnd::MessageBox.

    Can anybody here tell me how to do this?

    Thx

    Reply
  • Background color of dialog

    Posted by Legacy on 11/26/2003 12:00am

    Originally posted by: Aqualung

    It's much easier to do the following in the InitInstance:

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

    • SetDialogBkColor problem??

      Posted by break; on 07/01/2009 08:43am

      Hi, SetDialogBkColor dont work in my project, and i dont know why?? :( Any idea for this? regards break;

      Reply
    Reply
  • thanks a lot,and a piece of advice

    Posted by Legacy on 10/22/2003 12:00am

    Originally posted by: Alex

    I am wondering about control background color changing. Through effect I find those methods mentioned in this article can change only color of text and dialog background.If we want to change the color of control,we must override the control class indivisually and rewrite their functions like DrawItem ,etc.
    Welcome comments about this opinion.

    Reply
  • Alternate simple ways to change the dialog background color

    Posted by Legacy on 09/22/2003 12:00am

    Originally posted by: Jes

    BOOL CJesDialog::OnEraseBkgnd( CDC* pDC )
    
    {
    CRect rectPage;

    GetClientRect(rectPage.operator LPRECT());

    // m_brDlgColor is CBrush object and should have created
    // in OnInitDialog
    pDC->FillRect(rectPage.operator LPRECT(),
    &m_brDlgColor);
    pDC->DrawText(CString("Jes"),rectPage.operator LPRECT(),
    DT_CENTER | DT_VCENTER);

    return TRUE;
    }

    or simply

    void CJesDialog::OnPaint()
    {

    CPaintDC dc(this);

    dc.FillRect(m_rectGUI.operator LPRECT(),&m_brDlgColor);

    // you may retain code for drawing Icon; But never
    // allow to call CDialog::OnPaint()
    }

    Reply
  • How to Change MessageBox Background?

    Posted by Legacy on 02/07/2003 12:00am

    Originally posted by: Anand Shah

    Hi the code mentioned does not work for Message Box's.
    Can anyone help with changing the Background color of the MessageBox.

    • How to Change MessageBox Background?

      Posted by Mark Veldt on 05/14/2004 08:07pm

      Search for Q99808 in Google. Microsoft has an example

      Reply
    Reply
  • I have a concern

    Posted by Legacy on 12/05/2002 12:00am

    Originally posted by: Deandrea Venon

    I need information on creating an atm application.
    The user should be able to enter deposit and withdrawal.
    In the end closing balance would be shown
    as:
    closingbalance=openingbalance+deposit-withdrawal
    i have attempted it but it displays anassertion error. then everything in the dialog becomes blank. Please help!!

    Reply
  • OK How to do it on a multi form app??

    Posted by Legacy on 05/30/2002 12:00am

    Originally posted by: javajunkie

    I have an app I would like to have the option of colors on each of 7 or 8 different forms that are CRecordViews. I can get the first form color changed but not the rest of them. I am using the OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) method. Any ideas on how to make all the forms have the background color changed?
    I know this isn't much deatil but if someone thinks they have a way I can provide the details for them.

    TIA

    Reply
  • How can I change check box's color and radio button color

    Posted by Legacy on 05/01/2002 12:00am

    Originally posted by: cthSon

    Button, radio and checkbox do not chang color

    Reply
  • Can't change bgcolor of messageboxes.

    Posted by Legacy on 04/09/2002 12:00am

    Originally posted by: Pkun

    It does not work for messageboxes. How can I change messagebox's background?

    Reply
  • thank you

    Posted by Legacy on 03/08/2002 12:00am

    Originally posted by: linxiao

    It really work!
    

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date