After createwindow(...), how to give the window a color?

user565739 picture user565739 · Apr 8, 2012 · Viewed 9.9k times · Source

I have created a window whose handle is handle_parent. Then I created a child window as following:

hwnd_child = CreateWindow(child_class_name, _T(""),
WS_CHILDWINDOW, 0, 0, 0, 0, hwnd_parent, (HMENU)0, ghinst, NULL);
ShowWindow(win->hwndSplitterBar, SW_SHOW);
UpdateWindow(win->hwndSplitterBar);

I would like to set the color of child window "child". If I do nothing, the color is grey by default. How could I set its color? I would like to keep the color as black permanent, do change in anycase.

Answer

David Heffernan picture David Heffernan · Apr 8, 2012

Create a brush of the desired color and then pass it in the hbrBackground member of the WNDCLASS struct when calling RegisterClass to register your window class.

The system will delete this brush automatically when you call UnregisterClass so once you have passed this brush to RegisterClass you can forget all about it and must not attempt to delete it yourself.