1

This Answer is really great if you want to change the background color of a "conventional" text label. But what if you want to put a border around that text label and expand its size so that the text is swimming in a veritable sea of color? It only paints the text background in the required color, and leaves the rest of the expanded control with the standard button face. How can one make the color consistent across the entire control?

Note: The attractive feature (to me anyway) about the above answer is that it makes use of OnCtlColor(), which provides a pointer to the CWnd control concerned. So there is no need to create a subclass of CStatic to handle the color change. An answer that avoids creating such a subclass would be preferred.

Community
  • 1
  • 1
omatai
  • 3,448
  • 5
  • 47
  • 74

2 Answers2

2

I'm not very sure about OP's Note section. Still posting this code for his help.

HBRUSH CSampleDlg::OnCtlColor(CDC* pDC, CWnd *pWnd, UINT nCtlColor)
{
  switch (nCtlColor)
  {
  case CTLCOLOR_STATIC:
    {
      CRect rcWindow(0, 0, 220, 40);
      //::GetWindowRect(pWnd->GetSafeHwnd(), &rcWindow);
      pDC->FillSolidRect(rcWindow, RGB(49, 49, 49));
      pDC->SetTextColor(RGB(255, 255, 255));
      return (HBRUSH)GetStockObject(NULL_BRUSH);
    }
  default:
    {
      return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    }
  }
}
hypheni
  • 756
  • 3
  • 17
  • 37
0

You can make the static control invisible in the resource editor then paint it from CMyDialog.

void CMyDialog::OnPaint()
{
    CDialog::OnPaint();
    paintstatic(IDC_STATIC1);
}

void CMyDialog::paintstatic(int id)
{
    CClientDC dc(this);
    CRect rc;
    CWnd *child = GetDlgItem(id);
    child->GetWindowRect(&rc);
    CPoint offset(0, 0);
    ClientToScreen(&offset);
    rc.OffsetRect(-offset);
    dc.FillSolidRect(rc, RGB(0, 255, 128));

    CFont *font = GetFont();
    dc.SelectObject(font);
    CString text;
    child->GetWindowText(text);
    dc.DrawText(text, rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • Why do you call the default paint, when you are doing all painting by yourself? – xMRi May 06 '15 at 08:14
  • @xMRi: calling default paint is mandatory the way I am doing this. If `CPaintDC` was used then `CDialog::OnPaint` should not be called. But I am using `CClientDC` to paint the static control, that's in case static control needs to be updated independently of `OnPaint`. – Barmak Shemirani May 06 '15 at 14:33
  • No. It isn't needed. You can just use CPaintDC by yourself (instead of CClientDC) if you are doing the complete job by yourself. – xMRi May 06 '15 at 14:42
  • @xMRI: It's not painting the whole dialog, it's just one static control within the dialog. The question makes reference to static control which needs to change background in response to button click or timer, without using subclass. This is the only way I know how to do it without having to update the whole dialog every time a button is clicked. It needs `CClientDC`. – Barmak Shemirani May 06 '15 at 15:10
  • Oh. Yes I see, but even than it just more strange... painting a child from the parent? – xMRi May 07 '15 at 06:06