Cannot get Static Picture Control (Rectangle Mode) to change color

Hey Everybody;

I am currently trying to get Static Picture Control in Rectangle Mode to Change colors from the default black, grey, or white. Here is my code to do so,

HBRUSH Cdev_discDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
      HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
      switch (nCtlColor) {
            case CTLCOLOR_STATIC:
                   if (pWnd->GetDlgCtrlID() == IDC_myRectangle)
                         pDC->SetBkColor(RGB(150, 150, 150));
                         return m_pBkBrush;
                        pDC->SetTextColor(RGB(54, 51, 127));
                        pDC->SetBkColor(RGB(209, 208, 226));
                  // Drop through to return the background brush.
            case CTRLCOLOR_DLG:
                  return m_pBkBrush;

                   return m_pBkBrush;

Now the function works correctly for all other controls. I can get it to change the dialogue and other static controls but not the rectangle. Any help would be greatly appreciated
Who is Participating?

Improve company productivity with a Business Account.Sign Up

PriyeshConnect With a Mentor Commented:
SS_xxxRECT style does not seem to work with CtlColor. Not sure why that is.

Tried making a derived class from CStatic and handling the OnPaint like this. Seems to work.
Has the added advantage of having the color as a member in this class.

void CMyStatic::OnPaint()
      CPaintDC dc(this) ;
      CRect rc ;
      GetClientRect(rc) ;
      dc.FillSolidRect(rc, RGB(0, 0, 255)) ;

OnCtlColor function may be used to change dialog controls color and text parameters. To change control reclangle you need MoveWindow function. To get current dialog control rectangle, use GetWindowRect function. OnCtlColor is not a good place to move the controls. You may do this in any other place of the program. For example, after pressing of some button you want to move some dialog control:

void CMyDialog::OnSomeButton()
    CRect rect;
    CWnd* pWnd = GetDlgItem(IDC_CONTROL_ID);
    rect.left += 10;
    rect.right += 10;
   My guess is that SS_xxxRECT style won't work with CtlColor. The following link may explain why.

The SS_BLACKRECT static control displays a frame around the control and fills the control with a solid color that is the same color as the Window Frame (COLOR_WINDOWFRAME). The default Windows color is black. The user can change the color by changing the window frame color in the Control Panel, or an application can call SetSysColors.

Obviously, a static with SS_BLACKRECT seems not relying on the brush passed from CtlColor, instead, it fills the rect with COLOR_WINDOWFRAME. I would also suggest the solution as given by Priyesh.

Any info which shows how this can be done as per given in the question would be appreciated.

Handle WM_ERASEBKGND and paint the background

BOOL CMyStatic::OnEraseBkgnd(CDC* pDC)

    CRect rcBounds;
    pDC->FillSolidRect(rcBounds, RGB(255, 0, 0)); // FILL WITH RED

    return TRUE;
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.