![](/img/trans.png)
[英]How to create custom background color for window created with createwindow
[英]After createwindow(...), how to give the window a color?
我创建了一个 window,它的句柄是 handle_parent。 然后我创建了一个孩子 window 如下:
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);
我想设置孩子 window “孩子”的颜色。 如果我什么都不做,颜色默认为灰色。 我该如何设置它的颜色? 我想保持黑色永久颜色,无论如何都要改变。
创建所需颜色的画笔,然后在调用RegisterClass
注册您的 window class 时将其传递到WNDCLASS
结构的hbrBackground
成员中。
当你调用UnregisterClass
时,系统会自动删除这个画笔,所以一旦你将这个画笔传递给RegisterClass
,你就可以忘记它,不要试图自己删除它。
这个例子可能会有所帮助:
//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;
}
使用 CreateSolidBrush()::
WNDCLASS wc = { 0 } ( 或 WNDCLASS wc; memset(&wc, 0, sizeof(wc)); )... wc.hbrBackground = CreateSolidBrush(RGB(255,0,0)) 或 CreateSolidBrush(0x000000ff); // 红色 window class 背景
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.