-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.cpp
230 lines (206 loc) · 6.82 KB
/
window.cpp
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
230
// window.cpp : 定义应用程序的入口点。
//
#include "framework.h"
#include "window.h"
#include "windowsx.h"
#include "Resource.h"
#define MAX_LOADSTRING 100
#pragma comment(lib,"msimg32.lib")
// 全局变量:
HINSTANCE hInst; // 当前实例
WCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
WCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此处放置代码。
// 初始化全局字符串
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_WINDOW, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDB_FAN));
MSG msg;
// 主消息循环:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// 函数: MyRegisterClass()
//
// 目标: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDB_TB));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WINDOW);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
//
// 函数: InitInstance(HINSTANCE, int)
//
// 目标: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // 将实例句柄存储在全局变量中
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目标: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int page = 0;
static HINSTANCE hInst;
HDC hdc, hdcMem;
HBITMAP hBitMap;
BITMAP bm;
PAINTSTRUCT ps;
RECT rect;
GetClientRect(hWnd, &rect);
int client_width = (rect.right - rect.left);
int client_height = (rect.bottom - rect.top);
switch (message){
case WM_CREATE:
hInst = ((LPCREATESTRUCT)lParam)->hInstance;
break;
case WM_LBUTTONDOWN:
//以下检测区域
POINT pt;
pt.x = GET_X_LPARAM(lParam);
pt.y = GET_Y_LPARAM(lParam);
if (page == 0 || page == 9 || page == 10) {
int tmp = 0;
if (page == 9) tmp = 10;
else if (page == 10) tmp = 12;
if (pt.y > client_height / 2) page = 1 + tmp;
else page = 2 + tmp;
}else if (page == 1 || page == 2) {
int tmp = 0;
if (page == 2) tmp = 4;
if (pt.y > client_height * 3 / 4) page = 3 + tmp;
else if (pt.y > client_height / 2) page = 4 + tmp;
else if (pt.y > client_height / 4) page = 5 + tmp;
else page = 6 + tmp;
}else if (page == 8) page = 15;
SendMessage(hWnd, WM_PAINT, 0, 0);
InvalidateRect(hWnd, &rect, false);
break;
case WM_KEYDOWN:
if (wParam == VK_ESCAPE) {
if (page == 15) page = 8;
else if (page >= 13) page = 10;
else if (page >= 11) page = 9;
else if (page >= 7) page = 2;
else if (page >= 3) page = 1;
else page = 0;
}
SendMessage(hWnd, WM_PAINT, 0, 0);
InvalidateRect(hWnd, &rect, false);
break;
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
int res;
switch (page) {
case 0: res = IDB_INDEX; break;
case 1: res = IDB_NORTH; break;
case 2: res = IDB_SOUTH; break;
case 3: res = IDB_N3; break;
case 4: res = IDB_N4; break;
case 5: res = IDB_N5; break;
case 6: res = IDB_N6; break;
case 7: res = IDB_S1; break;
case 8: res = IDB_S2; break;
case 9: res = IDB_S3; break;
case 10: res = IDB_S4; break;
case 11: res = IDB_S3_2; break;
case 12: res = IDB_S3_1; break;
case 13: res = IDB_S4_2; break;
case 14: res = IDB_S4_1; break;
case 15: res = IDB_S2_1; break;
default: res = IDB_DEFAULT;
}
hBitMap = LoadBitmap(hInst, MAKEINTRESOURCE(res));
hdc = BeginPaint(hWnd, &ps);
hdcMem = CreateCompatibleDC(hdc);
SetStretchBltMode(hdc, COLORONCOLOR);
SelectObject(hdcMem, hBitMap);
GetObject(hBitMap, sizeof(BITMAP), &bm);
StretchBlt(hdc, 0, 0, client_width, client_height, hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
DeleteObject(hBitMap);
// 释放内存DC
DeleteDC(hdcMem);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}