Fork me on GitHub

10 Windows编程——鼠标消息

和鼠标相关的三个属于:click,double-click,drag

鼠标消息和键盘消息不同:只要鼠标跨越某个窗口,或者在某个窗口中按键,消息。

客户去鼠标消息

WM_MOUSEMOVE

WM_[L,M,R] BUTTONDOWN

WM_[L,M,R] BUTTONUP

WM_[L,M,R] BUTTONDBLCLK

对于这些消息,lParam的低字节为x坐标,高字节为y坐标。
wParam只是鼠标按键以及shift和Ctr1按键的状态(这个和键盘消息相似)。
要测试鼠标按钮是否按下:MK_LBUTTON,MK_MBUTTON,MK_RBUTTON,MK_SHIFT,MK_CONTROL.

非客户区鼠标消息:
WM_NCMOUS EMOVE
WM_NCLBUTTONDOWN.
WM_NCLBUTTONUP
WM_NCLBUTTONDBLCLK

 1 #include<Windows.h>
 2 #include<WinUser.h>
 3 #include<tchar.h>
 4 #include<stdio.h>
 5 
 6 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
 7 
 8 int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow)
 9 {
10     WNDCLASS WndClass;
11     TCHAR* ClassName = TEXT("MyClass");
12     HWND hwnd;
13     MSG msg;
14 
15     WndClass.cbClsExtra = 0;
16     WndClass.cbWndExtra = 0;
17     WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
18     WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
19     WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
20     WndClass.hInstance = hInst;
21     WndClass.lpfnWndProc = WindProc;
22     WndClass.lpszClassName = ClassName;
23     WndClass.lpszMenuName = NULL;
24     WndClass.style = CS_VREDRAW | CS_HREDRAW;
25 
26     RegisterClass(&WndClass);
27     hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 300, NULL, NULL, hInst, NULL);
28     ShowWindow(hwnd, nShow);
29     UpdateWindow(hwnd);
30 
31     while (GetMessage(&msg, NULL, 0, 0))
32     {
33         TranslateMessage(&msg);
34         DispatchMessage(&msg);
35     }
36     return 0;
37 }
38 
39 
40 
41 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
42 {
43     HDC hdc;
44     PAINTSTRUCT pt;
45     TCHAR *rectwords = TEXT("我在学习鼠标消息");
46     static int i;
47     switch (message)
48     {
49     case WM_CREATE:
50         return 0;
51     case WM_SIZE:
52         return 0;
53     case WM_PAINT:
54         hdc = BeginPaint(hwnd, &pt);
55         Rectangle(hdc, 10, 10, 100, 100);
56         if (i)
57         {
58             TextOut(hdc, 100, 10, rectwords, _tcslen(rectwords));
59         }
60         EndPaint(hwnd, &pt);
61         return 0;
62     case WM_CHAR:
63         return 0;
64     case WM_LBUTTONUP:
65         i = 0;
66         InvalidateRect(hwnd, NULL, TRUE);
67         return 0;
68     case WM_LBUTTONDOWN:
69         InvalidateRect(hwnd, NULL, TRUE);
70         if (LOWORD(lParam)>10 && LOWORD(lParam)<100 && HIWORD(lParam)>10 && HIWORD(lParam)<100)
71         {
72             i = 1;
73         }
74         return 0;
75     case WM_MOUSEMOVE:
76         hdc = GetDC(hwnd);
77         if (wParam&MK_LBUTTON)
78         {
79             SetPixel(hdc, LOWORD(lParam), HIWORD(lParam), 0);
80         }
81         ReleaseDC(hwnd, hdc);
82         return 0;
83     case WM_DESTROY:
84         PostQuitMessage(0);
85         return 0;
86     default:
87         break;
88     }
89 
90     return DefWindowProc(hwnd, message, wParam, lParam);
91 }
View Code

 

posted @ 2018-07-17 11:04  克拉默与矩阵  阅读(336)  评论(0编辑  收藏  举报