-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
704 lines (614 loc) · 23.3 KB
/
Copy pathMain.cpp
File metadata and controls
704 lines (614 loc) · 23.3 KB
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
// Copyright 2026 Focus <scutjiaodian@outlook.com>
//
// Win32 主窗口入口,包含窗口初始化、消息循环、布局管理、菜单/工具栏创建
// 以及键盘快捷键和文件对话框处理。
#ifndef NOMINMAX
#define NOMINMAX
#endif
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <commdlg.h>
#include <shellscalingapi.h>
#include <psapi.h>
#include <string>
#include <sstream>
#pragma comment(lib, "comctl32.lib")
#pragma comment(lib, "comdlg32.lib")
#pragma comment(lib, "Shcore.lib")
#pragma comment(lib, "psapi.lib")
// 前置声明
#include "Canvas.h"
#include "UIManager.h"
#include "LayerManager.h"
// 全局常量
static const wchar_t* APP_TITLE = L"曲线编辑器 by Focus";
static const wchar_t* WND_CLASS = L"CurveEditorWnd";
// 布局尺寸(运行时 DPI 缩放)
static int TOOLBAR_H = 44;
static int LAYER_W = 180;
static int PROPANEL_W = 240;
static int STATUSBAR_H = 24;
// 状态栏常量
static const int SBT_MEM_PART_W = 260; // 右侧信息区域原始宽度
// 控件 ID
static const int ID_TOOLBAR = 100;
static const int ID_CANVAS = 101;
static const int ID_PROPANEL = 102;
static const int ID_STATUSBAR = 103;
// 命令 ID
static const int CMD_NEW = 201;
static const int CMD_OPEN = 202;
static const int CMD_SAVE_SVG = 203;
static const int CMD_EXPORT_PNG = 204;
static const int CMD_SAVE_CRV = 206;
static const int CMD_OPEN_CRV = 207;
static const int CMD_EXIT = 205;
static const int CMD_TOOL_DEL = 212;
static const int CMD_UNDO = 220;
static const int CMD_REDO = 221;
// 应用状态
struct AppState {
HWND hwndMain = nullptr;
HWND hwndToolbar = nullptr;
HWND hwndStatus = nullptr;
Canvas* canvas = nullptr;
UIManager* ui = nullptr;
LayerManager* layers = nullptr;
};
static AppState g_app;
// 全局 DPI 缩放因子
double g_dpiScale = 1.0;
// 菜单创建
static HMENU CreateAppMenu()
{
HMENU hMenu = CreateMenu();
HMENU hFile = CreatePopupMenu();
AppendMenuW(hFile, MF_STRING, CMD_NEW, L"新建\tCtrl+N");
AppendMenuW(hFile, MF_STRING, CMD_OPEN, L"打开 SVG\tCtrl+O");
AppendMenuW(hFile, MF_STRING, CMD_OPEN_CRV, L"打开 CRV\tCtrl+R");
AppendMenuW(hFile, MF_SEPARATOR, 0, nullptr);
AppendMenuW(hFile, MF_STRING, CMD_SAVE_SVG, L"保存为 SVG\tCtrl+S");
AppendMenuW(hFile, MF_STRING, CMD_EXPORT_PNG, L"导出为 PNG\tCtrl+E");
AppendMenuW(hFile, MF_STRING, CMD_SAVE_CRV, L"保存为 CRV\tCtrl+D");
AppendMenuW(hFile, MF_SEPARATOR, 0, nullptr);
AppendMenuW(hFile, MF_STRING, CMD_EXIT, L"退出\tAlt+F4");
HMENU hEdit = CreatePopupMenu();
AppendMenuW(hEdit, MF_STRING, CMD_UNDO, L"撤销\tCtrl+Z");
AppendMenuW(hEdit, MF_STRING, CMD_REDO, L"重做\tCtrl+Y");
AppendMenuW(hMenu, MF_POPUP, (UINT_PTR)hFile, L"文件");
AppendMenuW(hMenu, MF_POPUP, (UINT_PTR)hEdit, L"编辑");
return hMenu;
}
// 工具栏创建(纯文本按钮)
static HWND CreateToolbar(HWND hParent)
{
HWND hTb = CreateWindowExW(
0, TOOLBARCLASSNAMEW, nullptr,
WS_CHILD | WS_VISIBLE | TBSTYLE_FLAT | TBSTYLE_TOOLTIPS | CCS_NODIVIDER | CCS_NORESIZE,
0, 0, 0, TOOLBAR_H,
hParent, (HMENU)(UINT_PTR)ID_TOOLBAR, GetModuleHandleW(nullptr), nullptr);
SendMessage(hTb, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
SendMessage(hTb, TB_SETBITMAPSIZE, 0, MAKELPARAM(0, 0));
SendMessage(hTb, TB_SETBUTTONSIZE, 0, MAKELPARAM(72, TOOLBAR_H - 4));
// 纯文本按钮
auto addBtn = [&](int id, const wchar_t* text, bool isCheck = false) {
TBBUTTON btn{};
btn.iBitmap = I_IMAGENONE;
btn.idCommand= id;
btn.fsState = TBSTATE_ENABLED;
btn.fsStyle = (BYTE)(TBSTYLE_BUTTON | (isCheck ? TBSTYLE_CHECK : 0));
btn.iString = (INT_PTR)text;
SendMessage(hTb, TB_ADDBUTTONS, 1, (LPARAM)&btn);
};
auto addSep = [&]() {
TBBUTTON sep{}; sep.fsStyle = TBSTYLE_SEP;
SendMessage(hTb, TB_ADDBUTTONS, 1, (LPARAM)&sep);
};
addBtn(CMD_NEW, L"新建");
addBtn(CMD_SAVE_SVG, L"保存SVG");
addBtn(CMD_EXPORT_PNG, L"导出PNG");
addSep();
addBtn(CMD_TOOL_SEL, L"选择", true);
addBtn(CMD_TOOL_PEN, L"贝塞尔", true);
addBtn(CMD_TOOL_NURBS, L"NURBS", true);
addBtn(CMD_TOOL_DEL, L"删除点");
addSep();
addBtn(CMD_UNDO, L"撤销");
addBtn(CMD_REDO, L"重做");
SendMessage(hTb, TB_AUTOSIZE, 0, 0);
return hTb;
}
// 自绘状态栏
static LRESULT CALLBACK StatusBarProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
// 缓存的内存信息(每 2 秒刷新)
static struct {
double usedMB = 0.0;
double totalMB = 0.0;
double pct = 0.0;
} s_mem;
// 缓存的字体和双缓冲位图
static HFONT s_font = nullptr;
static HDC s_memDC = nullptr;
static HBITMAP s_memBmp = nullptr;
static HBITMAP s_oldBmp = nullptr;
static int s_cx = 0, s_cy = 0;
switch (msg)
{
case WM_CREATE:
SetTimer(hWnd, 1, 2000, nullptr);
s_font = CreateFontW(-(int)(11 * g_dpiScale + 0.5), 0, 0, 0,
FW_NORMAL, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Segoe UI");
return 0;
case WM_DESTROY:
KillTimer(hWnd, 1);
if (s_memDC) {
SelectObject(s_memDC, s_oldBmp);
DeleteObject(s_memBmp);
DeleteDC(s_memDC);
}
if (s_font) DeleteObject(s_font);
return 0;
case WM_SIZE:
s_cx = 0; // 标记重建缓冲区
InvalidateRect(hWnd, nullptr, FALSE);
return 0;
case WM_TIMER: {
// 查询系统内存信息并缓存
PROCESS_MEMORY_COUNTERS pmc;
pmc.cb = sizeof(pmc);
GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));
MEMORYSTATUSEX ms;
ms.dwLength = sizeof(ms);
GlobalMemoryStatusEx(&ms);
SIZE_T used = pmc.WorkingSetSize;
DWORDLONG total = ms.ullTotalPhys;
s_mem.usedMB = used / (1024.0 * 1024.0);
s_mem.totalMB = total / (1024.0 * 1024.0);
s_mem.pct = (total > 0) ? (double)used / (double)total : 0.0;
if (s_mem.pct > 1.0) s_mem.pct = 1.0;
InvalidateRect(hWnd, nullptr, FALSE);
return 0;
}
case WM_ERASEBKGND:
return 1;
case WM_SETTEXT: {
// 仅在文本变化时重绘,避免无谓刷新
wchar_t old[256] = {};
GetWindowTextW(hWnd, old, 255);
LRESULT ret = DefWindowProcW(hWnd, msg, wp, lp);
if (wcscmp(old, (const wchar_t*)lp) != 0)
InvalidateRect(hWnd, nullptr, FALSE);
return ret;
}
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
RECT rc; GetClientRect(hWnd, &rc);
int w = rc.right, h = rc.bottom;
// 窗口大小变化时重建双缓冲
if (s_cx != w || s_cy != h) {
if (s_memDC) {
SelectObject(s_memDC, s_oldBmp);
DeleteObject(s_memBmp);
DeleteDC(s_memDC);
}
s_memDC = CreateCompatibleDC(hdc);
s_memBmp = CreateCompatibleBitmap(hdc, w, h);
s_oldBmp = (HBITMAP)SelectObject(s_memDC, s_memBmp);
s_cx = w; s_cy = h;
}
auto SC2 = [](int v) { return (int)(v * g_dpiScale + 0.5); };
// 绘制背景
HBRUSH bgBr = CreateSolidBrush(RGB(240, 240, 240));
FillRect(s_memDC, &rc, bgBr);
DeleteObject(bgBr);
// 内存信息(缓存值)
double usedMB = s_mem.usedMB;
double totalMB = s_mem.totalMB;
double pct = s_mem.pct;
// 右侧信息区域坐标
int memW = SC2(SBT_MEM_PART_W);
int memX = w - memW;
// 分隔线
HPEN sepPen = CreatePen(PS_SOLID, 1, RGB(200, 200, 200));
HPEN oldSep = (HPEN)SelectObject(s_memDC, sepPen);
MoveToEx(s_memDC, memX, SC2(3), nullptr);
LineTo(s_memDC, memX, h - SC2(3));
SelectObject(s_memDC, oldSep);
DeleteObject(sepPen);
// 内存使用进度条
int barW = SC2(60), barH = SC2(10);
int barX = memX + SC2(8), barY = (h - barH) / 2;
RECT barBg = { barX, barY, barX + barW, barY + barH };
HPEN bPen = CreatePen(PS_SOLID, 1, RGB(180, 180, 180));
HPEN oPen = (HPEN)SelectObject(s_memDC, bPen);
SelectObject(s_memDC, GetStockObject(WHITE_BRUSH));
Rectangle(s_memDC, barBg.left, barBg.top, barBg.right, barBg.bottom);
int fillW = (int)((barW - 2) * pct);
if (fillW > 0) {
COLORREF barC;
// 三段式颜色:绿(<50%)、黄(50~75%)、红(>75%)
if (pct < 0.5) barC = RGB(100, 200, 100);
else if (pct < 0.75) barC = RGB(220, 200, 60);
else barC = RGB(220, 80, 80);
HBRUSH fBr = CreateSolidBrush(barC);
RECT fRc = { barX + 1, barY + 1, barX + 1 + fillW, barY + barH - 1 };
FillRect(s_memDC, &fRc, fBr);
DeleteObject(fBr);
}
SelectObject(s_memDC, oPen);
DeleteObject(bPen);
// 内存文本(使用缓存字体)
wchar_t memTxt[64];
swprintf_s(memTxt, L"%.1fMB / %.0fMB (%.0f%%)", usedMB, totalMB, pct * 100.0);
HFONT oldF = (HFONT)SelectObject(s_memDC, s_font);
SetBkMode(s_memDC, TRANSPARENT);
SetTextColor(s_memDC, RGB(80, 80, 80));
RECT memRc = { barX + barW + SC2(6), 0, memX + memW, h };
DrawTextW(s_memDC, memTxt, -1, &memRc, DT_VCENTER | DT_SINGLELINE | DT_LEFT | DT_END_ELLIPSIS);
// 状态文本(左侧区域)
wchar_t statusTxt[256] = {};
GetWindowTextW(hWnd, statusTxt, 255);
if (statusTxt[0]) {
SetTextColor(s_memDC, RGB(80, 80, 80));
RECT stRc = { SC2(6), 0, memX - SC2(4), h };
DrawTextW(s_memDC, statusTxt, -1, &stRc, DT_VCENTER | DT_SINGLELINE | DT_LEFT | DT_END_ELLIPSIS);
}
SelectObject(s_memDC, oldF);
// 窗口大小调整手柄
RECT gripRc = { w - SC2(16), h - SC2(16), w, h };
DrawFrameControl(s_memDC, &gripRc, DFC_SCROLL, DFCS_SCROLLSIZEGRIP);
// 输出到屏幕(保留缓冲区)
BitBlt(hdc, 0, 0, w, h, s_memDC, 0, 0, SRCCOPY);
EndPaint(hWnd, &ps);
return 0;
}
}
return DefWindowProcW(hWnd, msg, wp, lp);
}
// 层级布局计算:工具栏、画布、图层面板、属性面板和状态栏。
static void LayoutChildren(HWND hWnd)
{
RECT rc;
GetClientRect(hWnd, &rc);
int W = rc.right;
int H = rc.bottom;
int canvasX = LAYER_W;
int canvasY = TOOLBAR_H;
int canvasW = W - LAYER_W - PROPANEL_W;
int canvasH = H - TOOLBAR_H - STATUSBAR_H;
if (canvasW < 1) canvasW = 1;
if (canvasH < 1) canvasH = 1;
// 工具栏:撑满顶部宽度
if (g_app.hwndToolbar)
SetWindowPos(g_app.hwndToolbar, nullptr,
0, 0, W, TOOLBAR_H, SWP_NOZORDER | SWP_NOACTIVATE);
// 画布
if (g_app.canvas)
g_app.canvas->Resize(canvasX, canvasY, canvasW, canvasH);
// 图层面板
if (g_app.layers)
g_app.layers->Resize(0, TOOLBAR_H, LAYER_W, canvasH);
// 属性面板
if (g_app.ui)
g_app.ui->Resize(W - PROPANEL_W, TOOLBAR_H, PROPANEL_W, canvasH);
// 状态栏
if (g_app.hwndStatus)
SetWindowPos(g_app.hwndStatus, nullptr,
0, H - STATUSBAR_H, W, STATUSBAR_H, SWP_NOZORDER | SWP_NOACTIVATE);
}
// 由 Canvas/UIManager 调用的状态栏文本更新函数。
void SetStatusText(const wchar_t* text)
{
if (!g_app.hwndStatus) return;
static std::wstring s_last;
if (s_last == text) return; // 内容未变则跳过,避免闪烁
s_last = text;
SetWindowTextW(g_app.hwndStatus, text);
}
// 文件保存对话框(SVG/PNG 共用)。
static bool ShowSaveDialog(HWND hOwner, bool png, std::wstring& outPath)
{
wchar_t buf[MAX_PATH] = {};
OPENFILENAMEW ofn{};
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hOwner;
ofn.lpstrFile = buf;
ofn.nMaxFile = MAX_PATH;
if (png) {
ofn.lpstrFilter = L"PNG 图像\0*.png\0所有文件\0*.*\0";
ofn.lpstrDefExt = L"png";
} else {
ofn.lpstrFilter = L"SVG 文件\0*.svg\0所有文件\0*.*\0";
ofn.lpstrDefExt = L"svg";
}
ofn.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
if (GetSaveFileNameW(&ofn)) { outPath = buf; return true; }
return false;
}
// 文件打开对话框。
static bool ShowOpenDialog(HWND hOwner, std::wstring& outPath)
{
wchar_t buf[MAX_PATH] = {};
OPENFILENAMEW ofn{};
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hOwner;
ofn.lpstrFile = buf;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFilter = L"SVG 文件\0*.svg\0所有文件\0*.*\0";
ofn.lpstrDefExt = L"svg";
ofn.Flags = OFN_FILEMUSTEXIST;
if (GetOpenFileNameW(&ofn)) { outPath = buf; return true; }
return false;
}
// 主窗口消息处理函数。
static LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
{
INITCOMMONCONTROLSEX icc{ sizeof(icc), ICC_BAR_CLASSES };
InitCommonControlsEx(&icc);
g_app.hwndToolbar = CreateToolbar(hWnd);
// 自绘状态栏,显示状态文本和内存使用进度条。
{
static bool s_reg = false;
if (!s_reg) {
WNDCLASSEXW wc{};
wc.cbSize = sizeof(wc);
wc.style = 0;
wc.lpfnWndProc = StatusBarProc;
wc.hInstance = GetModuleHandleW(nullptr);
wc.hbrBackground = nullptr;
wc.lpszClassName = L"CurveStatusBar";
wc.hCursor = LoadCursorW(nullptr, MAKEINTRESOURCEW(IDC_ARROW));
RegisterClassExW(&wc);
s_reg = true;
}
}
g_app.hwndStatus = CreateWindowExW(
0, L"CurveStatusBar", L"就绪",
WS_CHILD | WS_VISIBLE,
0, 0, 0, STATUSBAR_H,
hWnd, (HMENU)(UINT_PTR)ID_STATUSBAR,
GetModuleHandleW(nullptr), nullptr);
// 创建画布
g_app.canvas = new Canvas(hWnd);
// 创建图层面板
g_app.layers = new LayerManager(hWnd, g_app.canvas);
g_app.canvas->layerManager = g_app.layers;
// 创建属性面板
g_app.ui = new UIManager(hWnd, g_app.canvas);
g_app.canvas->uiManager = g_app.ui;
LayoutChildren(hWnd);
// 默认选中钢笔工具
SendMessage(g_app.hwndToolbar, TB_CHECKBUTTON, CMD_TOOL_PEN, TRUE);
g_app.canvas->SetTool(Tool::Pen);
return 0;
}
case WM_DESTROY:
delete g_app.layers;
delete g_app.ui;
delete g_app.canvas;
PostQuitMessage(0);
return 0;
case WM_SIZE:
LayoutChildren(hWnd);
return 0;
case WM_COMMAND:
{
int id = LOWORD(wParam);
switch (id)
{
case CMD_NEW:
if (MessageBoxW(hWnd, L"新建画布将清空当前内容,继续?",
L"新建", MB_YESNO | MB_ICONQUESTION) == IDYES)
g_app.canvas->NewDocument();
break;
case CMD_OPEN: {
std::wstring path;
if (ShowOpenDialog(hWnd, path))
g_app.canvas->LoadSVG(path);
break;
}
case CMD_SAVE_SVG: {
std::wstring path;
if (ShowSaveDialog(hWnd, false, path))
g_app.canvas->SaveSVG(path);
break;
}
case CMD_EXPORT_PNG: {
std::wstring path;
if (ShowSaveDialog(hWnd, true, path))
g_app.canvas->ExportPNG(path);
break;
}
case CMD_SAVE_CRV: {
std::wstring path;
OPENFILENAMEW ofn{};
wchar_t buf[MAX_PATH] = {};
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWnd;
ofn.lpstrFile = buf;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFilter = L"CRV 文件\0*.crv\0所有文件\0*.*\0";
ofn.lpstrDefExt = L"crv";
ofn.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
if (GetSaveFileNameW(&ofn)) {
g_app.canvas->SaveCRV(buf);
}
break;
}
case CMD_OPEN_CRV: {
std::wstring path;
OPENFILENAMEW ofn{};
wchar_t buf[MAX_PATH] = {};
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWnd;
ofn.lpstrFile = buf;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFilter = L"CRV 文件\0*.crv\0所有文件\0*.*\0";
ofn.lpstrDefExt = L"crv";
ofn.Flags = OFN_FILEMUSTEXIST;
if (GetOpenFileNameW(&ofn)) {
g_app.canvas->LoadCRV(buf);
}
break;
}
case CMD_EXIT:
DestroyWindow(hWnd);
break;
case CMD_TOOL_SEL:
SendMessage(g_app.hwndToolbar, TB_CHECKBUTTON, CMD_TOOL_PEN, FALSE);
SendMessage(g_app.hwndToolbar, TB_CHECKBUTTON, CMD_TOOL_NURBS, FALSE);
SendMessage(g_app.hwndToolbar, TB_CHECKBUTTON, CMD_TOOL_SEL, TRUE);
g_app.canvas->SetTool(Tool::Select);
break;
case CMD_TOOL_PEN:
SendMessage(g_app.hwndToolbar, TB_CHECKBUTTON, CMD_TOOL_SEL, FALSE);
SendMessage(g_app.hwndToolbar, TB_CHECKBUTTON, CMD_TOOL_NURBS, FALSE);
SendMessage(g_app.hwndToolbar, TB_CHECKBUTTON, CMD_TOOL_PEN, TRUE);
g_app.canvas->SetTool(Tool::Pen);
break;
case CMD_TOOL_NURBS:
SendMessage(g_app.hwndToolbar, TB_CHECKBUTTON, CMD_TOOL_SEL, FALSE);
SendMessage(g_app.hwndToolbar, TB_CHECKBUTTON, CMD_TOOL_PEN, FALSE);
SendMessage(g_app.hwndToolbar, TB_CHECKBUTTON, CMD_TOOL_NURBS, TRUE);
g_app.canvas->SetTool(Tool::NURBS);
break;
case CMD_TOOL_DEL:
g_app.canvas->DeleteSelectedPoint();
break;
case CMD_UNDO:
g_app.canvas->Undo();
break;
case CMD_REDO:
g_app.canvas->Redo();
break;
}
// 将属性面板的控件通知转发给 UIManager
if (g_app.ui)
g_app.ui->HandleCommand(wParam, lParam);
return 0;
}
// 键盘快捷键(Ctrl+Z/Y/S/E/D/R/N 和 Escape/Delete)
case WM_KEYDOWN:
if (GetKeyState(VK_CONTROL) & 0x8000) {
switch (wParam) {
case 'Z': g_app.canvas->Undo(); break;
case 'Y': g_app.canvas->Redo(); break;
case 'S': { std::wstring p; if(ShowSaveDialog(hWnd,false,p)) g_app.canvas->SaveSVG(p); } break;
case 'E': { std::wstring p; if(ShowSaveDialog(hWnd,true, p)) g_app.canvas->ExportPNG(p); } break;
case 'D': { std::wstring p; OPENFILENAMEW ofn{}; wchar_t b[MAX_PATH]={}; ofn.lStructSize=sizeof(ofn); ofn.hwndOwner=hWnd; ofn.lpstrFile=b; ofn.nMaxFile=MAX_PATH; ofn.lpstrFilter=L"CRV\0*.crv\0", ofn.lpstrDefExt=L"crv"; ofn.Flags=OFN_PATHMUSTEXIST|OFN_OVERWRITEPROMPT; if(GetSaveFileNameW(&ofn)) g_app.canvas->SaveCRV(b); } break;
case 'R': { std::wstring p; OPENFILENAMEW ofn{}; wchar_t b[MAX_PATH]={}; ofn.lStructSize=sizeof(ofn); ofn.hwndOwner=hWnd; ofn.lpstrFile=b; ofn.nMaxFile=MAX_PATH; ofn.lpstrFilter=L"CRV\0*.crv\0"; ofn.lpstrDefExt=L"crv"; ofn.Flags=OFN_FILEMUSTEXIST; if(GetOpenFileNameW(&ofn)) g_app.canvas->LoadCRV(b); } break;
case 'N': SendMessage(hWnd, WM_COMMAND, CMD_NEW, 0); break;
}
}
if (wParam == VK_ESCAPE) g_app.canvas->CancelEdit();
if (wParam == VK_DELETE || wParam == VK_BACK) g_app.canvas->DeleteSelectedPoint();
return 0;
case WM_ERASEBKGND:
{
HDC hdc = (HDC)wParam;
RECT rc;
GetClientRect(hWnd, &rc);
HBRUSH br = CreateSolidBrush(RGB(240, 240, 240));
FillRect(hdc, &rc, br);
DeleteObject(br);
return 1;
}
// DPI 变化时更新缩放因子和布局尺寸
case WM_DPICHANGED: {
RECT* prcNew = (RECT*)lParam;
SetWindowPos(hWnd, nullptr,
prcNew->left, prcNew->top,
prcNew->right - prcNew->left,
prcNew->bottom - prcNew->top,
SWP_NOZORDER | SWP_NOACTIVATE);
HDC hdc = GetDC(hWnd);
if (hdc) {
g_dpiScale = GetDeviceCaps(hdc, LOGPIXELSX) / 96.0;
ReleaseDC(hWnd, hdc);
}
TOOLBAR_H = (int)(44 * g_dpiScale + 0.5);
LAYER_W = (int)(180 * g_dpiScale + 0.5);
PROPANEL_W = (int)(240 * g_dpiScale + 0.5);
STATUSBAR_H = (int)(24 * g_dpiScale + 0.5);
// 通知 Canvas 重新创建 D2D 资源
if (g_app.canvas)
g_app.canvas->OnDPIChanged();
// 重新布局所有子窗口
LayoutChildren(hWnd);
return 0;
}
// 状态栏自适应高度
case WM_SIZE + 1:
SendMessage(g_app.hwndStatus, WM_SIZE, 0, 0);
return 0;
default:
return DefWindowProcW(hWnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, LPWSTR, int nCmdShow)
{
SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);
HDC hdc = GetDC(nullptr);
g_dpiScale = GetDeviceCaps(hdc, LOGPIXELSX) / 96.0;
ReleaseDC(nullptr, hdc);
TOOLBAR_H = (int)(44 * g_dpiScale + 0.5);
LAYER_W = (int)(180 * g_dpiScale + 0.5);
PROPANEL_W = (int)(240 * g_dpiScale + 0.5);
STATUSBAR_H = (int)(24 * g_dpiScale + 0.5);
// 注册主窗口类
WNDCLASSEXW wc{};
wc.cbSize = sizeof(wc);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wc.lpfnWndProc = MainWndProc;
wc.hInstance = hInst;
wc.hCursor = LoadCursorW(nullptr, MAKEINTRESOURCEW(IDC_ARROW));
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = nullptr;
wc.lpszClassName = WND_CLASS;
wc.hIcon = LoadIconW(nullptr, MAKEINTRESOURCEW(IDI_APPLICATION));
wc.hIconSm = LoadIconW(nullptr, MAKEINTRESOURCEW(IDI_APPLICATION));
RegisterClassExW(&wc);
// 设置菜单
HMENU hMenu = CreateAppMenu();
// 创建主窗口
HWND hWnd = CreateWindowExW(
WS_EX_APPWINDOW,
WND_CLASS, APP_TITLE,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 1280, 800,
nullptr, hMenu, hInst, nullptr);
if (!hWnd) return -1;
g_app.hwndMain = hWnd;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// 消息循环,附带快捷键加速键表
ACCEL accel[] = {
{ FCONTROL | FVIRTKEY, 'Z', CMD_UNDO },
{ FCONTROL | FVIRTKEY, 'Y', CMD_REDO },
{ FCONTROL | FVIRTKEY, 'S', CMD_SAVE_SVG },
{ FCONTROL | FVIRTKEY, 'N', CMD_NEW },
{ FCONTROL | FVIRTKEY, 'D', CMD_SAVE_CRV },
{ FCONTROL | FVIRTKEY, 'R', CMD_OPEN_CRV },
};
HACCEL hAccel = CreateAcceleratorTableW(accel, ARRAYSIZE(accel));
MSG msg{};
while (GetMessageW(&msg, nullptr, 0, 0)) {
if (!TranslateAcceleratorW(hWnd, hAccel, &msg)) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
DestroyAcceleratorTable(hAccel);
return (int)msg.wParam;
}