I wrapped the handles in a widget class so the foreground color, background color, and cursor go along for the ride.
I am having a lot of trouble figuring out how to set the foreground (text) color of a static control. I can get the foreground color to change, but then the background color doesn't match the Window color, or the Window color is correct but the text color is not.
Here is what I have, where the control is question is a static control, but there are edit controls on the window also:
case WM_CTLCOLOREDIT:
case WM_CTLCOLORSTATIC:
widget = Widget::find((HWND)lParam); // find a widget from handle
if (widget)
{
COLORREF color = widget->color();
HBRUSH bg = widget->background(); // may be nullptr if none set (use default window bg)
SetBkMode((HDC)wParam, TRANSPARENT);
SetTextColor((HDC)wParam, color);
// try 1: always return the background, even if nullptr
// result: correct foreground color, incorrect background color (white)
return (INT_PTR)bg;
// try 2: only return the background if set, else default to DefWindowProcW(...)
// result: incorrect foreground color (black), correct background color
if (bg) return (INT_PTR)bg;
break;
// try 3: if no background, ask for parent's background
// result: both foreground and background are correct,
// however, every control has the same background color (the main window's),
// but some controls I want to have their default
// (i.e. edit controls have a white background by default)
while (!bg && widget)
{
widget = widget->parent();
if (widget) bg = widget->background();
}
return (INT_PTR)bg;
}
Can the foreground (text) color be set without setting the background? I don't always know what the background color is (when background() returns nullptr), and want to use the control's default.
Or, am I taking the complete wrong approach to this?
I am new to the Win32 API and GUI programming.