Skip to content

Commit 59301c7

Browse files
committed
AboutGetMessage
At there, we can not catch WM_CLOSE
1 parent 25d789d commit 59301c7

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

Diff for: 0x01/MsgA.cpp

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// A program use test function GetMessage
2+
// Use the program, we can catch WM_LBUTTONDOWN but can not catch WM_CLOSE
3+
#include <Windows.h>
4+
#include <WindowsX.h>
5+
#include <stdio.h>
6+
7+
void CreateWindowsClass(WNDCLASSEX *pwinclass, HINSTANCE hInstance);
8+
9+
// 2.创建WinProc
10+
LRESULT CALLBACK WinProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
11+
12+
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hprevinstance, LPSTR lpCmdline, INT nCmdshow){
13+
WNDCLASSEX winclass;
14+
HWND hwnd;
15+
MSG msg;
16+
17+
// 1.创建一个Windows类
18+
CreateWindowsClass(&winclass, hInstance);
19+
// 3.注册Windows类
20+
if(!RegisterClassEx(&winclass)){
21+
return 0;
22+
}
23+
// 4.创建窗口
24+
hwnd = CreateWindowExW(NULL, TEXT("winclass-MsgA"), TEXT("Msg TestA"),
25+
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
26+
0, 0, 480, 320, NULL, NULL, hInstance, NULL);
27+
if(!hwnd){
28+
return 0;
29+
}
30+
31+
while(GetMessage(&msg, NULL, 0, 0)){
32+
if(msg.message == WM_CLOSE){
33+
MessageBoxA(NULL, "I catch WM_CLOSE!", "NULL", MB_OK);
34+
}else if(msg.message == WM_LBUTTONDOWN){
35+
MessageBoxA(NULL, "I catch other Message WM_LBUTTONDOWN!", "NULL", MB_OK);
36+
}else{
37+
;
38+
}
39+
40+
TranslateMessage(&msg);
41+
DispatchMessage(&msg);
42+
}
43+
44+
return msg.wParam;
45+
}
46+
47+
void CreateWindowsClass(WNDCLASSEX *pwinclass, HINSTANCE hInstance){
48+
pwinclass->cbSize = sizeof(WNDCLASSEX);
49+
pwinclass->style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
50+
pwinclass->lpfnWndProc = WinProc;
51+
pwinclass->cbClsExtra = 0;
52+
pwinclass->cbWndExtra = 0;
53+
pwinclass->hInstance = hInstance;
54+
pwinclass->hIcon = LoadIcon(NULL, IDI_APPLICATION);
55+
pwinclass->hCursor = LoadCursor(NULL, IDC_ARROW);
56+
pwinclass->hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
57+
pwinclass->lpszMenuName = NULL;
58+
pwinclass->lpszClassName = TEXT("WinClass-MsgA");
59+
pwinclass->hIconSm = LoadIcon(NULL, IDI_APPLICATION);
60+
}
61+
62+
LRESULT CALLBACK WinProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam){
63+
PAINTSTRUCT ps;
64+
HDC hdc;
65+
66+
switch(Msg){
67+
case WM_CREATE:{
68+
return 0;
69+
}break;
70+
71+
case WM_PAINT:{
72+
hdc = BeginPaint(hWnd, &ps);
73+
EndPaint(hWnd, &ps);
74+
return 0;
75+
}break;
76+
77+
case WM_CLOSE:{
78+
;
79+
}break;
80+
81+
case WM_DESTROY:{
82+
PostQuitMessage(0);
83+
return 0;
84+
break;
85+
}
86+
87+
default:break;
88+
}
89+
90+
return DefWindowProc(hWnd, Msg, wParam, lParam);
91+
}

0 commit comments

Comments
 (0)