-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsettings.h
More file actions
1412 lines (1260 loc) · 61.8 KB
/
Copy pathsettings.h
File metadata and controls
1412 lines (1260 loc) · 61.8 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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// settings.h — PinyinIME 设置系统
// 功能: 繁体/简体切换, 皮肤配色, 用户词典管理, 模糊音, 智能纠错等
// 使用纯 Win32 API (comctl32), 无额外依赖
#pragma once
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#include <commctrl.h>
#include <gdiplus.h>
#include <msctf.h>
#include <combaseapi.h>
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "comctl32.lib")
#pragma comment(lib, "comdlg32.lib")
#pragma comment(lib, "Advapi32.lib")
#include <string>
#include <vector>
#include <unordered_map>
#include <fstream>
#include <sstream>
#include <algorithm>
// ==================== UTF 转换工具函数 ====================
inline std::wstring utf8ToWide(const std::string& s) {
if (s.empty()) return L"";
int len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, s.c_str(), -1, nullptr, 0);
UINT codePage = CP_UTF8;
if (len <= 0) {
codePage = CP_ACP;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), -1, nullptr, 0);
}
if (len <= 0) return L"";
std::wstring result(len - 1, 0);
MultiByteToWideChar(codePage, 0, s.c_str(), -1, &result[0], len);
return result;
}
inline std::string wideToUtf8(const std::wstring& s) {
if (s.empty()) return "";
int len = WideCharToMultiByte(CP_UTF8, 0, s.c_str(), -1, nullptr, 0, nullptr, nullptr);
if (len <= 0) return "";
std::string result(len - 1, 0);
WideCharToMultiByte(CP_UTF8, 0, s.c_str(), -1, &result[0], len, nullptr, nullptr);
return result;
}
// ==================== 简繁转换 ====================
// 数据与转换函数统一在 s2t_data.h 中维护, 支持:
// - 单字级映射 (2000+ 对, 涵盖《简化字总表》全部)
// - 词汇级消歧映射 (一简对多繁, 如 发→發/髮)
// - 两岸 IT 词汇差异 (如 软件→軟體)
// - 最长匹配优先策略 (词级优先于字级)
#include "s2t_data.h"
// ==================== 设置数据结构 ====================
struct PinyinSettings {
// === 语言 ===
bool useTraditional = false;
// === 外观 (默认 亮色 墨绿主题) ===
COLORREF bgColor = RGB(0xF0, 0xF5, 0xF0); // 淡绿背景
COLORREF borderColor = RGB(0x96, 0xC6, 0x96); // 中绿边框
COLORREF textColor = RGB(0x28, 0x3D, 0x28); // 深绿文字
COLORREF indexColor = RGB(0x3C, 0x81, 0x3C); // 墨绿序号
COLORREF inputColor = RGB(0x32, 0x64, 0x32); // 墨绿输入
std::wstring fontName = L"Microsoft YaHei"; // 字体名称
int fontSize = 20; // 字体高度 (负值=像素)
int candidateCount = 5; // 候选词数量 5-9
bool verticalLayout = false; // 候选框竖排
// === 模糊音 ===
bool fuzzyZ_Zh = false;
bool fuzzyC_Ch = false;
bool fuzzyS_Sh = false;
bool fuzzyN_L = false;
bool fuzzyF_H = false;
bool fuzzyEn_Eng = false;
bool fuzzyIn_Ing = false;
// === 智能功能 ===
bool chinesePunctuation = true;
// === 快捷键 ===
DWORD toggleHotkey = VK_RSHIFT;
DWORD toggleModifier = 0; // 修饰键: 0=无, VK_MENU=Alt, VK_CONTROL=Ctrl
bool hasToggleModifierInFile = false; // 配置文件是否显式设置了修饰键
// 预设皮肤
static const struct Skin {
const wchar_t* name;
COLORREF bg, border, text, index, input;
} skins[];
static const int SKIN_COUNT = 9;
void applySkin(int idx) {
if (idx < 0 || idx >= SKIN_COUNT) return;
bgColor = skins[idx].bg;
borderColor = skins[idx].border;
textColor = skins[idx].text;
indexColor = skins[idx].index;
inputColor = skins[idx].input;
}
bool loadFromFile(const std::string& path) {
std::ifstream fin(path);
if (!fin.is_open()) return false;
std::string line;
while (std::getline(fin, line)) {
if (line.empty() || line[0] == '#') continue;
size_t eq = line.find('=');
if (eq == std::string::npos) continue;
std::string key = line.substr(0, eq);
std::string val = line.substr(eq + 1);
// trim
key.erase(0, key.find_first_not_of(" \t"));
key.erase(key.find_last_not_of(" \t") + 1);
val.erase(0, val.find_first_not_of(" \t"));
val.erase(val.find_last_not_of(" \t") + 1);
if (key == "useTraditional") useTraditional = (val == "1");
else if (key == "bgColor") bgColor = (COLORREF)std::stoul(val);
else if (key == "borderColor") borderColor = (COLORREF)std::stoul(val);
else if (key == "textColor") textColor = (COLORREF)std::stoul(val);
else if (key == "indexColor") indexColor = (COLORREF)std::stoul(val);
else if (key == "inputColor") inputColor = (COLORREF)std::stoul(val);
else if (key == "fontName") fontName = utf8ToWide(val);
else if (key == "fontSize") fontSize = std::stoi(val);
else if (key == "candidateCount") candidateCount = std::stoi(val);
else if (key == "verticalLayout") verticalLayout = (val == "1");
else if (key == "fuzzyZ_Zh") fuzzyZ_Zh = (val == "1");
else if (key == "fuzzyC_Ch") fuzzyC_Ch = (val == "1");
else if (key == "fuzzyS_Sh") fuzzyS_Sh = (val == "1");
else if (key == "fuzzyN_L") fuzzyN_L = (val == "1");
else if (key == "fuzzyF_H") fuzzyF_H = (val == "1");
else if (key == "fuzzyEn_Eng") fuzzyEn_Eng = (val == "1");
else if (key == "fuzzyIn_Ing") fuzzyIn_Ing = (val == "1");
else if (key == "chinesePunctuation") chinesePunctuation = (val == "1");
else if (key == "toggleHotkey") toggleHotkey = (DWORD)std::stoul(val);
else if (key == "toggleModifier") { toggleModifier = (DWORD)std::stoul(val); hasToggleModifierInFile = true; }
}
return true;
}
bool saveToFile(const std::string& path) {
std::ofstream fout(path);
if (!fout.is_open()) return false;
fout << "# PinyinIME Settings\n";
fout << "useTraditional=" << useTraditional << "\n";
fout << "bgColor=" << (unsigned long)bgColor << "\n";
fout << "borderColor=" << (unsigned long)borderColor << "\n";
fout << "textColor=" << (unsigned long)textColor << "\n";
fout << "indexColor=" << (unsigned long)indexColor << "\n";
fout << "inputColor=" << (unsigned long)inputColor << "\n";
fout << "fontName=" << wideToUtf8(fontName) << "\n";
fout << "fontSize=" << fontSize << "\n";
fout << "candidateCount=" << candidateCount << "\n";
fout << "verticalLayout=" << verticalLayout << "\n";
fout << "fuzzyZ_Zh=" << fuzzyZ_Zh << "\n";
fout << "fuzzyC_Ch=" << fuzzyC_Ch << "\n";
fout << "fuzzyS_Sh=" << fuzzyS_Sh << "\n";
fout << "fuzzyN_L=" << fuzzyN_L << "\n";
fout << "fuzzyF_H=" << fuzzyF_H << "\n";
fout << "fuzzyEn_Eng=" << fuzzyEn_Eng << "\n";
fout << "fuzzyIn_Ing=" << fuzzyIn_Ing << "\n";
fout << "chinesePunctuation=" << chinesePunctuation << "\n";
fout << "toggleHotkey=" << (unsigned long)toggleHotkey << "\n";
fout << "toggleModifier=" << (unsigned long)toggleModifier << "\n";
return true;
}
};
// 预设皮肤定义 (5 款暗色 + 4 款亮色)
inline const PinyinSettings::Skin PinyinSettings::skins[] = {
// ── 暗色 系列 ──
{L"暗色 墨黑", RGB(0x15,0x15,0x15), RGB(0x33,0x33,0x33), RGB(0xBD,0xBD,0xBD), RGB(0x76,0x76,0x76), RGB(0x5E,0x97,0xF6)},
{L"暗色 炭灰", RGB(0x21,0x21,0x21), RGB(0x3C,0x3C,0x3C), RGB(0xC8,0xC8,0xC8), RGB(0x82,0x82,0x82), RGB(0x6D,0xA0,0xF8)},
{L"暗色 暖咖", RGB(0x1C,0x18,0x16), RGB(0x3D,0x34,0x30), RGB(0xC8,0xB8,0xA0), RGB(0x88,0x78,0x60), RGB(0xD4,0xA0,0x50)},
{L"暗色 墨绿", RGB(0x14,0x1A,0x14), RGB(0x2A,0x3A,0x2A), RGB(0xA0,0xC0,0xA0), RGB(0x60,0x80,0x60), RGB(0x50,0xB8,0x70)},
{L"暗色 藏蓝", RGB(0x14,0x16,0x20), RGB(0x2A,0x30,0x40), RGB(0xA0,0xB0,0xD0), RGB(0x68,0x78,0xA0), RGB(0x60,0x78,0xE0)},
// ── 亮色系列 ──
{L"亮色 浅灰", RGB(0xF0,0xF0,0xF0), RGB(0xC0,0xC0,0xC0), RGB(0x33,0x33,0x33), RGB(0x70,0x70,0xC0), RGB(0x00,0x60,0xC0)},
{L"亮色 纯白", RGB(0xFF,0xFF,0xFF), RGB(0xD0,0xD0,0xD0), RGB(0x1A,0x1A,0x1A), RGB(0x58,0x58,0xB8), RGB(0x00,0x48,0xB8)},
{L"亮色 暖米", RGB(0xFF,0xF8,0xF0), RGB(0xD8,0xC8,0xB0), RGB(0x4A,0x3A,0x2A), RGB(0xB0,0x70,0x50), RGB(0xA0,0x50,0x30)},
{L"亮色 墨绿", RGB(0xF0,0xF5,0xF0), RGB(0x96,0xC6,0x96), RGB(0x28,0x3D,0x28), RGB(0x3C,0x81,0x3C), RGB(0x32,0x64,0x32)},
};
// ==================== 系统注册功能 ====================
// 将 PinyinIME 注册到 Windows 输入法系统中
// 检查当前进程是否以管理员权限运行
inline bool IsRunningAsAdmin() {
BOOL isAdmin = FALSE;
PSID adminSid = nullptr;
SID_IDENTIFIER_AUTHORITY ntAuth = SECURITY_NT_AUTHORITY;
if (AllocateAndInitializeSid(&ntAuth, 2, SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &adminSid)) {
CheckTokenMembership(nullptr, adminSid, &isAdmin);
FreeSid(adminSid);
}
return isAdmin != FALSE;
}
// 通过 UAC 提权重启自身进程 (用于执行需管理员权限的操作)
inline bool RelaunchWithUAC(const wchar_t* extraArgs) {
wchar_t exePath[MAX_PATH];
GetModuleFileNameW(nullptr, exePath, MAX_PATH);
SHELLEXECUTEINFOW sei = {};
sei.cbSize = sizeof(sei);
sei.fMask = SEE_MASK_NOCLOSEPROCESS;
sei.lpVerb = L"runas"; // 触发 UAC 提权对话框
sei.lpFile = exePath;
sei.lpParameters = extraArgs;
sei.nShow = SW_SHOWNORMAL;
if (ShellExecuteExW(&sei)) {
if (sei.hProcess) {
WaitForSingleObject(sei.hProcess, 15000); // 等 15 秒
DWORD exitCode = 0;
GetExitCodeProcess(sei.hProcess, &exitCode);
CloseHandle(sei.hProcess);
return exitCode == 0;
}
return true;
}
// 用户取消 UAC 弹窗 (ERROR_CANCELLED)
return false;
}
// ==================== 标准 TSF 注册功能 ====================
// 请将这里的 GUID 替换为你输入法实际的 GUID
// 示例中使用的是你代码里的 {A1B2C3D4-E5F6-7890-ABCD-EF1234567890}
static const GUID CLSID_PinyinIME = { 0xA1B2C3D4, 0xE5F6, 0x7890, { 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90 } };
// 输入法配置文件的 GUID,可以随机生成一个,但必须固定
static const GUID GUID_PinyinProfile = { 0xB2C3D4E5, 0xF6A1, 0x7890, { 0xBC, 0xDE, 0xF1, 0x23, 0x45, 0x67, 0x89, 0x0A } };
inline HRESULT RegisterTSFProfile(const wchar_t* dllPath) {
// 初始化 COM 库(如果外部没初始化的话)
HRESULT hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
bool coInit = SUCCEEDED(hr);
ITfInputProcessorProfiles* pProfiles = nullptr;
hr = CoCreateInstance(CLSID_TF_InputProcessorProfiles, nullptr, CLSCTX_INPROC_SERVER,
IID_ITfInputProcessorProfiles, (void**)&pProfiles);
if (SUCCEEDED(hr)) {
// 1. 注册输入法文本服务配置文件(对应简体中文:0x0804)
// 这一步会自动在系统的语言包及 TSF 内部框架中建立正确的信任与索引关系
hr = pProfiles->RegisterProfile(
CLSID_PinyinIME,
GUID_PinyinProfile,
L"PinyinIME", // 语言栏和设置中显示的名称
(ULONG)wcslen(L"PinyinIME"),
dllPath, // 你的输入法 DLL 的绝对路径
(ULONG)wcslen(dllPath),
0, // 图标文件的资源 ID(没有就填0)
0x0804 // 关键:0x0804 代表简体中文 (zh-CN)
);
if (SUCCEEDED(hr)) {
// 2. 将输入法分类(Category)注册为“键盘”类型输入法
// 如果不注册这个分类,系统虽然能看到它,但不会把它归类到“键盘布局”中
ITfCategoryMgr* pCategoryMgr = nullptr;
hr = CoCreateInstance(CLSID_TF_CategoryMgr, nullptr, CLSCTX_INPROC_SERVER,
IID_ITfCategoryMgr, (void**)&pCategoryMgr);
if (SUCCEEDED(hr)) {
// 声明它是一个标准的 KEYBOARD TIP
pCategoryMgr->RegisterCategory(CLSID_PinyinIME, GUID_TFCAT_TIP_KEYBOARD, CLSID_PinyinIME);
// 允许在 Windows 10/11 的安全模式(如锁屏界面、UAC弹窗)下运行(可选但强烈建议)
pCategoryMgr->RegisterCategory(CLSID_PinyinIME, GUID_TFCAT_TIPCAP_SECUREMODE, CLSID_PinyinIME);
// 允许在 Windows 现代 UWP/Metro 应用中运行
pCategoryMgr->RegisterCategory(CLSID_PinyinIME, GUID_TFCAT_TIP_TEXTEFFECT, CLSID_PinyinIME);
pCategoryMgr->Release();
}
}
pProfiles->Release();
}
if (coInit) CoUninitialize();
return hr;
}
// 将 PinyinIME 注册到系统 (写入 4 项注册表)
// silent=true: 静默模式 (用于 --register-system 命令行), 不弹总结框
// 返回 true 表示所有步骤均成功
inline bool registerIMEToSystem(bool silent) {
wchar_t exePath[MAX_PATH];
GetModuleFileNameW(nullptr, exePath, MAX_PATH);
int okCount = 0, failCount = 0, skipCount = 0;
std::wstring details;
auto logOk = [&](const wchar_t* m) { okCount++; details += L" ✅ "; details += m; details += L"\n"; };
auto logFail = [&](const wchar_t* m, LONG ec=0) { failCount++; details += L" ❌ "; details += m;
if (ec) { details += L" (代码 "; details += std::to_wstring(ec); details += L")"; }
details += L"\n"; };
auto logSkip = [&](const wchar_t* m) { skipCount++; details += L" ⏭️ "; details += m; details += L"\n"; };
// ── 1. 开机自启动 (HKCU) ────────────────────────────
{
HKEY hKey = nullptr;
LONG res = RegCreateKeyExW(HKEY_CURRENT_USER,
L"Software\\Microsoft\\Windows\\CurrentVersion\\Run",
0, nullptr, REG_OPTION_NON_VOLATILE,
KEY_WRITE | KEY_SET_VALUE, nullptr, &hKey, nullptr);
if (res == ERROR_SUCCESS) {
DWORD dataLen = (DWORD)(wcslen(exePath) + 1) * sizeof(wchar_t);
res = RegSetValueExW(hKey, L"PinyinIME", 0, REG_SZ,
(const BYTE*)exePath, dataLen);
RegCloseKey(hKey);
(res == ERROR_SUCCESS) ? logOk(L"开机自启动 已注册")
: logFail(L"开机自启动 写入失败", res);
} else {
logFail(L"开机自启动 无法打开注册表项", res);
}
}
// ── 2. 键盘布局预加载 (HKCU) ───────────────────────
{
HKEY hKey = nullptr;
LONG res = RegCreateKeyExW(HKEY_CURRENT_USER,
L"Keyboard Layout\\Preload",
0, nullptr, REG_OPTION_NON_VOLATILE,
KEY_READ | KEY_SET_VALUE, nullptr, &hKey, nullptr);
if (res == ERROR_SUCCESS) {
// 检查是否已注册过,避免重复添加
std::wstring targetId = L"E0800804";
bool alreadyExists = false;
for (int slot = 1; slot <= 20; slot++) {
std::wstring vn = std::to_wstring(slot);
wchar_t data[64] = {};
DWORD ds = sizeof(data);
if (RegQueryValueExW(hKey, vn.c_str(), nullptr, nullptr,
(LPBYTE)data, &ds) == ERROR_SUCCESS && targetId == data) {
alreadyExists = true; break;
}
}
if (alreadyExists) {
logOk(L"键盘布局 已存在, 跳过");
} else {
int maxSlot = 0;
for (int s = 1; s <= 20; s++) {
std::wstring vn = std::to_wstring(s);
DWORD d = 0, ds = sizeof(d);
if (RegQueryValueExW(hKey, vn.c_str(), nullptr, nullptr,
(LPBYTE)&d, &ds) == ERROR_SUCCESS) { if (s > maxSlot) maxSlot = s; }
}
std::wstring sn = std::to_wstring(maxSlot + 1);
res = RegSetValueExW(hKey, sn.c_str(), 0, REG_SZ,
(const BYTE*)targetId.c_str(),
(DWORD)((targetId.size() + 1) * sizeof(wchar_t)));
(res == ERROR_SUCCESS) ? logOk(L"键盘布局 已注册")
: logFail(L"键盘布局 写入失败", res);
}
RegCloseKey(hKey);
} else {
logFail(L"键盘布局 无法打开注册表项", res);
}
}
// ── 3. TSF 配置文件桩 (HKCU) ───────────────────────
{
const wchar_t* clsid = L"{A1B2C3D4-E5F6-7890-ABCD-EF1234567890}";
std::wstring tipPath = L"Software\\Microsoft\\CTF\\TIP\\";
tipPath += clsid;
HKEY hKey = nullptr;
LONG res = RegCreateKeyExW(HKEY_CURRENT_USER, tipPath.c_str(),
0, nullptr, REG_OPTION_NON_VOLATILE,
KEY_WRITE | KEY_SET_VALUE, nullptr, &hKey, nullptr);
if (res == ERROR_SUCCESS) {
RegSetValueExW(hKey, L"Display Description", 0, REG_SZ,
(const BYTE*)L"PinyinIME", 20);
RegSetValueExW(hKey, nullptr, 0, REG_SZ,
(const BYTE*)L"PinyinIME 拼音输入法", 36);
std::wstring catPath = tipPath;
catPath += L"\\Category\\Item\\{34745C63-B2F0-4784-8B67-5E12C8701A31}";
HKEY hCat = nullptr;
LONG catRes = RegCreateKeyExW(HKEY_CURRENT_USER, catPath.c_str(),
0, nullptr, REG_OPTION_NON_VOLATILE,
KEY_WRITE | KEY_SET_VALUE, nullptr, &hCat, nullptr);
if (hCat) RegCloseKey(hCat);
RegCloseKey(hKey);
(catRes == ERROR_SUCCESS) ? logOk(L"TSF 输入法配置文件 已注册")
: logFail(L"TSF 输入法配置文件 写入失败", catRes);
} else {
logFail(L"TSF 输入法配置文件 无法打开注册表项", res);
}
}
// ── 4. 系统级键盘布局 (HKLM, 需管理员) ────────────
{
LONG hklmErr = 0;
{
HKEY hKey = nullptr;
LONG res = RegCreateKeyExW(HKEY_LOCAL_MACHINE,
L"SYSTEM\\CurrentControlSet\\Control\\Keyboard Layouts\\E0800804",
0, nullptr, REG_OPTION_NON_VOLATILE,
KEY_WRITE | KEY_SET_VALUE, nullptr, &hKey, nullptr);
if (res == ERROR_SUCCESS) {
RegSetValueExW(hKey, L"Layout File", 0, REG_SZ,
(const BYTE*)L"kbdus.dll", 20);
RegSetValueExW(hKey, L"Layout Text", 0, REG_SZ,
(const BYTE*)L"PinyinIME Chinese", 36);
RegSetValueExW(hKey, L"Layout Display Name", 0, REG_SZ,
(const BYTE*)L"PinyinIME 拼音输入法", 36);
RegCloseKey(hKey);
logOk(L"系统级键盘布局 已注册 (HKLM)");
} else {
hklmErr = res;
}
}
if (hklmErr == ERROR_ACCESS_DENIED && hklmErr != 0) {
// 权限不足 → 询问用户是否 UAC 提权 (静默模式直接提权)
bool elevate = silent;
if (!silent) {
int mb = MessageBoxW(nullptr,
L"注册系统级键盘布局需要管理员权限。\n\n"
L"是否通过 UAC 提权继续?\n"
L"(选择「否」将跳过此步骤,输入法核心功能不受影响)",
L"PinyinIME — 需要管理员权限",
MB_YESNO | MB_ICONQUESTION);
elevate = (mb == IDYES);
}
if (elevate) {
if (RelaunchWithUAC(L"--register-system")) {
logOk(L"系统级键盘布局 已注册 (UAC 提权)");
} else {
logSkip(L"系统级键盘布局 用户取消或提权失败");
}
} else {
logSkip(L"系统级键盘布局 已跳过 (无管理员权限)");
}
} else if (hklmErr != 0) {
logFail(L"系统级键盘布局 注册失败", hklmErr);
}
}
// ── 显示结果 (非静默模式) ──────────────────────────
if (!silent) {
std::wstring summary;
summary += L"成功: " + std::to_wstring(okCount) + L" 项";
if (failCount > 0) summary += L", 失败: " + std::to_wstring(failCount) + L" 项";
if (skipCount > 0) summary += L", 跳过: " + std::to_wstring(skipCount) + L" 项";
std::wstring msg = L"PinyinIME 系统注册结果\n\n" + summary + L"\n\n" + details;
msg += L"\n━━━━━━━━━━━━━━━━━━━━\n";
msg += L"💡 提示:\n";
msg += L"• HKCU 注册项无需管理员权限\n";
msg += L"• 系统级键盘布局 (HKLM) 需要管理员权限\n";
msg += L"• 即使跳过 HKLM,输入法仍可正常使用\n";
msg += L"• 系统托盘可找到 PinyinIME 图标\n";
MessageBoxW(nullptr, msg.c_str(),
L"PinyinIME 系统注册", MB_OK | MB_ICONINFORMATION | MB_TOPMOST);
}
return failCount == 0;
}
// ==================== 前向声明 ====================
// 这些函数在 main.cpp 中实现 (在完整的类定义之后)
extern void userDictAddEntry(const std::string& pinyin, const std::string& word, int freq);
extern void userDictRemoveEntry(const std::string& pinyin, const std::string& word);
extern void userDictClearAll();
extern std::string userDictGetAllAsString();
extern void userDictSaveToFile();
extern void applySettingsToEngine(const PinyinSettings& s);
// 全局设置对象 (定义在 main.cpp)
extern PinyinSettings g_settings;
// ==================== 用户词典管理对话框 ====================
struct UserDictDialog {
static std::vector<std::pair<std::string, std::pair<std::string, int>>> g_entries;
static HWND g_hList;
static HWND g_hParent;
static void refreshList(HWND hList) {
SendMessageW(hList, LB_RESETCONTENT, 0, 0);
std::string all = userDictGetAllAsString();
if (all.empty()) return;
std::istringstream iss(all);
std::string line;
while (std::getline(iss, line)) {
if (line.empty()) continue;
std::wstring wline = utf8ToWide(line);
SendMessageW(hList, LB_ADDSTRING, 0, (LPARAM)wline.c_str());
}
}
static INT_PTR CALLBACK dlgProc(HWND hDlg, UINT msg, WPARAM wp, LPARAM lp) {
switch (msg) {
case WM_INITDIALOG: {
g_hParent = hDlg;
g_hList = GetDlgItem(hDlg, 101);
refreshList(g_hList);
return TRUE;
}
case WM_COMMAND: {
switch (LOWORD(wp)) {
case 201: { // 添加
HWND hPy = GetDlgItem(hDlg, 301);
HWND hWd = GetDlgItem(hDlg, 302);
wchar_t py[256] = {0}, wd[256] = {0};
GetWindowTextW(hPy, py, 256);
GetWindowTextW(hWd, wd, 256);
if (py[0] && wd[0]) {
std::string spy = wideToUtf8(py);
std::string swd = wideToUtf8(wd);
userDictAddEntry(spy, swd, 8500);
refreshList(g_hList);
SetWindowTextW(hPy, L"");
SetWindowTextW(hWd, L"");
}
return TRUE;
}
case 202: { // 删除选中
int sel = (int)SendMessageW(g_hList, LB_GETCURSEL, 0, 0);
if (sel >= 0) {
wchar_t buf[512];
SendMessageW(g_hList, LB_GETTEXT, sel, (LPARAM)buf);
std::string line = wideToUtf8(buf);
size_t t1 = line.find('\t');
size_t t2 = line.find('\t', t1 + 1);
if (t1 != std::string::npos && t2 != std::string::npos) {
std::string py = line.substr(0, t1);
std::string wd = line.substr(t1 + 1, t2 - t1 - 1);
userDictRemoveEntry(py, wd);
}
refreshList(g_hList);
}
return TRUE;
}
case 203: { // 清空全部
if (MessageBoxW(hDlg, L"确定清空所有用户词典吗?", L"确认", MB_YESNO | MB_ICONQUESTION) == IDYES) {
userDictClearAll();
refreshList(g_hList);
}
return TRUE;
}
case 204: EndDialog(hDlg, 0); return TRUE; // 关闭
}
break;
}
}
return FALSE;
}
static void show(HINSTANCE hInst, HWND hParent) {
// 使用 DialogBox 会阻塞,这里用 CreateDialog 实现非阻塞
HWND hDlg = CreateDialogParamW(hInst, MAKEINTRESOURCEW(200), hParent, dlgProc, 0);
ShowWindow(hDlg, SW_SHOW);
}
};
inline HWND UserDictDialog::g_hList = nullptr;
inline HWND UserDictDialog::g_hParent = nullptr;
inline std::vector<std::pair<std::string, std::pair<std::string, int>>> UserDictDialog::g_entries;
// ==================== 主设置对话框 ====================
struct SettingsDialog {
static PinyinSettings g_tempSettings;
static HWND g_hSkinPreview;
static int g_selectedSkin;
// 自定义皮肤颜色预览
static LRESULT CALLBACK previewWndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
if (msg == WM_PAINT) {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
RECT rc;
GetClientRect(hwnd, &rc);
HBRUSH hBg = CreateSolidBrush(g_tempSettings.bgColor);
FillRect(hdc, &rc, hBg);
DeleteObject(hBg);
HPEN hPen = CreatePen(PS_SOLID, 1, g_tempSettings.borderColor);
HPEN oldPen = (HPEN)SelectObject(hdc, hPen);
Rectangle(hdc, rc.left, rc.top, rc.right, rc.bottom);
SelectObject(hdc, oldPen);
DeleteObject(hPen);
HFONT hFont = CreateFontW(-16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY, DEFAULT_PITCH | FF_SWISS, g_tempSettings.fontName.c_str());
SelectObject(hdc, hFont);
SetBkMode(hdc, TRANSPARENT);
// 候选词 (从左边距开始, 无拼音前缀)
SetTextColor(hdc, g_tempSettings.indexColor);
TextOutW(hdc, 8, 5, L"1.", 2);
SetTextColor(hdc, g_tempSettings.textColor);
TextOutW(hdc, 23, 5, L"你好", 2);
SetTextColor(hdc, g_tempSettings.indexColor);
TextOutW(hdc, 58, 5, L"2.", 2);
SetTextColor(hdc, g_tempSettings.textColor);
TextOutW(hdc, 73, 5, L"泥嚎", 2);
DeleteObject(hFont);
EndPaint(hwnd, &ps);
return 0;
}
return DefWindowProcW(hwnd, msg, wp, lp);
}
static void applySettingsToDialog(HWND hDlg) {
// 基本设置
CheckDlgButton(hDlg, 401, g_tempSettings.useTraditional ? BST_CHECKED : BST_UNCHECKED);
SetDlgItemInt(hDlg, 402, g_tempSettings.candidateCount, FALSE);
SetDlgItemInt(hDlg, 403, g_tempSettings.fontSize, TRUE);
CheckDlgButton(hDlg, 404, g_tempSettings.verticalLayout ? BST_CHECKED : BST_UNCHECKED);
CheckDlgButton(hDlg, 405, g_tempSettings.chinesePunctuation ? BST_CHECKED : BST_UNCHECKED);
// 模糊音
CheckDlgButton(hDlg, 501, g_tempSettings.fuzzyZ_Zh ? BST_CHECKED : BST_UNCHECKED);
CheckDlgButton(hDlg, 502, g_tempSettings.fuzzyC_Ch ? BST_CHECKED : BST_UNCHECKED);
CheckDlgButton(hDlg, 503, g_tempSettings.fuzzyS_Sh ? BST_CHECKED : BST_UNCHECKED);
CheckDlgButton(hDlg, 504, g_tempSettings.fuzzyN_L ? BST_CHECKED : BST_UNCHECKED);
CheckDlgButton(hDlg, 505, g_tempSettings.fuzzyF_H ? BST_CHECKED : BST_UNCHECKED);
CheckDlgButton(hDlg, 506, g_tempSettings.fuzzyEn_Eng ? BST_CHECKED : BST_UNCHECKED);
CheckDlgButton(hDlg, 507, g_tempSettings.fuzzyIn_Ing ? BST_CHECKED : BST_UNCHECKED);
// 皮肤下拉
HWND hSkin = GetDlgItem(hDlg, 701);
SendMessageW(hSkin, CB_RESETCONTENT, 0, 0);
for (int i = 0; i < PinyinSettings::SKIN_COUNT; i++) {
SendMessageW(hSkin, CB_ADDSTRING, 0, (LPARAM)PinyinSettings::skins[i].name);
}
SendMessageW(hSkin, CB_SETCURSEL, g_selectedSkin, 0);
InvalidateRect(g_hSkinPreview, nullptr, TRUE);
}
static INT_PTR CALLBACK dlgProc(HWND hDlg, UINT msg, WPARAM wp, LPARAM lp) {
switch (msg) {
case WM_INITDIALOG: {
g_tempSettings = g_settings;
g_selectedSkin = 0;
// 创建皮肤预览窗口
g_hSkinPreview = CreateWindowExW(0, L"STATIC", L"",
WS_CHILD | WS_VISIBLE | SS_OWNERDRAW,
390, 15, 260, 35, hDlg, nullptr, nullptr, nullptr);
SetWindowLongPtrW(g_hSkinPreview, GWLP_WNDPROC, (LONG_PTR)previewWndProc);
applySettingsToDialog(hDlg);
// 限制候选数量范围
SendMessageW(GetDlgItem(hDlg, 402), EM_LIMITTEXT, 1, 0);
return TRUE;
}
case WM_DRAWITEM: {
LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)lp;
if (lpdis->hwndItem == g_hSkinPreview) {
// handled by previewWndProc via WM_PAINT
return TRUE;
}
break;
}
case WM_CTLCOLORSTATIC: {
// 确保预览窗口背景刷新
if ((HWND)lp == g_hSkinPreview) {
InvalidateRect(g_hSkinPreview, nullptr, TRUE);
}
break;
}
case WM_COMMAND: {
int id = LOWORD(wp);
int code = HIWORD(wp);
switch (id) {
case 701: // 皮肤选择
if (code == CBN_SELCHANGE) {
int sel = (int)SendMessageW(GetDlgItem(hDlg, 701), CB_GETCURSEL, 0, 0);
if (sel >= 0) {
g_selectedSkin = sel;
g_tempSettings.applySkin(sel);
InvalidateRect(g_hSkinPreview, nullptr, TRUE);
}
}
break;
case 702: { // 自定义主色调
CHOOSECOLORW cc = {};
static COLORREF acr[16];
cc.lStructSize = sizeof(cc);
cc.hwndOwner = hDlg;
cc.rgbResult = g_tempSettings.bgColor;
cc.lpCustColors = acr;
cc.Flags = CC_FULLOPEN | CC_RGBINIT;
if (ChooseColorW(&cc)) {
// 基于选色生成一套配色
COLORREF base = cc.rgbResult;
int r = GetRValue(base), g = GetGValue(base), b = GetBValue(base);
g_tempSettings.bgColor = base;
g_tempSettings.borderColor = RGB((r*3)/4, (g*3)/4, (b*3)/4);
// 根据亮度决定文字颜色
int brightness = (r*299 + g*587 + b*114) / 1000;
if (brightness > 128) {
g_tempSettings.textColor = RGB((r*1)/5, (g*1)/5, (b*1)/5);
g_tempSettings.indexColor = RGB((r*1)/3, (g*2)/5, (b+100>255?255:b+100));
g_tempSettings.inputColor = RGB(0, (g*1)/4, (b*4)/5);
} else {
g_tempSettings.textColor = RGB(220, 220, 220);
g_tempSettings.indexColor = RGB((r+100>255?255:r+100), (g+80>255?255:g+80), 255);
g_tempSettings.inputColor = RGB(150, 200, 255);
}
g_selectedSkin = -1; // 自定义
SendMessageW(GetDlgItem(hDlg, 701), CB_SETCURSEL, -1, 0);
InvalidateRect(g_hSkinPreview, nullptr, TRUE);
}
break;
}
case 801: // 打开用户词典管理
UserDictDialog::show((HINSTANCE)GetWindowLongPtrW(hDlg, GWLP_HINSTANCE), hDlg);
break;
case 901: { // 保存
// 从控件读取设置
g_tempSettings.useTraditional = (IsDlgButtonChecked(hDlg, 401) == BST_CHECKED);
BOOL ok;
int cnt = GetDlgItemInt(hDlg, 402, &ok, FALSE);
if (ok && cnt >= 3 && cnt <= 9) g_tempSettings.candidateCount = cnt;
int fs = GetDlgItemInt(hDlg, 403, &ok, TRUE);
if (ok && fs >= 12 && fs <= 36) g_tempSettings.fontSize = fs;
g_tempSettings.verticalLayout = (IsDlgButtonChecked(hDlg, 404) == BST_CHECKED);
g_tempSettings.chinesePunctuation = (IsDlgButtonChecked(hDlg, 405) == BST_CHECKED);
g_tempSettings.fuzzyZ_Zh = (IsDlgButtonChecked(hDlg, 501) == BST_CHECKED);
g_tempSettings.fuzzyC_Ch = (IsDlgButtonChecked(hDlg, 502) == BST_CHECKED);
g_tempSettings.fuzzyS_Sh = (IsDlgButtonChecked(hDlg, 503) == BST_CHECKED);
g_tempSettings.fuzzyN_L = (IsDlgButtonChecked(hDlg, 504) == BST_CHECKED);
g_tempSettings.fuzzyF_H = (IsDlgButtonChecked(hDlg, 505) == BST_CHECKED);
g_tempSettings.fuzzyEn_Eng = (IsDlgButtonChecked(hDlg, 506) == BST_CHECKED);
g_tempSettings.fuzzyIn_Ing = (IsDlgButtonChecked(hDlg, 507) == BST_CHECKED);
// 应用设置
g_settings = g_tempSettings;
g_settings.saveToFile(getExeDirectory() + "pinyin_config.ini");
applySettingsToEngine(g_settings);
EndDialog(hDlg, 1);
return TRUE;
}
case 902: // 取消
EndDialog(hDlg, 0);
return TRUE;
}
break;
}
}
return FALSE;
}
static bool show(HINSTANCE hInst, HWND hParent) {
// 需要用 DialogBox 创建的资源对话框
// 由于资源文件不存在,这里用 CreateWindow 手动构建
// 详见 createSettingsWindow 函数
INT_PTR result = DialogBoxParamW(hInst, MAKEINTRESOURCEW(100), hParent, dlgProc, 0);
return result == 1;
}
};
inline PinyinSettings SettingsDialog::g_tempSettings;
inline HWND SettingsDialog::g_hSkinPreview = nullptr;
inline int SettingsDialog::g_selectedSkin = 0;
// ==================== 系统字体枚举 ====================
// 使用 EnumFontFamiliesExW 遍历系统已安装的 TrueType/OpenType 字体
// 返回去重并排序的字体名称列表,可直接用于 CreateFontW
inline std::vector<std::wstring> enumSystemFonts() {
std::vector<std::wstring> fonts;
HDC hdc = GetDC(nullptr);
if (!hdc) return fonts;
LOGFONTW lf = {};
lf.lfCharSet = DEFAULT_CHARSET;
lf.lfFaceName[0] = L'\0';
lf.lfPitchAndFamily = 0;
EnumFontFamiliesExW(hdc, &lf,
[](const LOGFONTW* pLf, const TEXTMETRICW*, DWORD fontType, LPARAM lParam) -> int {
auto* pFonts = reinterpret_cast<std::vector<std::wstring>*>(lParam);
// 只收录 TrueType / OpenType 字体 (排除点阵字体和设备字体)
if (fontType & TRUETYPE_FONTTYPE) {
std::wstring name = pLf->lfFaceName;
// 去重 (同一字体家族的不同样式只保留一个)
if (std::find(pFonts->begin(), pFonts->end(), name) == pFonts->end()) {
pFonts->push_back(name);
}
}
return 1; // 继续枚举
},
(LPARAM)&fonts, 0);
ReleaseDC(nullptr, hdc);
// 按 Unicode 排序 (拉丁字母在前, 中文在后)
std::sort(fonts.begin(), fonts.end());
return fonts;
}
// ==================== 无资源文件的设置窗口构建 ====================
// 因为不使用 .rc 资源文件,纯代码创建所有控件
struct SettingsWindow {
HWND m_hDlg = nullptr;
HWND m_hSkinPreview = nullptr;
int m_selectedSkin = 8; // 默认 亮色 墨绿
PinyinSettings m_temp;
HFONT m_hFont = nullptr; // 所有控件共用字体
HBRUSH m_hBgBrush = nullptr; // 对话框背景画刷 (跟随皮肤)
float m_dpiScale = 1.0f; // DPI 缩放因子 (96 DPI=1.0, 192 DPI=2.0)
// 皮肤预览子窗口过程
static LRESULT CALLBACK skinPreviewProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
SettingsWindow* self = (SettingsWindow*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
if (!self && msg == WM_CREATE) {
CREATESTRUCTW* cs = (CREATESTRUCTW*)lp;
self = (SettingsWindow*)cs->lpCreateParams;
SetWindowLongPtrW(hwnd, GWLP_USERDATA, (LONG_PTR)self);
}
if (msg == WM_PAINT && self) {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
RECT rc; GetClientRect(hwnd, &rc);
auto S = [self](int v) -> int { return (int)(v * self->m_dpiScale + 0.5f); };
// 字体 (字号与真实候选框默认一致)
HFONT hFont = CreateFontW(-S(16), 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY, DEFAULT_PITCH | FF_SWISS, self->m_temp.fontName.c_str());
SelectObject(hdc, hFont);
SetBkMode(hdc, TRANSPARENT);
// 获取字体度量 (用于圆角半径和文字定位)
TEXTMETRICW tm;
GetTextMetrics(hdc, &tm);
// ── GDI+ 抗锯齿: 背景填充 + 渐变圆角边框 ──
{
Gdiplus::Graphics graphics(hdc);
graphics.SetSmoothingMode(Gdiplus::SmoothingModeAntiAlias);
int w = rc.right, h = rc.bottom;
int cr = (std::max)(6, (std::min)(12, (int)(tm.tmHeight * 2 / 3)));
// 圆角矩形路径构建器
auto makeRR = [](Gdiplus::GraphicsPath& p, int x, int y, int rw, int rh, int rad) {
p.Reset();
p.StartFigure();
p.AddArc(x, y, rad*2, rad*2, 180, 90);
p.AddArc(x + rw - rad*2, y, rad*2, rad*2, 270, 90);
p.AddArc(x + rw - rad*2, y + rh - rad*2, rad*2, rad*2, 0, 90);
p.AddArc(x, y + rh - rad*2, rad*2, rad*2, 90, 90);
p.CloseFigure();
};
// 填充背景
Gdiplus::GraphicsPath bgPath;
{
COLORREF c = self->m_temp.bgColor;
Gdiplus::SolidBrush bgBrush(Gdiplus::Color(GetRValue(c), GetGValue(c), GetBValue(c)));
makeRR(bgPath, 0, 0, w, h, cr);
graphics.FillPath(&bgBrush, &bgPath);
}
// 渐变边框
COLORREF bc = self->m_temp.borderColor;
COLORREF bg = self->m_temp.bgColor;
int rr = GetRValue(bc), rg = GetGValue(bc), rb = GetBValue(bc);
int bgBright = (GetRValue(bg)*299 + GetGValue(bg)*587 + GetBValue(bg)*114) / 1000;
int dir = (bgBright < 128) ? 1 : -1;
auto clampC = [](int v) -> int { return v < 0 ? 0 : (v > 255 ? 255 : v); };
Gdiplus::GraphicsPath borderPath;
for (int layer = 0; layer < 3; layer++) {
int off = layer;
int delta = (layer == 0) ? 40 : (layer == 1) ? 18 : 0;
int lw = w - off * 2, lh = h - off * 2;
int lcr = cr - off; if (lcr < 2) lcr = 2;
Gdiplus::Color penColor(clampC(rr + delta * dir), clampC(rg + delta * dir), clampC(rb + delta * dir));
Gdiplus::Pen pen(penColor, 1.0f);
makeRR(borderPath, off, off, lw, lh, lcr);
graphics.DrawPath(&pen, &borderPath);
}
}
// GDI+ Graphics 析构, 恢复 HDC 状态
// ── GDI 文字渲染 ──
int borderW = 3;
int pad = (std::max)(4, (int)(tm.tmHeight / 8));
int textY = borderW + pad;
int x = S(8);
// 候选词 (从左边距开始, 无拼音前缀)
SIZE sz;
// 候选词 1
SetTextColor(hdc, self->m_temp.indexColor);
TextOutW(hdc, x, textY, L"1.", 2);
GetTextExtentPoint32W(hdc, L"1.", 2, &sz); x += sz.cx;
SetTextColor(hdc, self->m_temp.textColor);
TextOutW(hdc, x, textY, L"你好", 2);
GetTextExtentPoint32W(hdc, L"你好", 2, &sz); x += sz.cx + S(8);
// 候选词 2
SetTextColor(hdc, self->m_temp.indexColor);
TextOutW(hdc, x, textY, L"2.", 2);
GetTextExtentPoint32W(hdc, L"2.", 2, &sz); x += sz.cx;
SetTextColor(hdc, self->m_temp.textColor);
TextOutW(hdc, x, textY, L"泥嚎", 2);
GetTextExtentPoint32W(hdc, L"泥嚎", 2, &sz); x += sz.cx + S(8);
// 候选词 3
SetTextColor(hdc, self->m_temp.indexColor);
TextOutW(hdc, x, textY, L"3.", 2);
GetTextExtentPoint32W(hdc, L"3.", 2, &sz); x += sz.cx;
SetTextColor(hdc, self->m_temp.textColor);
TextOutW(hdc, x, textY, L"你号", 2);
GetTextExtentPoint32W(hdc, L"你号", 2, &sz); x += sz.cx + S(4);
// 翻页提示
SetTextColor(hdc, RGB(150, 150, 150));
TextOutW(hdc, x, textY, L" -/=/翻页", 8);
GetTextExtentPoint32W(hdc, L" -/=/翻页", 8, &sz); x += sz.cx + S(4);
// 齿轮图标
SetTextColor(hdc, RGB(80, 80, 200));
TextOutW(hdc, x, textY, L"⚙", 1);
DeleteObject(hFont);
EndPaint(hwnd, &ps);
return 0;
}
return DefWindowProcW(hwnd, msg, wp, lp);
}
// 创建控件辅助函数 (统一使用微软雅黑字体)
HWND addLabel(const wchar_t* text, int x, int y, int w, int h) {
HWND ctrl = CreateWindowExW(0, L"STATIC", text,
WS_CHILD | WS_VISIBLE,
x, y, w, h, m_hDlg, nullptr,
(HINSTANCE)GetWindowLongPtrW(m_hDlg, GWLP_HINSTANCE), nullptr);
if (ctrl && m_hFont) SendMessageW(ctrl, WM_SETFONT, (WPARAM)m_hFont, TRUE);
return ctrl;
}
HWND addCheck(const wchar_t* text, int id, int x, int y, int w, int h, bool checked) {
HWND ctrl = CreateWindowExW(0, L"BUTTON", text,
WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
x, y, w, h, m_hDlg, (HMENU)(UINT_PTR)id,
(HINSTANCE)GetWindowLongPtrW(m_hDlg, GWLP_HINSTANCE), nullptr);
if (ctrl) {
if (m_hFont) SendMessageW(ctrl, WM_SETFONT, (WPARAM)m_hFont, TRUE);
SendMessageW(ctrl, BM_SETCHECK, checked ? BST_CHECKED : BST_UNCHECKED, 0);
}
return ctrl;
}
HWND addButton(const wchar_t* text, int id, int x, int y, int w, int h) {
HWND ctrl = CreateWindowExW(0, L"BUTTON", text,
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
x, y, w, h, m_hDlg, (HMENU)(UINT_PTR)id,
(HINSTANCE)GetWindowLongPtrW(m_hDlg, GWLP_HINSTANCE), nullptr);
if (ctrl && m_hFont) SendMessageW(ctrl, WM_SETFONT, (WPARAM)m_hFont, TRUE);
return ctrl;
}
HWND addEdit(int id, int x, int y, int w, int h, const wchar_t* text) {
HWND ctrl = CreateWindowExW(WS_EX_CLIENTEDGE, L"EDIT", text,
WS_CHILD | WS_VISIBLE | ES_LEFT | ES_AUTOHSCROLL,
x, y, w, h, m_hDlg, (HMENU)(UINT_PTR)id,
(HINSTANCE)GetWindowLongPtrW(m_hDlg, GWLP_HINSTANCE), nullptr);
if (ctrl && m_hFont) SendMessageW(ctrl, WM_SETFONT, (WPARAM)m_hFont, TRUE);
return ctrl;
}
HWND addCombo(int id, int x, int y, int w, int h) {
HWND ctrl = CreateWindowExW(0, L"COMBOBOX", L"",
WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | WS_VSCROLL,
x, y, w, h, m_hDlg, (HMENU)(UINT_PTR)id,
(HINSTANCE)GetWindowLongPtrW(m_hDlg, GWLP_HINSTANCE), nullptr);
if (ctrl && m_hFont) SendMessageW(ctrl, WM_SETFONT, (WPARAM)m_hFont, TRUE);
return ctrl;
}
// 更新背景画刷 (跟随当前皮肤)