0

I am trying to change the color of static text (and also checkbox items) in a dialog window in my MFC application.

Following this (MFC - change text color of a cstatic text control) and similar suggestions, I did the following on ON_WM_CTLCOLOR() message:

HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd *pWnd, UINT nCtlColor)
{
      pDC->SetTextColor(RGB(255, 0, 0));
      return (HBRUSH)GetStockObject(NULL_BRUSH);
}

The problem is that this only affects edit text boxes and not static text or checkboxes. those still have black texts.

I also tried to look for sth similar to winapi's WM_CTLCOLORSTATIC message as that worked well in win32 applications but did not find any equivalent in MFC. Any idea how to change the color of the static texts and checkbox text?

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
Kevin
  • 21
  • 1
  • 6

2 Answers2

1

This works for me:

Put this in the message map:

ON_WM_CTLCOLOR()

And implement something like this:

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

  if (pWnd->GetDlgCtrlID() == IDC_SOMESTATIC)
  {
    // display the static control IDC_SOMESTATIC in red
    pDC->SetTextColor(RGB(255, 0, 0));
  }

  return hbr;
}
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • Thanks, it still does not change the static text color or checkboxes for me. I am trying this on a BCGSoft Property page. I did a quick test and print out all the IDs to console. It seems for static text drawings, it does not even go through OnCtlColor(...) as I do not see any static id printed out to console... And for checkboxes even though it goes through the OnCtlColor, it still has no effect. I tried to disable the windows theme for checkboxes as below, and still no success on checkboxes either: ::SetWindowTheme(GetDlgItem(IDC_PAGE0_CHECK1)->GetSafeHwnd(), L"wstr", L"wstr"); – Kevin Nov 06 '20 at 20:50
  • I disabled BCG visual theme on that dialog window and now I can see the effect of color change on static texts. At this stage, it seems now the only way is now to override the visual manager and instruct it to use a certain color for drawing the static texts. – Kevin Nov 06 '20 at 21:33
0

When you add a Static Text control from the Toolbox, it will get the ID IDC_STATIC. You need to rename this ID to something else, and then use OnCtlColor. Assuming you name it IDC_STATIC_1, In OnCtlColor, use:

case IDC_STATIC_1:
    pDC->SetBkMode(TRANSPARENT);
    pDC->SetTextColor(RGB(100,110,120);
Michael Haephrati
  • 3,660
  • 1
  • 33
  • 56
  • I tied renaming the id of static texts to make them unique before. And I still cannot change their color. The problem is related to BCGSoft that I use which do not work sometimes pure MFC does. – Kevin Nov 21 '20 at 22:22
  • My answer applies to MFC. Have you tried BCGSoft documentation? – Michael Haephrati Nov 22 '20 at 02:11
  • I asked them and they said they do not support those types of cosutomization. If I am not wrong, there should be a visual manager class for each application look in BCGSoft. It should be possible to override the colors in there and then hook that to the application bu that was too much work without any sample/support to where to go. – Kevin Nov 30 '20 at 00:48