每当窗口更改大小时,您可以通过为窗口类设置CS_HREDRAW和CS_VREDRAW样式,使应用程序重新绘制客户区域的所有内容。基于窗口大小调整绘图大小的应用程序使用这些样式,以确保在绘制时以空白的客户端区域开头。
在以下示例中,窗口过程绘制一个五角星,整齐地放在客户区中。它使用公共DC,并且必须在每次处理WM_PAINT消息时设置映射模式以及窗口和视口扩展数据块。
LRESULT APIENTRY WndProc(hwnd,message,wParam,lParam)
HWND hwnd;
UINT message;
WPARAM wParam;
LPARAM lParam;
{
PAINTSTRUCT ps;
HDC hdc;
RECT rc;
POINT aptStar[6] = {50,2, 2,98, 98,33, 2,33, 98,98, 50,2};
.
.
.
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rc);
SetMapMode(hdc, MM_ANISOTROPIC);
SetWindowExtEx(hdc, 100, 100, NULL);
SetViewportExtEx(hdc, rc.right, rc.bottom, NULL);
Polyline(hdc, aptStar, 6);
EndPaint(hwnd, &ps);
return 0L;
.
.
.
}
int APIENTRY WinMain(hInstance,hPrevInstance,lpCmdLine,nCmdShow)
HINSTANCE hInstance; /* handle of current instance */
HINSTANCE hPrevInstance; /* handle of previous instance */
LPSTR lpCmdLine; /* address of command line */
int nCmdShow; /* show-window type (open/icon) */
{
WNDCLASS wc;
.
.
.
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC) WndProc;
.
.
.
RegisterClass(&wc);
.
.
.
return msg.wParam;
}