In MFC, you can find old threads like : How to change Checkbox text color
Can I change the text of a check/radio button using WM_CTLCOLOR?
I have a need to set the text color for the text of a button class where the button is a check button or radio button when they are on dialogs. I have a handler for WM_CTLCOLOR. What I am seeing is that the button is passed in and nCtlColor is 6 which is CTLCOLOR_STATIC. I am getting the window class name and it is "Button". I get the style and see it is BS_CHECKBOX/BS_RADIOBUTTON. I return my background brush. I also call SetTextColor on the DC at that time but the color isn't used.
If I return the HOLLOW_BRUSH (just testing), I get the system background color for the button and still the text is black, which isn't the color I need to see.
Is there something else I can do in OnCtlColor? Or some other API I can use in GDI to get my desired color?
About the only other thing I can try is to get the text and font ... and make my own calls to draw the text. Probably have to change the caption to "" in order to avoid display issues if I can't get all the pixels lined up exactly the same. I sure hope there is an easier way. I know about owner draw but that has caused me issues before as I had to subclass the window and it turned out I hit a case where some other code had subclassed a window too (I'm working with MFC so ... kaboom). I already handle the BS_OWNERDRAW style for these buttons in OnCtlColor so it seems dangerous to dynamically switch buttons to owner draw and do my own drawing.
Hm. Tried to "Tag" GDI but it never shows up.
3 answers
Sort by: Most helpful
-
-
Holland, RD (DI SW ME PRD SDE AS) 31 Reputation points
2022-10-20T15:26:42.97+00:00 Hi Castorix31,
That's no different than what I am doing. Just to be sure, I created a MFC app and hooked up OnCtlColor for the About dialog. My code that sets the text color breakpoint trips when it comes up. And, it trips anytime I move over the box. But, the color doesn't change. That post is from 2001 and a lot has changed since then, including the use of UIThemes (I suppose).
I even went super simple since in this case I "know" the ID of the control and don't have to examine the window class name.
HBRUSH CAboutDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);if( pWnd->GetDlgCtrlID() == IDC_CHECK1 ) { pDC->SetTextColor( RGB(255,0,0) ); } return hbr;
}
I even tried without checking the input and just always call SetTextColor. Everything but buttons change text color.
-
Holland, RD (DI SW ME PRD SDE AS) 31 Reputation points
2022-10-20T15:27:55.27+00:00 Sorry about the huuuuuuge image there. Windows really doesn't know what to do with a scaled desktop. What you see is no longer what you get!