use*_*739 6 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);
Run Code Online (Sandbox Code Playgroud)
我想设置子窗口“child”的颜色。如果我什么都不做,默认颜色是灰色的。我怎样才能设置它的颜色?我想永久保持黑色,无论如何都要改变。
创建所需颜色的画笔,然后在调用注册窗口类时将其传递到结构hbrBackground成员中。WNDCLASSRegisterClass
当你调用时系统会自动删除这个画笔UnregisterClass,所以一旦你把这个画笔传递给RegisterClass你,你就可以忘记它,并且不要尝试自己删除它。
小智 5
这个例子可能会有所帮助:
//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;
}
Run Code Online (Sandbox Code Playgroud)