学习windows编程 day6 之处理鼠标移动

#define POINT_MAX 1000

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
static int cxClient, cyClient;
static POINT pt[POINT_MAX];
static int iCount;
int x, y;


switch (message)
{
case WM_LBUTTONDOWN:
iCount = 0;
InvalidateRect(hwnd, NULL, TRUE);
break;
case WM_MOUSEMOVE: //当处理完一个WM_MOUSEMOVE消息后,才会到系统消息队列中去取出下一个,所以,并不是鼠标一直移动就会一直产生WM_MOUSEMOVE消息在消息队列中,其中还可以产生其他消息
if (wParam&MK_LBUTTON && iCount<POINT_MAX)
{
pt[iCount].x = GET_X_LPARAM(lParam); //相比于LOWORD(lParam)更加可靠,在多屏显示时LOWORD(lParam)易出错
pt[iCount++].y = GET_Y_LPARAM(lParam);


hdc = GetDC(hwnd);
SetPixelV(hdc, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), 0);
ReleaseDC(hwnd, hdc);
}
break;
case WM_LBUTTONUP:
InvalidateRect(hwnd, NULL, FALSE);//不擦除背景,产生WM_PAINT消息
break;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);


SetCursor(LoadCursor(NULL, IDC_WAIT));
//Windows为鼠标光标保存了一个「显示计数」若是安装了鼠标,就会初始为0,否则为-1
//showcursor(true)会增加显示计数,当其非0 时会显示鼠标光标
//ShowCursor(TRUE);//1
for (x = 0; x < iCount;x++)
{
for (y = x+1; y < iCount;y++)
{
MoveToEx(hdc, pt[x].x, pt[x].y, NULL);
LineTo(hdc, pt[y].x, pt[y].y);
}
}
//减少显示计数
//ShowCursor(FALSE);//0
//使两个成对出现,才不会影响其他地方对鼠标的操作


SetCursor(LoadCursor(NULL, IDC_ARROW)); //切换两个备用指针
//设置后鼠标就会显示出样式,不需要showCursor(true)
//(在不移动鼠标情况下不会改变,当鼠标移动时会回复为原来光标图,是因为当产生WM_MOUSEOVE时,系统会自动将鼠标改为原来的(注册时设置的光标)


EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}


return DefWindowProc(hwnd, message, wParam, lParam);
}

 
上一篇:IDEA循环依赖报错解决方案


下一篇:项目Beta冲刺(团队6/7)