To change the appearance (background color and text foreground color) of a MFC checkbox and a radiobutton, I used the following implementation which worked fine in Windows2000, half OK in Windows XP, but not OK in Windows 7:
BEGIN_MESSAGE_MAP(mycheckbox, CButton)
...
ON_WM_CTLCOLOR_REFLECT()
...
END_MESSAGE_MAP()
HBRUSH mycheckbox::CtlColor(CDC* pDC, UINT nCtlColor)
{
pDC->SetBkColor( RGB( 255, 0, 0 ) );
pDC->SetTextColor( RGB( 0, 255, 0 ) );
return m_brush;
}
This works fine as long as the Windows Classic theme is used. However, when using a different theme:
- Symptoms in Windows XP:
SetBkColor
works butSetTextColor
is ignored - Symptoms in Windows 7: both
SetBkColor
andSetTextColor
are ignored
I tried OnEraseBkgnd to fill the background with a custom color (pDC->FillSolidRect
) but even this had no effect.
I want to avoid using ownerdrawn OnPaint
so the check and radio marks keep following the theme that is active in Windows. As written before, this code is used in W2000, Windows Xp, Vista and Windows 7... I just want to change the background color and text color.
I have written a CButton that will use ownerdraw when theming is active in Windows (that is not the case when Windows Classic is used), and will do so dynamically. This sample code is not complete but it demonstrates everything needed to get the results.
The difficult part is that you need to represent highlighted and pressed states, see the parameters for
DrawCheckBox
. I am ignoring them as they will not be entirely missed in my application.I even tried runtime theme switching, however that gives undesired effect when switching from
Windows 7
theme toWindows Classic
(checkbox then looks like a regular button). So I am not using this but maybe it is interesting to share: