1、第四讲鼠标第四讲:鼠标 只要鼠标跨越窗口,或者在某窗口中按下鼠标按钮,那么窗口过程函数就会收到鼠标消息,而不管该窗口是否为活动窗口和是否拥有输入焦点窗口。 鼠标消息:1. lParam是相对于客户区左上角的坐标,其中X坐标:LOWORD(lParam)Y坐标:HIWORD(lParam)2. wParam是Shift键和Ctrl键或鼠标按钮的状态,若wParam & MK_SHIFT0wParam & MK_CONTROL0wParam & MK_LBUTTON0wParam & MK_MBUTTON0wParam & MK_RBUTTON0表示在产生相应的鼠标消息时,也按下了Shift键和C
2、trl键或鼠标按钮。WM_LBUTTONDOWN WM_MBUTTONDOWN WM_RBUTTONDOWN WM_LBUTTONUPWM_RBUTTONUPWM_MBUTTONUPWM_LBUTTONDBLCLKWM_MBUTTONDBLCLKWM_RBUTTONDBLCLKWM_MOUSEMOVE 鼠标双击消息如果希望窗口过程函数能接受鼠标双击消息,那么在注册窗口类时,窗口风格应为:wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ; 重点:鼠标消息:WM_LBUTTONDOWNWM _MBUTTONDOWN WM_RBUTTOND
3、OWNWM_LBUTTONUPWM_RBUTTONUP WM_MBUTTONUPWM_LBUTTONDBLCLKWM_MBUTTONDBLCLKWM_RBUTTONDBLCLKWM_MOUSEMOVE子窗口风格:WS_CHILDWINDOW | WS_VISIBLE取程序实例句柄:(HINSTANCE)GetWindowLong (hwnd, GWL_HINSTANCE);函数MoveWindow (hwndChildxy, x * cxBlock, y * cyBlock, cxBlock, cyBlock, TRUE) ;移动窗口和改变窗口大小尺寸,产生WM_SIZE消息。存取预留在窗口额
4、外字节的函数:SetWindowLong (hwnd, 0, 0) ;GetWindowLong (hwnd, 0,0);设置窗口捕获鼠标函数:SetCapture(hwnd);一旦窗口hwnd被设置了捕获鼠标,不管鼠标光标是否在窗口hwnd的边界之内,窗口hwnd都接受鼠标输入。释放窗口捕获鼠标函数:ReleaseCapture();WM_MOUSEMOVE消息:每当鼠标移动时,窗口接收WM_MOUSEMOVE消息,系统此时自动把鼠标光标形状切换到在窗口类中定义的鼠标光标形状,如wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;/*- CONN
5、ECT.C - Connect-the-Dots Mouse Demo Program (c) Charles Petzold, 1998 -*/#include #define MAXPOINTS 1000LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) static TCHAR szAppName = TEXT (Connect) ; HWN
6、D 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)
7、; wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = NULL ; wndclass.lpszClassName = szAppName ; if (!RegisterClass (&wndclass) MessageBox (NULL, TEXT (Program requires Windows NT!), szAppName, MB_ICONERROR) ; return 0 ; hwnd = CreateWindow (szAppName, TEXT (Con
8、nect-the-Points Mouse Demo), 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 ;
9、LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) static POINT ptMAXPOINTS ; static int iCount ; HDC hdc ; int i, j ; PAINTSTRUCT ps ; switch (message) case WM_LBUTTONDOWN: iCount = 0 ; InvalidateRect (hwnd, NULL, TRUE) ; return 0 ; case WM_MOUSEMOVE: if (wParam & MK_L
10、BUTTON & iCount 1000) ptiCount .x = LOWORD (lParam) ; ptiCount+.y = HIWORD (lParam) ; hdc = GetDC (hwnd) ; /SetPixel (hdc, LOWORD (lParam), HIWORD (lParam), 0) ; SetPixel (hdc, LOWORD (lParam), HIWORD (lParam), RGB(255,0,0) ; ReleaseDC (hwnd, hdc) ; return 0 ; case WM_LBUTTONUP: InvalidateRect (hwnd
11、, NULL, FALSE) ; return 0 ; case WM_PAINT: hdc = BeginPaint (hwnd, &ps) ; SetCursor (LoadCursor (NULL, IDC_WAIT) ; ShowCursor (TRUE) ; for (i = 0 ; i iCount - 1 ; i+) for (j = i + 1 ; j iCount ; j+) MoveToEx (hdc, pti.x, pti.y, NULL) ; LineTo (hdc, ptj.x, ptj.y) ; ShowCursor (FALSE) ; SetCursor (Loa
12、dCursor (NULL, IDC_ARROW) ; EndPaint (hwnd, &ps) ; return 0 ; case WM_DESTROY: PostQuitMessage (0) ; return 0 ; return DefWindowProc (hwnd, message, wParam, lParam) ;/*- CHECKER1.C - Mouse Hit-Test Demo Program No. 1 (c) Charles Petzold, 1998 -*/#include #define DIVISIONS 5LRESULT CALLBACK WndProc (
13、HWND, UINT, WPARAM, LPARAM) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) static TCHAR szAppName = TEXT (Checker1) ; HWND hwnd ; MSG msg ; WNDCLASS wndclass ; wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbCls
14、Extra = 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 = szAppN
15、ame ; if (!RegisterClass (&wndclass) MessageBox (NULL, TEXT (Program requires Windows NT!), szAppName, MB_ICONERROR) ; return 0 ; hwnd = CreateWindow (szAppName, TEXT (Checker1 Mouse Hit-Test Demo), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstanc
16、e, 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) static BOOL fStateDIVISIONSDIVISIONS ; static int
17、 cxBlock, cyBlock ; HDC hdc ; int x, y ; PAINTSTRUCT ps ; RECT rect ; switch (message) case WM_SIZE : cxBlock = LOWORD (lParam) / DIVISIONS ; cyBlock = HIWORD (lParam) / DIVISIONS ; return 0 ; case WM_LBUTTONDOWN : x = LOWORD (lParam) / cxBlock ; y = HIWORD (lParam) / cyBlock ; if (x DIVISIONS & y D
18、IVISIONS) fState xy = 1 ; rect.left = x * cxBlock ; rect.top = y * cyBlock ; rect.right = (x + 1) * cxBlock ; rect.bottom = (y + 1) * cyBlock ; InvalidateRect (hwnd, &rect, FALSE) ; else MessageBeep (0) ; return 0 ; case WM_PAINT : hdc = BeginPaint (hwnd, &ps) ; for (x = 0 ; x DIVISIONS ; x+) for (y
19、 = 0 ; y DIVISIONS ; y+) Rectangle (hdc, x * cxBlock, y * cyBlock, (x + 1) * cxBlock, (y + 1) * cyBlock) ; if (fState xy) MoveToEx (hdc, x * cxBlock, y * cyBlock, NULL) ; LineTo (hdc, (x+1) * cxBlock, (y+1) * cyBlock) ; MoveToEx (hdc, x * cxBlock, (y+1) * cyBlock, NULL) ; LineTo (hdc, (x+1) * cxBloc
20、k, y * cyBlock) ; EndPaint (hwnd, &ps) ; return 0 ; case WM_DESTROY : PostQuitMessage (0) ; return 0 ; return DefWindowProc (hwnd, message, wParam, lParam) ;/*- CHECKER3.C - Mouse Hit-Test Demo Program No. 3 (c) Charles Petzold, 1998 -*/#include #define DIVISIONS 5LRESULT CALLBACK WndProc (HWND, UIN
21、T, WPARAM, LPARAM) ;LRESULT CALLBACK ChildWndProc (HWND, UINT, WPARAM, LPARAM) ;TCHAR szChildClass = TEXT (Checker3_Child) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) static TCHAR szAppName = TEXT (Checker3) ; HWND hwnd ; MSG msg ; WNDCLASS wndcla
22、ss ; 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
23、) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = NULL ; wndclass.lpszClassName = szAppName ; if (!RegisterClass (&wndclass) MessageBox (NULL, TEXT (Program requires Windows NT!), szAppName, MB_ICONERROR) ; return 0 ; wndclass.lpfnWndProc = ChildWndProc ; wndclass.cbWndExtra = sizeof (long) ;
24、 wndclass.hIcon = NULL ; wndclass.lpszClassName = szChildClass ; RegisterClass (&wndclass) ; hwnd = CreateWindow (szAppName, TEXT (Checker3 Mouse Hit-Test Demo), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ; ShowWindow (hwnd, iCmdShow
25、) ; 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) static HWND hwndChildDIVISIONSDIVISIONS ; int cxBlock, cyBlock, x, y ; switch (message
26、) case WM_CREATE : for (x = 0 ; x DIVISIONS ; x+) for (y = 0 ; y DIVISIONS ; y+) hwndChildxy = CreateWindow (szChildClass, NULL, WS_CHILDWINDOW | WS_VISIBLE, 0, 0, 0, 0, hwnd, (HMENU) (y 8 | x), (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE), NULL) ; return 0 ; case WM_SIZE : cxBlock = LOWORD (lPar
27、am) / DIVISIONS ; cyBlock = HIWORD (lParam) / DIVISIONS ; for (x = 0 ; x DIVISIONS ; x+) for (y = 0 ; y DIVISIONS ; y+) MoveWindow (hwndChildxy, x * cxBlock, y * cyBlock, cxBlock, cyBlock, TRUE) ; return 0 ; case WM_LBUTTONDOWN : MessageBeep (0) ; return 0 ; case WM_DESTROY : PostQuitMessage (0) ; r
28、eturn 0 ; return DefWindowProc (hwnd, message, wParam, lParam) ;LRESULT CALLBACK ChildWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) HDC hdc ; PAINTSTRUCT ps ; RECT rect ; switch (message) case WM_CREATE : SetWindowLong (hwnd, 0, 0) ; / on/off flag return 0 ; case WM_LBUTTONDOWN : SetWindowLong (hwnd, 0, 1 GetWindowLong (hwnd, 0) ; InvalidateRect (hwnd, NULL, FALSE) ; return 0 ; case WM_PAINT : hdc = BeginPaint (hwnd, &ps) ; GetClientRect (hwnd, &rect) ; Rectangle (hdc, 0, 0, rect.right, rect.bottom) ; if (GetWindowLong (hw
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1