學習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);
}
學習windows編程 day6 之處理鼠標移動