我已经用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磁盘上添加到整个项目的链接,但是我不能保证它会永久存在多年。
最佳答案
只能在WM_PAINT上调用BeginPaint,
要获取WM_ERASEBKGND上的剪辑框,请调用GetClipBox((HDC)wParam,&rect);