SDK编程关于设置无效区域的一个小问题
#include <strsafe.h>#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("MyWindows");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("这个程序需要在 Windows NT 才能执行!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName,
TEXT("Hello World!"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
static HRGN hRgn, hRgnTemp;
static int cxClient, cyClient;
static TCHAR szBuffer;
static size_t size;
int i;
switch (message)
{
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
hRgn = CreateRectRgn(cxClient / 4, cyClient / 4, cxClient * 3 / 4, cyClient * 3 / 4);
hRgnTemp = CreateRectRgn(cxClient / 4, cyClient / 4, cxClient / 2, cyClient * 3 / 4);
/*InvalidateRgn(hwnd, hRgn, FALSE);*/
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
SelectClipRgn(hdc, hRgnTemp);
StringCchPrintf(szBuffer, 20, TEXT("%d%d%d%d"), ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
StringCchLength(szBuffer, 20, &size);
MessageBox(NULL, szBuffer, TEXT("hello"), MB_OK);
for (i = 0; i < cxClient; i++)
{
MoveToEx(hdc, i * 10, 0, NULL);
LineTo(hdc, i * 10, cyClient);
}
/*FillRgn(hdc, hRgn, (HBRUSH)GetStockObject(BLACK_BRUSH));*/
InvalidateRgn(hwnd, hRgn, FALSE);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
在上面的代码里面我注释了WM_SIZE里面的InvalidateRgn函数。
运行结果是,在后面的弹窗中能正常显示无效区域的坐标。
如果注释掉WM_PAINT里面的InvalidateRgn函数,而把前面的注释取消掉,即每次在WM_SIZE里面设置无效区域。
每当客户区发生改变时,弹窗上显示的无效区域的坐标都是整个客户区,即每次放大、缩小,都重绘了整个客户区,似乎忽视了我设置的无效区域。
难道是当窗口大小发生改变时系统会强制重绘整个客户区吗?这个时候在WM_SIZE里面设置的无效区域实际上是没有用的?
无效区域的设置只在没有WM_SIZE消息时有效? 重绘和WM_SIZE没有什么关系.
CS_HREDRAW和CS_VREDRAW在这个标示下 窗口大小发生改变的时候会进行重绘;
一般你收到WM_SIZE消息的时候都是窗体发生了改变. 将会自动重绘整个客户区 ,你说的没有被用到就是这个原因,所以WM_SIZE 设置重绘函数没什么意义. wuyexinfei 发表于 2017-5-14 18:31
重绘和WM_SIZE没有什么关系.
CS_HREDRAW和CS_VREDRAW在这个标示下 窗口大小发生改变的时候会进行重绘;
...
谢谢,了解了。{:10_282:} 感谢楼主分享,涨姿势了 楼主解决了吗,加油 {:10_249:}{:10_249:}{:10_249:}{:10_249:}{:10_249:}{:10_249:}{:10_249:}{:10_249:}{:10_249:} 谢谢搂着的分享,谢谢喽。 0.0 {:5_101:} {:5_1013:}gffgjhjkjhljhj _(:3」∠)_ 才开始学的我一脸懵逼 {:10_266:} 学习
页:
[1]