windows编程消息处理

1

2

3

4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include<Windows.h>
#include<iostream>
int a = 1;

const UINT WM_USERMSG = WM_USER + 1; //自定义消息事件
void print(LPCWSTR format, ...) {
WCHAR wchar_buff[100]{ 0 };
va_list arglist;
va_start(arglist, format);
wvsprintfW(wchar_buff, format, arglist);
va_end(arglist);
OutputDebugStringW(wchar_buff);

}

LRESULT CALLBACK WindowProc( //消息处理函数
_In_ HWND hwnd,
_In_ UINT uMsg,
_In_ WPARAM wParam,
_In_ LPARAM lParam
) {
switch (uMsg)
{
case WM_CREATE:
MessageBoxW(hwnd, L"窗口创建了", L"提示", MB_OK);
break;
case WM_CLOSE:
MessageBoxW(hwnd, L"窗口关闭了", L"提示", MB_OK);
DestroyWindow(hwnd);
PostQuitMessage(0);
break;
case WM_MOUSEMOVE:
//print(L"鼠标移动了!/n,%d",a);
//a++;
{
WORD X = LOWORD(lParam);
WORD Y = HIWORD(lParam);
print(L"鼠标移动了!X坐标%d,Y坐标%d", X, Y);
break;
}
case WM_LBUTTONDOWN:
print(L"鼠标左键按下了/n");
SendMessageW(hwnd, WM_USERMSG, 0, 0);
case WM_USERMSG:
print(L"自定义消息!/n");
break;
default:
break;
}
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}

int WINAPI WinMain(
_In_ HINSTANCE hInstance, // handle to current instance
_In_opt_ HINSTANCE hPrevInstance, // handle to previous instance
_In_ LPSTR lpCmdLine, // command line
_In_ int nCmdShow // show state
)
{
//创建一个窗口类
WNDCLASSW myClass = { 0 };
myClass.lpszClassName = L"51hook";
myClass.lpfnWndProc = WindowProc;
//2.注册窗口类
RegisterClassW(&myClass);
//3.创建窗口
HWND hwnd = CreateWindowW(
myClass.lpszClassName,
L"51hook",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
hInstance,
0
);

//4.显示窗口
ShowWindow(hwnd, SW_SHOWNORMAL);
//5.获取消息

MSG msg = { 0 };
while (GetMessageW(&msg,0,0,0)){
DispatchMessage(&msg);
}
return 0;
}

5

6

7


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!