[WINAPI] Set Gui Background Color

Hello! I'am currently developing an lua-guiframework. It will be based on the WinApi. Creating Winodws, Buttons etc ist already done. Only problem: I want do add a funktion like: GuiSetBkColor(hwnd,color). I didn't found any funktion to do that. Anyone got a clue how to resolve it ^.^?
(Insert your windowsclass here).hbrBackground = CreateSolidBrush(RGB(100, 100, 100));

You can specify your background color before registering the windows class. Or you can do it dynamically by waiting for a WM_ERASEBKGND to get passed a long with a handle to the windows device context in the wparam for everytime the window has to repaint it's background.

Helpful resource
Last edited on
If you want to change the background color of an individual, generic window on the fly you will need to go the WM_ERASEBKGND route, with the window class hbrBackground = NULL. You can change hbrBackground after creating a window, using SetClassLongPtr() with GCLP_HBRBACKGROUND, but it will change the background of all windows of that class; not just the window corresponding to the HWND you pass to SetClassLongPtr().

But if the window is one of the older standard controls (Edit, Button, ...), you should probably be handling WM_CTLCOLOR* messages (WM_CTLCOLOREDIT, WM_CTLCOLORSTATIC, etc.) One gotcha is that read-only and disabled edit controls send WM_CTLCOLORSTATIC rather than WM_CTLCOLOREDIT. And a bigger gotcha is that WM_CTLCOLORBTN does not work for pushbuttons (just for checkboxes, radio buttons, etc); if you want to customize the colors of a pushbutton you will need to use the owner draw mechanism (see WM_DRAWITEM, etc.) See the MSDN entries for these messages for more info, esp. the remarks.

The common controls (List View, Rebar, etc) also have their own mechanisms. Some of them have a specific message, e.g. LVM_SETBKCOLOR for List View, RB_SETBKCOLOR for a Rebar, But others, like the Header control, require you to handle the NM_CUSTOMDRAW notification.

Handling the pushbutton will prob be the most involved individual bit of this work. But the other issue is that most of the customization mechanisms rely on the parent window doing work for the control. Assuming you're using some sort of class hierarchy (this is a C++ forum!), if you want (e.g.) the Edit control class instance to handle the WM_CTLCOLOREDIT message rather than its parent then you will have to follow MFC's example and implement some form of "message reflection" mechanism (which sends the relevant messages on from the parent class to the correct child window.) You will also need to subclass the standard controls.

Sadly there is no such thing as a generic WM_SETBKCOLOR...


PS About MFC message reflection:

TN062: Message Reflection for Windows Controls

Last edited on
Topic archived. No new replies allowed.