/
KalScope.cpp
337 lines (290 loc) · 8.41 KB
/
KalScope.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// KalScope.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "KalScope.h"
#define MAX_LOADSTRING 100
// 全局变量:
ULONG_PTR gdiplusToken;
HWND ghDlg, ghWnd;
int gnCmdShow;
HINSTANCE hInst; // 当前实例
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK Splash(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
GdiplusStartupInput gdiplusStartupInput;
// Initialize GDI+.
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
srand((unsigned int)time(NULL));
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
ghDlg = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_SPLASH), 0, Splash);
ShowWindow(ghDlg, SW_SHOW);
std::thread* t = new std::thread(init_table);
t->detach();
// TODO: 在此放置代码。
MSG msg;
HACCEL hAccelTable;
// 初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_KALSCOPE, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_KALSCOPE));
// 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX 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(IDI_KALSCOPE));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_KALSCOPE);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
//
// 函数: InitInstance(HINSTANCE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // 将实例句柄存储在全局变量中
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, 450, 450, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ghWnd = hWnd;
gnCmdShow = nCmdShow;
return TRUE;
}
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case ID_HELP_SPLASHSCREEN:
ghDlg = CreateDialog(hInst, MAKEINTRESOURCE(IDD_SPLASH), 0, Splash);
ShowWindow(ghDlg, SW_SHOW);
break;
case ID_CONTROL_YIELD:
if (!eval_null()){
MessageBoxA(0, "You could only yield before a game start.", "Yield", 0);
break;
}
mainboard[7][7] = 1;
paint_board(hWnd);
break;
case ID_CONTROL_RESTART:
clear_board(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:{
Graphics *myGraphics;
Pen *myPen;
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意绘图代码...
myGraphics = new Graphics(hdc);
myPen = new Pen(Color(255, 0, 0, 0), 1);
myGraphics->DrawRectangle(myPen, Rect(400, 20, 20, 20));
for (int i = 0; i < 375; i += 25)
myGraphics->DrawLine(myPen, 20, 20 + i, 370, 20 + i);
for (int i = 0; i < 375; i += 25)
myGraphics->DrawLine(myPen, 20 + i, 20, 20 + i, 370);
delete myGraphics;
delete myPen;
EndPaint(hWnd, &ps);
paint_board(hWnd);
}
break;
case WM_DESTROY:
GdiplusShutdown(gdiplusToken);
PostQuitMessage(0);
break;
case WM_LBUTTONDOWN:
board_clicked(hWnd, LOWORD(lParam), HIWORD(lParam));
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
SetDlgItemTextA(hDlg, ID_CODENAME, codename_str );
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDURL && HIWORD(wParam) == BN_CLICKED)
{
ShellExecute(NULL, TEXT("open"), TEXT("http://aean.net/"), NULL, NULL, SW_SHOWNORMAL);
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
else if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
BLENDFUNCTION m_Blend;
size_t splashcount=0;
void drawsplash(HWND hDlg, char* str){
m_Blend.BlendOp = AC_SRC_OVER; //theonlyBlendOpdefinedinWindows2000
m_Blend.BlendFlags = 0; //nothingelseisspecial...
m_Blend.AlphaFormat = AC_SRC_ALPHA; //...
m_Blend.SourceConstantAlpha = 255;//AC_SRC_ALPHA
static Image *m_pImageBack;
if(!m_pImageBack) ImageFromIDResource(IDB_PNG1, L"PNG", m_pImageBack);
//----绘制
PAINTSTRUCT ps;
HDC hdcTemp = BeginPaint(hDlg, &ps);
HDC hMemDC = CreateCompatibleDC(hdcTemp);
RECT rct;
GetWindowRect(hDlg, &rct);
HBITMAP hBitMap = CreateCompatibleBitmap(hdcTemp, rct.right - rct.left, rct.bottom - rct.top);
HBITMAP hBmpOld = (HBITMAP)SelectObject(hMemDC, hBitMap);
HDC hdcScreen = GetDC(hDlg);
POINT ptWinPos = { rct.left, rct.top };
Graphics imageGraphics(hMemDC);
Point points[] = { Point(0, 0),
Point(400, 0),
Point(0, 255) };
Point points2[] = { Point(50, 50),
Point(450, 50),
Point(50, 305) };
// 设置层次窗口
DWORD dwExStyle = GetWindowLong(hDlg, GWL_EXSTYLE);
if ((dwExStyle & 0x80000) != 0x80000){
SetWindowLong(hDlg, GWL_EXSTYLE, dwExStyle ^ 0x80000);
}
POINT ptSrc = { 0, 0 };
SIZE sizeWindow = { rct.right - rct.left, rct.bottom - rct.top };
// 完成透明不规则窗口的绘制
imageGraphics.DrawImage(m_pImageBack, points, 3);
WCHAR* drawString = (WCHAR*)calloc(256, sizeof(WCHAR));
MultiByteToWideChar(CP_ACP, 0, str, (int)strlen(str), drawString, 255);
// Create font and brush.
Font* drawFont = new Font(L"Tahoma", 8);
SolidBrush* drawBrush = new SolidBrush(Color::White);
// Create point for upper-left corner of drawing.
PointF drawPoint = PointF(210.0F, 240.0F);
// Draw string to screen.
imageGraphics.DrawString(drawString, -1, drawFont, drawPoint, drawBrush);
delete drawFont;
delete drawBrush;
UpdateLayeredWindow(hDlg, hdcScreen, &ptWinPos, &sizeWindow, hMemDC, &ptSrc, 255, &m_Blend, ULW_ALPHA);
// 释放空间
EndPaint(hDlg, &ps);
}
// Splash消息处理程序。
INT_PTR CALLBACK Splash(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
SetTimer(hDlg, 1, 60, NULL);
drawsplash(hDlg, " ");
return (INT_PTR)TRUE;
case WM_PAINT:
return (INT_PTR)TRUE;
case WM_TIMER:
if (splashcount == 0){
drawsplash(hDlg, init_str);
if (init_finished){
splashcount++;
KillTimer(hDlg, 1);
ShowWindow(ghWnd, gnCmdShow);
UpdateWindow(ghWnd);
DestroyWindow(hDlg);
}
}
else KillTimer(hDlg, 1);
return (INT_PTR)TRUE;
case WM_LBUTTONDOWN:
if (splashcount > 0){
KillTimer(hDlg, 1);
DestroyWindow(hDlg);
}
return (INT_PTR)TRUE;
}
return (INT_PTR)FALSE;
}