win32窗口程序基本框架

普通版:

收集一些常用框架…

Windows 窗体应用程序是在用户计算机上运行的客户端应用程序,可显示信息、请求用户输入以及通过网络与远程计算机进行通信。在可以使用时,可能需要研究 .NET Framework 和它所提供的类。

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <Windows.h>
#include <tchar.h>

HWND hButton1, hButton2, hButton3, hButton4, hText1;
HINSTANCE g_hInstance;

LRESULT CALLBACK WndProc(
HWND hWnd,
UINT uMessage,
WPARAM wParam,
LPARAM lParam
);

int WINAPI _tWinMain(
HINSTANCE hInstance, //实例句柄
HINSTANCE hPreInstance,//没有用了
LPTSTR szCmd, //命令行参数,和WinMain要对应
int nShow //命令行参数
) {
g_hInstance = hInstance;

//1 设计窗口类
WNDCLASS wc;
wc.style = CS_VREDRAW | CS_HREDRAW; //类风格
wc.lpfnWndProc = WndProc; //回调函数的地址(重要)
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance; //实例句柄,代表此程序
wc.hIcon = 0;
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = L"WNDCLASS";

//2 注册窗口类
RegisterClass(&wc);

//3 创建窗口
HWND hWnd = CreateWindow(
L"WNDCLASS", //类名
L"窗口", //窗口名
WS_OVERLAPPEDWINDOW,//重叠窗口风格
100, 100, 430, 350, //位置和大小
NULL, //父窗口句柄
NULL, //菜单句柄
hInstance, //实例句柄
NULL //附加信息
);

//4 更新显示窗口
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);

//5 实现消息循环
MSG msg = {};
while (GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}

LRESULT CALLBACK WndProc(
HWND hWnd,
UINT uMessage,
WPARAM wParam,
LPARAM lParam
) {
switch (uMessage)
{
case WM_CREATE:
{
//WM_CREATE的lParam参数携带的是创建窗口时的信息。
LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam;
hButton1 = CreateWindow(
_T("button"), //创建按钮
_T("获取内容"), //窗口名
BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE, //创建一个按钮
// | 子窗口
// | 窗口可见
100, 100, 80, 30,
hWnd,
(HMENU)0x1001, //控件ID
pcs->hInstance, //实例句柄
0
);
hButton2 = CreateWindow(
_T("button"),
_T("修改内容"),
BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE,
190, 100, 80, 30,
hWnd,
(HMENU)0x1002,
pcs->hInstance,
0
);
hButton3 = CreateWindow(
_T("button"),
_T("移动"),
BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE,
100, 180, 80, 30,
hWnd,
(HMENU)0x1003,
pcs->hInstance,
0
);
hButton4 = CreateWindow(
_T("button"),
_T("隐藏"),
BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE,
190, 180, 80, 30,
hWnd,
(HMENU)0x1004,
pcs->hInstance,
0
);
hText1 = CreateWindow(
_T("edit"), //文本框
_T(""),
WS_BORDER | WS_CHILD | WS_VISIBLE,//环绕
100, 140, 160, 30,
hWnd,
(HMENU)0x2001,
pcs->hInstance,
0
);
}
break;
case WM_COMMAND:
{
int nCode = HIWORD(wParam);
int nId = LOWORD(wParam);
switch (nId)
{
case 0x1001:
if (nCode == BN_CLICKED)
{
TCHAR buf[100] = {};
GetWindowText(hText1, buf, 100);
SetWindowText(hText1, buf);
MessageBox(0, buf, L"文本框", 0);
}
break;
case 0x1002:
{
if (nCode == BN_CLICKED)
{
TCHAR buf[100] = {};
int nNum = 0;
GetWindowText(hText1, buf, 100);
_stscanf_s(buf, _T("%d"), &nNum);
nNum++;
_stprintf_s(buf, _T("%d"), nNum);
SetWindowText(hText1, buf);
}
}
break;
case 0x1003:
{
static int n1 = 0;
if (n1 == 0) {
MoveWindow(hButton3, 100, 220, 80, 30, TRUE);
n1 = 1;
}
else if (n1 == 1) {
MoveWindow(hButton3, 100, 180, 80, 30, TRUE);
n1 = 0;
}
}
break;
case 0x1004:
{
static int n2 = 0;

if (n2 == 0)
{
ShowWindow(hText1, SW_HIDE);
n2 = 1;
}
else
{
ShowWindow(hText1, SW_SHOW);
n2 = 0;
}
}
break;
default:
break;
}
}
break;
case WM_CLOSE:
//窗口关闭消息
PostQuitMessage(0);
break;
case WM_DESTROY:
//窗口销毁消息
break;
default:
break;
}
return DefWindowProc(hWnd, uMessage, wParam, lParam);
}

资源版:

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <Windows.h>
#include <tchar.h>
#include "resource.h"

HWND hButton1, hButton2, hButton3, hButton4, hText1;
HINSTANCE g_hInstance;

LRESULT CALLBACK WndProc(
HWND hWnd,
UINT uMessage,
WPARAM wParam,
LPARAM lParam
);

int WINAPI _tWinMain(
HINSTANCE hInstance, //实例句柄
HINSTANCE hPreInstance,//没有用了
LPTSTR szCmd, //命令行参数,和WinMain要对应
int nShow //命令行参数
) {
g_hInstance = hInstance;

//1 设计窗口类
WNDCLASS wc;
wc.style = CS_VREDRAW | CS_HREDRAW; //类风格
wc.lpfnWndProc = WndProc; //回调函数的地址(重要)
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance; //实例句柄,代表此程序
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
wc.hCursor = LoadCursor(hInstance, MAKEINTRESOURCE(IDC_CURSOR1));
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
wc.lpszClassName = L"WNDCLASS";

//2 注册窗口类
RegisterClass(&wc);

//3 创建窗口
HWND hWnd = CreateWindow(
L"WNDCLASS", //类名
L"窗口", //窗口名
WS_OVERLAPPEDWINDOW,//重叠窗口风格
100, 100, 430, 350, //位置和大小
NULL, //父窗口句柄
NULL, //菜单句柄
hInstance, //实例句柄
NULL //附加信息
);

//4 更新显示窗口
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);

//5 实现消息循环
MSG msg = {};
while (GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}

LRESULT CALLBACK WndProc(
HWND hWnd,
UINT uMessage,
WPARAM wParam,
LPARAM lParam
) {
switch (uMessage)
{
case WM_CREATE:
{
//WM_CREATE的lParam参数携带的是创建窗口时的信息。
LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam;
hButton1 = CreateWindow(
_T("button"), //创建按钮
_T("获取内容"), //窗口名
BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE, //创建一个按钮
// | 子窗口
// | 窗口可见
100, 100, 80, 30,
hWnd,
(HMENU)0x1001, //控件ID
pcs->hInstance, //实例句柄
0
);
hButton2 = CreateWindow(
_T("button"),
_T("修改内容"),
BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE,
190, 100, 80, 30,
hWnd,
(HMENU)0x1002,
pcs->hInstance,
0
);
hButton3 = CreateWindow(
_T("button"),
_T("移动"),
BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE,
100, 180, 80, 30,
hWnd,
(HMENU)0x1003,
pcs->hInstance,
0
);
hButton4 = CreateWindow(
_T("button"),
_T("隐藏"),
BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE,
190, 180, 80, 30,
hWnd,
(HMENU)0x1004,
pcs->hInstance,
0
);
hText1 = CreateWindow(
_T("edit"), //文本框
_T(""),
WS_BORDER | WS_CHILD | WS_VISIBLE,//环绕
100, 140, 160, 30,
hWnd,
(HMENU)0x2001,
pcs->hInstance,
0
);
}
break;
case WM_COMMAND:
{
int nCode = HIWORD(wParam);
int nId = LOWORD(wParam);
switch (nId)
{
case 0x1001:
if (nCode == BN_CLICKED)
{
TCHAR buf[100] = {};
GetWindowText(hText1, buf, 100);
SetWindowText(hText1, buf);
MessageBox(0, buf, L"文本框", 0);
}
break;
case 0x1002:
{
if (nCode == BN_CLICKED)
{
TCHAR buf[100] = {};
int nNum = 0;
GetWindowText(hText1, buf, 100);
_stscanf_s(buf, _T("%d"), &nNum);
nNum++;
_stprintf_s(buf, _T("%d"), nNum);
SetWindowText(hText1, buf);
}
}
break;
case 0x1003:
{
static int n1 = 0;
if (n1 == 0) {
MoveWindow(hButton3, 100, 220, 80, 30, TRUE);
n1 = 1;
}
else if (n1 == 1) {
MoveWindow(hButton3, 100, 180, 80, 30, TRUE);
n1 = 0;
}
}
break;
case 0x1004:
{
static int n2 = 0;

if (n2 == 0)
{
ShowWindow(hText1, SW_HIDE);
n2 = 1;
}
else
{
ShowWindow(hText1, SW_SHOW);
n2 = 0;
}
}
break;
default:
break;
}
}
break;
case WM_RBUTTONDOWN://鼠标右键单击
{
//这个坐标是相对于窗口的坐标
int x = LOWORD(lParam);
int y = HIWORD(lParam);
//弹出的时候,使用是相对于屏幕的坐标
//需要做一个转换
POINT pt = { x,y };
ClientToScreen(hWnd, &pt);

//弹出菜单
HMENU hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_MENU1));
//获取下拉菜单的句柄
HMENU hSubMenu0 = GetSubMenu(hMenu, 0);
HMENU hSubMenu1 = GetSubMenu(hMenu, 1);
TrackPopupMenu(
hSubMenu1, //要弹出的菜单的句柄
TPM_LEFTALIGN, //对齐方式等属性
pt.x, pt.y, //弹出的坐标
0, //保留的参数 ,没有用
hWnd, //菜单的响应窗口是谁
NULL
);
}break;
case WM_CLOSE:
//窗口关闭消息
PostQuitMessage(0);
break;
case WM_DESTROY:
//窗口销毁消息
break;
default:
break;
}
return DefWindowProc(hWnd, uMessage, wParam, lParam);
}

对话框版:

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
#include <Windows.h>
#include <tchar.h>
#include "resource.h"

BOOL CALLBACK DlgProc(
HWND hWnd,
UINT uMessage,
WPARAM wParam,
LPARAM lParam
);

int WINAPI _tWinMain(
HINSTANCE hInstance, //实例句柄
HINSTANCE hPreInstance,//没有用了
LPTSTR szCmd, //命令行参数,和WinMain要对应
int nShow //命令行参数
) {
//模态
DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)DlgProc);

//非模态需要自己维护消息
HWND hWnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)DlgProc);
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
MSG msg = { 0 };
while (GetMessage(&msg, 0, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return 0;
}

BOOL CALLBACK DlgProc(
HWND hWnd,
UINT uMessage,
WPARAM wParam,
LPARAM lParam
) {
switch (uMessage)
{
case WM_INITDIALOG:
return true;
break;
case WM_CLOSE:
EndDialog(hWnd, wParam);
break;
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
default:
break;
}
}
break;
default:
break;
}
return 0;
}