0

I'm working on a Win32 C++ GUI Desktop application.

All the edit text are created statically in the resource file and their background is changed in the DialogBox routine using the WM_CTLCOLOREDIT and WM_CTLCOLORSTATIC messages.

case WM_CTLCOLOREDIT:
  if (lParam == (LPARAM)Edit1Hwnd)
    return SetBkColor((HDC)wParam, GuiColors[RED]);
  else if (lParam == (LPARAM)Edit2Hwnd)
    return SetBkColor((HDC)wParam, GuiColors[GREEN]);
  break;

case WM_CTLCOLORSTATIC:
  if (lParam == (LPARAM)Edit3Hwnd)
    return SetBkColor((HDC)wParam, GuiColors[BLUE]);
  else if (lParam == (LPARAM)Edit4Hwnd)
    return SetBkColor((HDC)wParam, GuiColors[YELLOW]);
  break;

When the window loads I can see that all the edit have the correct background color.

The problem rises when I use Common Controls 6.0 (which I need in order to load a BMP as a list-view background).

In order to enable Common Controls 6.0 I do:

#pragma comment(linker,"\"/manifestdependency:type='win32' \name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

If I run the program with the Common Controls enabled, all the edit text appear with a white background. But I can change the edit color selecting it with the mouse cursor.

Searching on the site I found this question.

It confirms the behaviour I experience, but it doesn't give a solution for changing the edit background color when it doesn't have the focus.

Any help is appreciated.

EDIT: Thanks RbMm for your answer, I changed the code to

case WM_CTLCOLOREDIT:
  if (lParam == (LPARAM)Edit1Hwnd)
    return (LRESULT)redBrush;
  break;

Where redBrush is declared and created like this

static HBRUSH redBrush = NULL;
redBrush = CreateSolidBrush(GuiColors[RED]);

The edit background color is properly red painted at the start, but when the edit gets the focus it turns white again (it turns back to red when the focus is lost).

EDIT 2: Thanks zett42, now it works!

case WM_CTLCOLOREDIT:
  if (lParam == (LPARAM)Edit1Hwnd){
    SetBkColor((HDC)wParam, GuiColors[RED]);
    return (LRESULT)redBrush;
    }
  break;
user3121071
  • 21
  • 2
  • 4
  • for [`WM_CTLCOLOREDIT`](https://learn.microsoft.com/en-us/windows/desktop/controls/wm-ctlcoloredit) you must return the handle of a brush to paint the background of the edit control. but you return result of [`SetBkColor`](https://learn.microsoft.com/en-us/windows/desktop/api/wingdi/nf-wingdi-setbkcolor) - so `COLORREF`. you need after call return separate such handle. create this brush once and then return – RbMm Jan 08 '19 at 14:22
  • You must do both. Call `SetBkColor()` and return brush with same color. – zett42 Jan 08 '19 at 20:11

0 Answers0