We help IT Professionals succeed at work.

MFC: How to change BG color of a group-box (CStatic)

7,297 Views
Last Modified: 2013-12-14
Hello,

I have a group-box in my MFC dialog box. Its variable is class CStatic.
I need to set the groupbox' background color to white.

How can I do that?

Thanks!
Comment
Watch Question

Change the id of your static from IDC_STATIC to IDC_STATIC_NEW

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

   if (pWnd->GetDlgCtrlID() == IDC_STATIC_NEW)
   {
      m_Brush = CreateSolidBrush(...); / / You can create this OnInitDialog Also
      // Return handle to our CBrush object
      hbr = m_brush;
   }
   return hbr;
}

Author

Commented:
Thanks buddies.
I tried this code:

HBRUSH CSDRegFirstDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
   HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

   if (pWnd->GetDlgCtrlID() == IDC_INNERFRAME)
   {
      HBRUSH m_Brush = CreateSolidBrush(RGB(255, 255, 255)); // You can create this OnInitDialog Also
      // Return handle to our CBrush object
      hbr = m_Brush;
   }
   return hbr;
}

I've put a breakpoint in the createsolidbrush line, and it actually reaches there. But I see to effect. The background isn't white, just the normal gray.

What could be the problem?
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
are you running any other GUI libraries?  e.g CodeJock libraries with Skins turned on will override this method.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.