在createwindow(...)之后,如何给窗口一个颜色?

时间:2012-04-08 14:37:52

标签: c++ window background-color createwindow

我创建了一个句柄为handle_parent的窗口。然后我创建了一个子窗口,如下所示:

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);

我想设置子窗口“child”的颜色。如果我什么都不做,默认颜色为灰色。 我怎么设置它的颜色?我希望将颜色保持为黑色永久性,随时更改。

3 个答案:

答案 0 :(得分:4)

创建所需颜色的画笔,然后在调用WNDCLASS时将其传递到RegisterClass结构的hbrBackground成员中以注册您的窗口类。

当您致电UnregisterClass时,系统会自动删除此画笔,因此一旦您将此画笔传递给RegisterClass,您就可以忘记所有相关内容,不得尝试自行删除它。

答案 1 :(得分:2)

此示例可能会有所帮助:

//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;
}

答案 2 :(得分:0)

使用CreateSolidBrush()::

WNDCLASS wc = {0}(或WNDCLASS wc; memset(& wc,0,sizeof(wc));) ... wc.hbrBackground = CreateSolidBrush(RGB(255,0,0))或CreateSolidBrush(0x000000ff); //红色窗口类背景