6

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.

user565739
  • 1,302
  • 4
  • 23
  • 46
  • How are you painting your child window? – David Heffernan Apr 08 '12 at 14:39
  • I don't paint it at all......Is there no direct way to do this, like something setbgcolor(hwnd_child)? I can't find such a direct way, so I came here. I don't know how to paint it, if I need to do this myself – user565739 Apr 08 '12 at 14:46

3 Answers3

5

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
5

This example may be helpful:

//Setting the background color of a window during window class registration
WNDCLASS wc = { 0 } ( or WNDCLASS wc; memset(&wc, 0, sizeof(wc)); )
...
...
...
wc.hbrBackground = CreateSolidBrush(0x000000ff); // a red window class background
...
...
RegisterClass(&wc);

// Setting the background during WM_ERASEBKGND
LRESULT CALLBACK YourWndProc(HWND hwnd, UINT umsg, WPARAM,LPARAM)
{
   switch( umsg )
   {
      case WM_ERASEBKGND:
      {
         RECT rc;
         GetClientRect(hwnd, &rc);
         SetBkColor((HDC)wParam, 0x000000ff); // red
         ExtTextOut((HDC)wParam, 0, 0, ETO_OPAQUE, &rc, 0, 0, 0);
         return 1;
      }
      // or in WM_PAINT
      case WM_PAINT:
      {
         PAINTSTRUCT ps;
         RECT rc;
         HDC hdc = BeginPaint(hwnd, &ps);
         GetClientRect(hwnd, &rc);
         SetBkColor(hdc, 0x000000ff); // red
         ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rc, 0, 0, 0);
         EndPaint(hwnd, &ps);
         break;
      }
      ...
      ...
      ...
      default:
         return DefWindowProc(...);
   }
   return 0;
}
Stanislav Mamontov
  • 1,734
  • 15
  • 21
Abet
  • 123
  • 1
  • 5
0

Use CreateSolidBrush()::

WNDCLASS wc = { 0 } ( or WNDCLASS wc; memset(&wc, 0, sizeof(wc)); ) ... wc.hbrBackground = CreateSolidBrush(RGB(255,0,0)) or CreateSolidBrush(0x000000ff); // a red window class background

Dpk
  • 1