Hello everybody,

I am currently developping a GUI interface game for windows XP. I am currently having difficulty changing the background of a EDIT box created with CreateWindowEx(). It's default background is white (which is the color I want). But the user is not supposed to be able to adjust the text within that EDIT box so I added a WS_DISABLED, but then the background color changes into gray and the text color into dark gray. How do I change that?

This is a part of the switch(Message) in the the WndProc() function, the case WM_CREATE:

/* Creating box that contains amount of errors */
                HFONT hfReg = CreateFont(16, 0, 0, 0, 0, FALSE, 0, 0, 0, 0, 0, 0, 0, "Arial");
		HWND hWrong;

		hWrong = CreateWindowEx(
                WS_EX_WINDOWEDGE,
                "EDIT",
                "Amount of errors: 0",
		WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_CENTER | WS_DISABLED,      
		0, 70, 190, 20,                             
		hwnd,                                      
		(HMENU)IDC_MAIN_WRONG,
		GetModuleHandle(NULL),
		NULL); 

	        if(hWrong == NULL)
		  MessageBox(hwnd, "Could not create edit box.", "Error", MB_OK | MB_ICONERROR);

		SendMessage(hWrong, WM_SETFONT, (WPARAM)hfReg, MAKELPARAM(FALSE, 0));

Does anyone have a suggestion?

~G

Edited 6 Years Ago by Graphix: n/a

When you are assigning values for your Windows Class, you can change the
hbrBackGround value to other colours.

WNDCLASSEX wc;
 wc.hbrBackGround=(HBRUSH)(COLOR_WINDOW + 2);

Edited 6 Years Ago by tkud: n/a

The window class hbrBackGround feature only effects the background of the main window, how do I implement that in my code? Do I used the hInstance of the main window? And how do I link a window class with the window, as there is no WNDCLASSEX parameter within CreateWindowEx(). Could you give me a example?

~G

Edited 6 Years Ago by Graphix: n/a

You have to process the WM_CTLCOLORSTATIC message in the parent window (WM_CTLCOLOREDIT should you change the edit box style so it is neither disabled nor read-only). It is sent by the control to its parent before being drawn, passing its device context in the WPARAM, so you can change text/background color in the message handler.

The code would be something like this:

case WM_CTLCOLORSTATIC:
{
	HDC hEdit = (HDC)wParam;

	SetTextColor( hEdit, RGB(0, 0, 0) );
	SetBkColor  ( hEdit, RGB(255, 255, 255) );

	// Do not return a brush created by CreateSolidBrush(...) because you'll get a memory leak
	return (INT_PTR)GetStockObject( WHITE_BRUSH );
}

The return value is the brush used to paint the background. Also, the MSDN documentation is incorrect, the brush is not deleted by the system (at least not on Vista/7) so if you need a custom color brush, create it once and delete it when the window closes.

Bear in mind that the system overrides the text color of disabled controls, so you wouldn't be able to change it this way. Instead, you can specify ES_READONLY when creating the edit box (the text will be selectable, though).

Comments
Thanks for the solution!
This question has already been answered. Start a new discussion instead.