locked
MFC dialog how to get background color RRS feed

  • Question

  • I want to get the background color of an mfc dialog form because my program draws on the form and to erase it I need the color of the dialog. Right now im erasing in black and it looks stupid it would work perfectly but I have no clue how to get the grey background color of the dialog. Anyone know? Im pretty new to MFC I think c# is a bit easier to use.
    Thursday, March 15, 2007 6:29 PM

Answers

  • I think you should consider this method too:

    COLORREF colour = ::GetSysColor(COLOR_3DFACE);

    I hope this helps.

     

    Friday, March 16, 2007 7:06 AM
  • // Dlg Bg Color using OnCtlColor mapping
    HBRUSH CSampleDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
       HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

       if (nCtlColor == CTLCOLOR_DLG )
       {
          m_Brush = CreateSolidBrush(...); / / You can create this OnInitDialog Also
          // Return handle to our CBrush object
          hbr = m_brush;
       }
       return hbr;
    }

    You can also make use of OnEraseBkgnd handler.

    Once I've posted sample snippet on same in my blog

    Check MSDN for more details on APIs
    Friday, March 16, 2007 3:54 AM
  • but I have no clue how to get the grey background color of the dialog. Anyone know?

    Override OnInitDialog of your dialog class, and use this code,

    CDC* pDc= GetDC(); 
    COLORREF crBkgnd = pDc->GetBkColor();

    Friday, March 16, 2007 6:35 AM

All replies

  • // Dlg Bg Color using OnCtlColor mapping
    HBRUSH CSampleDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
       HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

       if (nCtlColor == CTLCOLOR_DLG )
       {
          m_Brush = CreateSolidBrush(...); / / You can create this OnInitDialog Also
          // Return handle to our CBrush object
          hbr = m_brush;
       }
       return hbr;
    }

    You can also make use of OnEraseBkgnd handler.

    Once I've posted sample snippet on same in my blog

    Check MSDN for more details on APIs
    Friday, March 16, 2007 3:54 AM
  • but I have no clue how to get the grey background color of the dialog. Anyone know?

    Override OnInitDialog of your dialog class, and use this code,

    CDC* pDc= GetDC(); 
    COLORREF crBkgnd = pDc->GetBkColor();

    Friday, March 16, 2007 6:35 AM
  • I think you should consider this method too:

    COLORREF colour = ::GetSysColor(COLOR_3DFACE);

    I hope this helps.

     

    Friday, March 16, 2007 7:06 AM
  • Thanks guys I could not find anything on how to do this. Lots of stuff on how to change the color but not on getting the background color.

    If I override the oninitdialog does it still run my the regular oninitdialog after it runs the override one? GetDC returns a HWND not a CWnd and has no GetBkColor that i could see.

    BOOL override CdialogmfcDlg::OnInitDialog()
    {
        HWND h=GetDC(mydlg->GetSafeHwnd());
        COLORREF crBkgnd = pDc->GetBkColor();

    }

    for this code do I pass the CTLCOLOR_DLG to createbrush then return that? How do I get get the result isnt this method called many times repeatedly?
    HBRUSH CSampleDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
       HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

       if (nCtlColor == CTLCOLOR_DLG )
       {
          m_Brush = CreateSolidBrush(...); / / You can create this OnInitDialog Also
          // Return handle to our CBrush object
          hbr = m_brush;
       }
       return hbr;
    }

    Friday, March 16, 2007 11:58 AM
  • Works as marvel thanks a lot :)
    Thursday, May 5, 2011 6:21 AM