【发布时间】:2015-11-28 20:27:39
【问题描述】:
我用C++写过用WinApi画的程序。 我的回调函数:
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage(0); /* send a WM_QUIT to the message queue */
break;
case WM_ERASEBKGND:
{
elWidget *widget = (elWidget *)GetWindowLong(hwnd, GWL_USERDATA);
if (widget)
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
HBRUSH hBrush = CreateSolidBrush(widget->color.ColorRef());
FillRect((HDC)wParam, &ps.rcPaint, hBrush);
DeleteObject(hBrush);
EndPaint(hwnd, &ps);
}
}
break;
default: /* for messages that we don't deal with */
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
它适用于独立窗口(样式为WS_OVERLAPPED),但当样式为WS_CHILD 或WS_CHILD | WS_VISIBLE 时,ps.rcPaint 始终为 (0,0,0,0)。不知道怎么解决。
elButton::elButton(elWidget *owner)
: elWidget(owner)
{
WNDCLASSEX winclChild; /* Data structure for the windowclass */
/* The Window structure */
winclChild.hInstance = gThisInstance; //global variable instance
winclChild.lpszClassName = L"Child";
winclChild.lpfnWndProc = WindowProcedure; /* This function is called by windows */
winclChild.style = CS_DBLCLKS; /* Catch double-clicks */
winclChild.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
winclChild.hIcon = LoadIcon (NULL, IDI_APPLICATION);
winclChild.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
winclChild.hCursor = LoadCursor (NULL, IDC_ARROW);
winclChild.lpszMenuName = NULL; /* No menu */
winclChild.cbClsExtra = 0; /* No extra bytes after the window class */
winclChild.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
winclChild.hbrBackground = 0;// CreateSolidBrush(RGB(255, 200, 200));//(HBRUSH)COLOR_WINDOW;//COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx(&winclChild))
return;
hwnd = CreateWindowEx(
0, /* Extended possibilites for variation */
L"Child", /* Classname */
L"Title", /* Title Text */
WS_CHILD | WS_VISIBLE,
100,
100,
40,
40,
owner->getHwnd(), /* The window is a child-window to desktop */
NULL, /* No menu */
gThisInstance, /* Program Instance handler */
this /* to lParam */
);
SetWindowLong(hwnd, GWL_USERDATA, (long)this);
}
我可以在 Google Disk 上添加指向整个项目的链接,但我不能保证它会永久保存多年。
【问题讨论】:
-
随机猜测是您忘记设置 CS_VREDRAW | CS_HREDRAW 类样式标志。您需要显示调用 RegisterWindow/Ex() 和 CreateWindow/Ex() 的代码。
-
WNDCLASSEX winclChild; winclChild.style = CS_DBLCLKS if (!RegisterClassEx(&winclChild)) 返回; hwnd = CreateWindowEx(WS_CHILD | WS_VISIBLE,
-
显示minimal reproducible example 这样我们就不必以这种方式从您那里提取代码不是更好吗?
-
第一个答案:但两个窗口都是红色的;如果我们应用“unsigned style = GetWindowLong(hWnd, GWL_STYLE); if ((style & WS_CHILD) != 0) FillRect((HDC)wParam, &ps.rcPaint, hBrush);”两者都是白色的,否则是红色的
-
@Saku:我已经更新了我的答案。
标签: c++ winapi visual-studio-2013 gdi