Repository navigation
Expand file tree
/
Copy pathSource.cpp
More file actions
4206 lines (4001 loc) · 571 KB
/
Copy pathSource.cpp
File metadata and controls
4206 lines (4001 loc) · 571 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
#pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#define NOMINMAX
#include <windows.h>
#include <shellapi.h>
#include <shlobj.h>
#include <shobjidl.h>
#include <propkey.h>
#include <propvarutil.h>
#pragma comment(lib, "propsys.lib")
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
#include <d3d11.h>
#include <dxgi1_2.h>
#include <d2d1_1.h>
#include <d2d1.h>
#include <dwrite.h>
#include <dcomp.h>
#include <initguid.h>
#include <d2d1effects.h>
#include <imm.h>
#include <commdlg.h>
#include <commctrl.h>
#include <dwmapi.h>
#include <uxtheme.h>
#include <string>
#include <vector>
#include <memory>
#include <cassert>
#include <algorithm>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <sstream>
#include <regex>
#include <cstring>
#include "compact_enc_det/compact_enc_det.h"
#include "resource.h"
#pragma comment(lib, "d2d1.lib")
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "dxgi.lib")
#pragma comment(lib, "dcomp.lib")
#pragma comment(lib, "dwrite.lib")
#pragma comment(lib, "dxguid.lib")
#pragma comment(lib, "imm32.lib")
#pragma comment(lib, "comdlg32.lib")
#pragma comment(lib, "comctl32.lib")
#pragma comment(lib, "dwmapi.lib")
#pragma comment(lib, "uxtheme.lib")
#pragma comment(lib, "ced.lib")
const std::wstring APP_VERSION = L"miu v1.0.30";
enum MiuEncoding { ENC_UTF8_NOBOM = 0, ENC_UTF8_BOM, ENC_UTF16LE, ENC_UTF16BE, ENC_LOCAL };
struct DetectResult { MiuEncoding type; UINT codePage; };
static void SwapBytes(wchar_t* buf, size_t count) {
for (size_t i = 0; i < count; ++i) { unsigned short x = (unsigned short)buf[i]; buf[i] = (wchar_t)((x >> 8) | (x << 8)); }
}
static bool IsValidUtf8(const char* buf, size_t len) {
if (len == 0) return true;
size_t check_len = (len > 4096) ? 4096 : len; size_t i = 0;
while (i < check_len) {
unsigned char c = buf[i];
if (c <= 0x7F) i++;
else if (c >= 0xC2 && c <= 0xDF) { if (i + 1 >= check_len) break; if ((buf[i + 1] & 0xC0) != 0x80) return false; i += 2; }
else if (c >= 0xE0 && c <= 0xEF) { if (i + 2 >= check_len) break; if ((buf[i + 1] & 0xC0) != 0x80 || (buf[i + 2] & 0xC0) != 0x80) return false; i += 3; }
else if (c >= 0xF0 && c <= 0xF4) { if (i + 3 >= check_len) break; if ((buf[i + 1] & 0xC0) != 0x80 || (buf[i + 2] & 0xC0) != 0x80 || (buf[i + 3] & 0xC0) != 0x80) return false; i += 4; }
else return false;
}
return true;
}
static UINT MapCedEncodingToCodePage(Encoding enc) {
switch (enc) {
case JAPANESE_SHIFT_JIS: return 932;
case JAPANESE_EUC_JP: return 51932;
case CHINESE_GB: return 936;
case CHINESE_BIG5: return 950;
case KOREAN_EUC_KR: return 949;
case RUSSIAN_CP1251: return 1251;
case LATIN1: return 1252;
case ASCII_7BIT: return CP_UTF8;
default: return CP_ACP;
}
}
static DetectResult DetectEncodingEx(const char* buf, size_t len) {
DetectResult res = { ENC_UTF8_NOBOM, CP_UTF8 };
if (len >= 3 && (unsigned char)buf[0] == 0xEF && (unsigned char)buf[1] == 0xBB && (unsigned char)buf[2] == 0xBF) { res.type = ENC_UTF8_BOM; return res; }
if (len >= 2) {
if ((unsigned char)buf[0] == 0xFF && (unsigned char)buf[1] == 0xFE) { res.type = ENC_UTF16LE; return res; }
if ((unsigned char)buf[0] == 0xFE && (unsigned char)buf[1] == 0xFF) { res.type = ENC_UTF16BE; return res; }
}
if (IsValidUtf8(buf, len)) { res.type = ENC_UTF8_NOBOM; return res; }
int bytes_consumed = 0; bool is_reliable = false; size_t ced_len = (len > 65536) ? 65536 : len;
Encoding ced_enc = CompactEncDet::DetectEncoding(buf, static_cast<int>(ced_len), nullptr, nullptr, nullptr, UNKNOWN_ENCODING, UNKNOWN_LANGUAGE, CompactEncDet::WEB_CORPUS, false, &bytes_consumed, &is_reliable);
res.type = ENC_LOCAL; res.codePage = MapCedEncodingToCodePage(ced_enc);
return res;
}
static std::wstring UTF8ToW(const std::string& s) {
if (s.empty()) return {};
int n = MultiByteToWideChar(CP_UTF8, 0, s.data(), (int)s.size(), NULL, 0);
if (n <= 0) return {};
std::wstring w; w.resize(n);
MultiByteToWideChar(CP_UTF8, 0, s.data(), (int)s.size(), &w[0], n);
return w;
}
static void UTF8ToW(const std::string& s, std::wstring& out) {
out.clear();
if (s.empty()) return;
int n = MultiByteToWideChar(CP_UTF8, 0, s.data(), (int)s.size(), NULL, 0);
if (n <= 0) return;
out.resize(n);
MultiByteToWideChar(CP_UTF8, 0, s.data(), (int)s.size(), &out[0], n);
}
static std::string LocalToUtf8(const char* data, size_t len, UINT cp) {
if (len == 0) return "";
int wLen = MultiByteToWideChar(cp, 0, data, (int)len, NULL, 0);
if (wLen <= 0) return "";
std::vector<wchar_t> wBuf(wLen);
MultiByteToWideChar(cp, 0, data, (int)len, wBuf.data(), wLen);
int uLen = WideCharToMultiByte(CP_UTF8, 0, wBuf.data(), wLen, NULL, 0, NULL, NULL);
if (uLen <= 0) return "";
std::string ret; ret.resize(uLen);
WideCharToMultiByte(CP_UTF8, 0, wBuf.data(), wLen, &ret[0], uLen, NULL, NULL);
return ret;
}
static std::string Utf8ToLocal(const std::string& utf8, UINT cp) {
if (utf8.empty()) return "";
std::wstring w = UTF8ToW(utf8);
int len = WideCharToMultiByte(cp, 0, w.c_str(), (int)w.size(), NULL, 0, NULL, NULL);
if (len <= 0) return "";
std::string ret; ret.resize(len);
WideCharToMultiByte(cp, 0, w.c_str(), (int)w.size(), &ret[0], len, NULL, NULL);
return ret;
}
static std::string Utf16ToUtf8(const char* data, size_t len, bool isBigEndian) {
if (len < 2) return "";
const wchar_t* wData = (const wchar_t*)(data + 2);
size_t wLen = (len - 2) / sizeof(wchar_t);
if (wLen == 0) return "";
std::vector<wchar_t> wBuf(wData, wData + wLen);
if (isBigEndian) SwapBytes(wBuf.data(), wBuf.size());
int uLen = WideCharToMultiByte(CP_UTF8, 0, wBuf.data(), (int)wBuf.size(), NULL, 0, NULL, NULL);
if (uLen <= 0) return "";
std::string ret; ret.resize(uLen);
WideCharToMultiByte(CP_UTF8, 0, wBuf.data(), (int)wBuf.size(), &ret[0], uLen, NULL, NULL);
return ret;
}
static std::wstring Utf8ToUtf16(const std::string& utf8) {
if (utf8.empty()) return L"";
int wLen = MultiByteToWideChar(CP_UTF8, 0, utf8.data(), (int)utf8.size(), NULL, 0);
if (wLen <= 0) return L"";
std::wstring ret; ret.resize(wLen);
MultiByteToWideChar(CP_UTF8, 0, utf8.data(), (int)utf8.size(), &ret[0], wLen);
return ret;
}
static std::wstring GetResString(UINT id) {
const wchar_t* pBuf = nullptr;
int len = LoadStringW(GetModuleHandle(NULL), id, (LPWSTR)&pBuf, 0);
if (len > 0 && pBuf) return std::wstring(pBuf, len);
return L"";
}
const int MAX_RECENT_FILES = 60;
const UINT IDM_RECENT_SEP = 0x7FF0;
const UINT IDM_RECENT_BASE = 0x8000;
const size_t MAX_RECENT_MENU_ITEMS = 20;
static std::vector<std::wstring> s_sysMenuRecentFiles;
static std::vector<std::wstring> loadRecentFiles() {
std::vector<std::wstring> list;
HKEY hKey;
if (RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\miu\\RecentFiles", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
for (int i = 0; i < MAX_RECENT_FILES; ++i) {
std::wstring valName = std::to_wstring(i);
wchar_t path[MAX_PATH];
DWORD size = sizeof(path);
DWORD type = 0;
if (RegQueryValueExW(hKey, valName.c_str(), NULL, &type, (LPBYTE)path, &size) == ERROR_SUCCESS && type == REG_SZ && path[0] != L'\0') {
list.push_back(path);
}
}
RegCloseKey(hKey);
}
return list;
}
static void saveRecentFiles(const std::vector<std::wstring>& list) {
HKEY hKey;
if (RegCreateKeyExW(HKEY_CURRENT_USER, L"Software\\miu\\RecentFiles", 0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
for (int i = 0; i < MAX_RECENT_FILES; ++i) {
std::wstring valName = std::to_wstring(i);
RegDeleteValueW(hKey, valName.c_str());
}
int count = std::min((int)list.size(), MAX_RECENT_FILES);
for (int i = 0; i < count; ++i) {
std::wstring valName = std::to_wstring(i);
const std::wstring& path = list[i];
RegSetValueExW(hKey, valName.c_str(), 0, REG_SZ, (const BYTE*)path.c_str(), (DWORD)((path.size() + 1) * sizeof(wchar_t)));
}
RegCloseKey(hKey);
}
}
static std::wstring normalizePath(const std::wstring& p) {
std::wstring s = p;
while (!s.empty() && (s.front() == L' ' || s.front() == L'\"' || s.front() == L'\'')) s.erase(s.begin());
while (!s.empty() && (s.back() == L' ' || s.back() == L'\"' || s.back() == L'\'')) s.pop_back();
if (s.empty()) return L"";
wchar_t fullPath[MAX_PATH];
if (GetFullPathNameW(s.c_str(), MAX_PATH, fullPath, NULL)) {
wchar_t longPath[MAX_PATH];
if (GetLongPathNameW(fullPath, longPath, MAX_PATH)) {
return longPath;
}
return fullPath;
}
return s;
}
static std::vector<std::wstring> extractRemovedPaths(IObjectArray* pRemoved) {
std::vector<std::wstring> toRemove;
if (!pRemoved) return toRemove;
UINT count = 0;
if (FAILED(pRemoved->GetCount(&count)) || count == 0) return toRemove;
wchar_t exePath[MAX_PATH] = {};
GetModuleFileNameW(NULL, exePath, MAX_PATH);
std::wstring normExe = normalizePath(exePath);
for (UINT i = 0; i < count; ++i) {
IUnknown* pUnk = nullptr;
if (SUCCEEDED(pRemoved->GetAt(i, IID_PPV_ARGS(&pUnk)))) {
IShellItem* pItem = nullptr;
if (SUCCEEDED(pUnk->QueryInterface(IID_PPV_ARGS(&pItem)))) {
LPWSTR pszPath = nullptr;
if (SUCCEEDED(pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszPath)) && pszPath) {
std::wstring norm = normalizePath(pszPath);
if (!norm.empty()) toRemove.push_back(norm);
CoTaskMemFree(pszPath);
} else if (SUCCEEDED(pItem->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &pszPath)) && pszPath) {
std::wstring norm = normalizePath(pszPath);
if (!norm.empty()) toRemove.push_back(norm);
CoTaskMemFree(pszPath);
}
pItem->Release();
}
IShellLinkW* pLink = nullptr;
if (SUCCEEDED(pUnk->QueryInterface(IID_PPV_ARGS(&pLink)))) {
wchar_t args[MAX_PATH] = {};
if (SUCCEEDED(pLink->GetArguments(args, MAX_PATH)) && args[0] != L'\0') {
std::wstring norm = normalizePath(args);
if (!norm.empty()) toRemove.push_back(norm);
}
wchar_t path[MAX_PATH] = {};
if (SUCCEEDED(pLink->GetPath(path, MAX_PATH, NULL, 0)) && path[0] != L'\0') {
std::wstring norm = normalizePath(path);
if (!norm.empty() && _wcsicmp(norm.c_str(), normExe.c_str()) != 0) {
toRemove.push_back(norm);
}
}
pLink->Release();
}
pUnk->Release();
}
}
return toRemove;
}
static void removeFromRecentFiles(const std::vector<std::wstring>& toRemove) {
if (toRemove.empty()) return;
std::vector<std::wstring> normRemove;
for (const auto& r : toRemove) {
std::wstring n = normalizePath(r);
if (!n.empty()) normRemove.push_back(n);
}
if (normRemove.empty()) return;
std::vector<std::wstring> current = loadRecentFiles();
std::vector<std::wstring> updated;
for (const auto& item : current) {
std::wstring normItem = normalizePath(item);
bool match = false;
for (const auto& rem : normRemove) {
if (_wcsicmp(normItem.c_str(), rem.c_str()) == 0) {
match = true;
break;
}
}
if (!match) updated.push_back(item);
}
if (updated.size() != current.size()) {
saveRecentFiles(updated);
}
}
static void updateJumpList() {
CoInitialize(NULL);
ICustomDestinationList* pDestList = nullptr;
if (FAILED(CoCreateInstance(CLSID_DestinationList, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDestList)))) {
return;
}
pDestList->SetAppID(L"miu");
UINT cMinSlots = 0;
IObjectArray* pRemoved = nullptr;
if (FAILED(pDestList->BeginList(&cMinSlots, IID_PPV_ARGS(&pRemoved)))) {
pDestList->Release();
return;
}
if (pRemoved) {
std::vector<std::wstring> toRemove = extractRemovedPaths(pRemoved);
if (!toRemove.empty()) {
removeFromRecentFiles(toRemove);
}
pRemoved->Release();
}
std::vector<std::wstring> recentList = loadRecentFiles();
std::vector<std::wstring> validList;
for (const auto& p : recentList) {
DWORD attrs = GetFileAttributesW(p.c_str());
if (attrs != INVALID_FILE_ATTRIBUTES && !(attrs & FILE_ATTRIBUTE_DIRECTORY)) {
validList.push_back(p);
}
}
if (validList.size() != recentList.size()) {
saveRecentFiles(validList);
}
UINT maxSlots = (cMinSlots > 0) ? std::min((UINT)validList.size(), cMinSlots) : (UINT)validList.size();
if (maxSlots > 0) {
IObjectCollection* pCollection = nullptr;
if (SUCCEEDED(CoCreateInstance(CLSID_EnumerableObjectCollection, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pCollection)))) {
wchar_t exePath[MAX_PATH];
GetModuleFileNameW(NULL, exePath, MAX_PATH);
for (UINT i = 0; i < maxSlots; ++i) {
const std::wstring& filePath = validList[i];
IShellLinkW* pLink = nullptr;
if (SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pLink)))) {
pLink->SetPath(exePath);
std::wstring arg = L"\"" + filePath + L"\"";
pLink->SetArguments(arg.c_str());
std::wstring title = filePath;
size_t lastSlash = filePath.find_last_of(L"\\/");
if (lastSlash != std::wstring::npos) title = filePath.substr(lastSlash + 1);
IPropertyStore* pPropStore = nullptr;
if (SUCCEEDED(pLink->QueryInterface(IID_PPV_ARGS(&pPropStore)))) {
PROPVARIANT pv;
InitPropVariantFromString(title.c_str(), &pv);
pPropStore->SetValue(PKEY_Title, pv);
PropVariantClear(&pv);
pPropStore->Commit();
pPropStore->Release();
}
pLink->SetDescription(filePath.c_str());
pLink->SetIconLocation(exePath, 0);
pCollection->AddObject(pLink);
pLink->Release();
}
}
std::wstring catName = GetResString(IDS_RECENT_FILES);
if (catName.empty()) catName = L"Recent Files";
pDestList->AppendCategory(catName.c_str(), pCollection);
pCollection->Release();
}
}
pDestList->CommitList();
pDestList->Release();
}
static void syncRemovedJumpListItems() {
CoInitialize(NULL);
ICustomDestinationList* pDestList = nullptr;
if (FAILED(CoCreateInstance(CLSID_DestinationList, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDestList)))) {
return;
}
pDestList->SetAppID(L"miu");
IObjectArray* pRemoved = nullptr;
if (SUCCEEDED(pDestList->GetRemovedDestinations(IID_PPV_ARGS(&pRemoved)))) {
if (pRemoved) {
UINT count = 0;
if (SUCCEEDED(pRemoved->GetCount(&count)) && count > 0) {
std::vector<std::wstring> toRemove = extractRemovedPaths(pRemoved);
pRemoved->Release();
pDestList->Release();
if (!toRemove.empty()) {
removeFromRecentFiles(toRemove);
}
updateJumpList();
return;
}
pRemoved->Release();
}
}
pDestList->Release();
}
static void addToRecentFiles(const std::wstring& path) {
if (path.empty()) return;
std::wstring normalizedPath = normalizePath(path);
if (normalizedPath.empty()) return;
DWORD attrs = GetFileAttributesW(normalizedPath.c_str());
if (attrs == INVALID_FILE_ATTRIBUTES || (attrs & FILE_ATTRIBUTE_DIRECTORY)) return;
std::vector<std::wstring> list = loadRecentFiles();
std::vector<std::wstring> updated;
updated.push_back(normalizedPath);
for (const auto& item : list) {
if (_wcsicmp(item.c_str(), normalizedPath.c_str()) != 0) {
updated.push_back(item);
}
if (updated.size() >= MAX_RECENT_FILES) break;
}
saveRecentFiles(updated);
updateJumpList();
SHAddToRecentDocs(SHARD_PATHW, normalizedPath.c_str());
}
enum PreferredAppMode {
PAM_DEFAULT = 0,
PAM_ALLOW_DARK = 1,
PAM_FORCE_DARK = 2,
PAM_FORCE_LIGHT = 3,
PAM_MAX = 4
};
using fnSetPreferredAppMode = PreferredAppMode (WINAPI *)(PreferredAppMode appMode);
using fnAllowDarkModeForWindow = BOOL (WINAPI *)(HWND hWnd, BOOL allow);
using fnFlushMenuThemes = void (WINAPI *)();
static void InitDarkMode() {
HMODULE hUxtheme = GetModuleHandleW(L"uxtheme.dll");
if (!hUxtheme) hUxtheme = LoadLibraryExW(L"uxtheme.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
if (hUxtheme) {
auto pSetPreferredAppMode = (fnSetPreferredAppMode)GetProcAddress(hUxtheme, MAKEINTRESOURCEA(135));
if (pSetPreferredAppMode) {
pSetPreferredAppMode(PAM_ALLOW_DARK);
}
auto pFlushMenuThemes = (fnFlushMenuThemes)GetProcAddress(hUxtheme, MAKEINTRESOURCEA(136));
if (pFlushMenuThemes) {
pFlushMenuThemes();
}
}
}
static void EnableDarkModeForWindow(HWND hwnd, BOOL enable) {
if (!hwnd) return;
HMODULE hUxtheme = GetModuleHandleW(L"uxtheme.dll");
if (hUxtheme) {
auto pAllowDarkModeForWindow = (fnAllowDarkModeForWindow)GetProcAddress(hUxtheme, MAKEINTRESOURCEA(133));
if (pAllowDarkModeForWindow) {
pAllowDarkModeForWindow(hwnd, enable);
}
}
}
static void FlushMenuThemesInternal() {
HMODULE hUxtheme = GetModuleHandleW(L"uxtheme.dll");
if (hUxtheme) {
auto pFlushMenuThemes = (fnFlushMenuThemes)GetProcAddress(hUxtheme, MAKEINTRESOURCEA(136));
if (pFlushMenuThemes) {
pFlushMenuThemes();
}
}
}
static void updateSystemMenu(HWND hwnd) {
if (!hwnd) return;
syncRemovedJumpListItems();
HMENU hSysMenu = GetSystemMenu(hwnd, FALSE);
if (!hSysMenu) return;
DeleteMenu(hSysMenu, IDM_RECENT_SEP, MF_BYCOMMAND);
for (UINT id = IDM_RECENT_BASE; id < IDM_RECENT_BASE + (UINT)(MAX_RECENT_MENU_ITEMS * 16); id += 16) {
DeleteMenu(hSysMenu, id, MF_BYCOMMAND);
}
s_sysMenuRecentFiles.clear();
std::vector<std::wstring> recentList = loadRecentFiles();
size_t count = std::min(recentList.size(), MAX_RECENT_MENU_ITEMS);
if (count > 0) {
AppendMenuW(hSysMenu, MF_SEPARATOR, IDM_RECENT_SEP, NULL);
for (size_t i = 0; i < count; ++i) {
const std::wstring& path = recentList[i];
s_sysMenuRecentFiles.push_back(path);
wchar_t dispPath[MAX_PATH];
if (path.length() > 60) {
PathCompactPathExW(dispPath, path.c_str(), 60, 0);
} else {
wcscpy_s(dispPath, path.c_str());
}
std::wstring escaped;
for (const wchar_t* p = dispPath; *p; ++p) {
if (*p == L'&') escaped += L"&&";
else escaped += *p;
}
std::wstring itemText;
if (i < 9) {
itemText = L"&" + std::to_wstring(i + 1) + L" " + escaped;
} else if (i == 9) {
itemText = L"1&0 " + escaped;
} else {
itemText = std::to_wstring(i + 1) + L" " + escaped;
}
UINT itemID = IDM_RECENT_BASE + (UINT)(i * 16);
AppendMenuW(hSysMenu, MF_STRING, itemID, itemText.c_str());
}
}
}
static std::string WToUTF8(const std::wstring& w) {
if (w.empty()) return {};
int n = WideCharToMultiByte(CP_UTF8, 0, w.data(), (int)w.size(), NULL, 0, NULL, NULL);
if (n <= 0) return {};
std::string s; s.resize(n);
WideCharToMultiByte(CP_UTF8, 0, w.data(), (int)w.size(), &s[0], n, NULL, NULL);
return s;
}
static std::string UnescapeString(const std::string& s, const std::string& newline) {
std::string out; out.reserve(s.size());
for (size_t i = 0; i < s.size(); ++i) {
if (s[i] == '\\' && i + 1 < s.size()) {
switch (s[i + 1]) {
case 'n': out += newline; break;
case 'r': out += '\r'; break;
case 't': out += '\t'; break;
case '\\': out += '\\'; break;
default: out += s[i]; out += s[i + 1]; break;
}
i++;
}
else out += s[i];
}
return out;
}
struct Piece { bool isOriginal; size_t start; size_t len; };
struct PieceTable {
const char* origPtr = nullptr; size_t origSize = 0; std::string addBuf; std::vector<Piece> pieces;
void initFromFile(const char* data, size_t size) { origPtr = data; origSize = size; pieces.clear(); addBuf.clear(); if (size > 0) pieces.push_back({ true, 0, size }); }
void initEmpty() { origPtr = nullptr; origSize = 0; pieces.clear(); addBuf.clear(); }
size_t length() const { size_t s = 0; for (auto& p : pieces) s += p.len; return s; }
std::string getRange(size_t pos, size_t count) const {
std::string out; getRange(pos, count, out); return out;
}
void getRange(size_t pos, size_t count, std::string& out) const {
out.clear(); if (count == 0) return;
out.reserve(count); size_t cur = 0;
for (const auto& p : pieces) {
if (cur + p.len <= pos) { cur += p.len; continue; }
size_t localStart = (pos > cur) ? (pos - cur) : 0; size_t take = std::min(p.len - localStart, count - out.size());
if (take == 0) break;
if (p.isOriginal) out.append(origPtr + p.start + localStart, take); else out.append(addBuf.data() + p.start + localStart, take);
if (out.size() >= count) break; cur += p.len;
}
}
void insert(size_t pos, const std::string& s) {
if (s.empty()) return; size_t cur = 0; size_t idx = 0;
while (idx < pieces.size() && cur + pieces[idx].len < pos) { cur += pieces[idx].len; ++idx; }
if (idx < pieces.size()) {
Piece p = pieces[idx]; size_t offsetInPiece = pos - cur;
if (offsetInPiece > 0 && offsetInPiece < p.len) { pieces[idx] = { p.isOriginal, p.start, offsetInPiece }; pieces.insert(pieces.begin() + idx + 1, { p.isOriginal, p.start + offsetInPiece, p.len - offsetInPiece }); idx++; }
else if (offsetInPiece == p.len) idx++;
}
else idx = pieces.size();
size_t addStart = addBuf.size(); addBuf.append(s); pieces.insert(pieces.begin() + idx, { false, addStart, s.size() }); coalesceAround(idx);
}
void erase(size_t pos, size_t count) {
if (count == 0) return; size_t cur = 0; size_t idx = 0;
while (idx < pieces.size() && cur + pieces[idx].len <= pos) { cur += pieces[idx].len; ++idx; }
size_t remaining = count; if (idx >= pieces.size()) return;
if (pos > cur) { Piece p = pieces[idx]; size_t leftLen = pos - cur; pieces[idx] = { p.isOriginal, p.start, leftLen }; pieces.insert(pieces.begin() + idx + 1, { p.isOriginal, p.start + leftLen, p.len - leftLen }); idx++; }
while (idx < pieces.size() && remaining > 0) { if (pieces[idx].len <= remaining) { remaining -= pieces[idx].len; pieces.erase(pieces.begin() + idx); } else { pieces[idx].start += remaining; pieces[idx].len -= remaining; remaining = 0; } }
coalesceAround(idx > 0 ? idx - 1 : 0);
}
void coalesceAround(size_t idx) {
if (pieces.empty()) return; if (idx >= pieces.size()) idx = pieces.size() - 1;
if (idx > 0) { Piece& a = pieces[idx - 1]; Piece& b = pieces[idx]; if (!a.isOriginal && !b.isOriginal && (a.start + a.len == b.start)) { a.len += b.len; pieces.erase(pieces.begin() + idx); idx--; } }
if (idx + 1 < pieces.size()) { Piece& a = pieces[idx]; Piece& b = pieces[idx + 1]; if (!a.isOriginal && !b.isOriginal && (a.start + a.len == b.start)) { a.len += b.len; pieces.erase(pieces.begin() + idx + 1); } }
}
char charAt(size_t pos) const {
size_t cur = 0;
for (const auto& p : pieces) { if (cur + p.len <= pos) { cur += p.len; continue; } size_t local = pos - cur; if (p.isOriginal) return origPtr[p.start + local]; else return addBuf[p.start + local]; }
return ' ';
}
};
struct Cursor { size_t head; size_t anchor; float desiredX; bool affinity = false; size_t start() const { return std::min(head, anchor); } size_t end() const { return std::max(head, anchor); } bool hasSelection() const { return head != anchor; } void clearSelection() { anchor = head; } };
struct EditOp { enum Type { Insert, Erase } type; size_t pos; std::string text; };
struct EditBatch { std::vector<EditOp> ops; std::vector<Cursor> beforeCursors; std::vector<Cursor> afterCursors; };
struct UndoManager {
std::vector<EditBatch> undoStack; std::vector<EditBatch> redoStack; int savePoint = 0;
void clear() { undoStack.clear(); redoStack.clear(); savePoint = 0; }
void markSaved() { savePoint = (int)undoStack.size(); }
bool isModified() const { return (int)undoStack.size() != savePoint; }
void push(const EditBatch& batch) { if (savePoint > (int)undoStack.size()) savePoint = -1; undoStack.push_back(batch); redoStack.clear(); }
bool canUndo() const { return !undoStack.empty(); }
bool canRedo() const { return !redoStack.empty(); }
EditBatch popUndo() { EditBatch e = undoStack.back(); undoStack.pop_back(); redoStack.push_back(e); return e; }
EditBatch popRedo() { EditBatch e = redoStack.back(); redoStack.pop_back(); undoStack.push_back(e); return e; }
};
struct MappedFile {
HANDLE hFile = INVALID_HANDLE_VALUE; HANDLE hMap = NULL; const char* ptr = nullptr; size_t size = 0;
std::vector<char> heapBuffer; bool isMapped = false;
static const size_t THRESHOLD = 50 * 1024 * 1024; // 50MB
bool open(const wchar_t* path) {
hFile = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) return false;
LARGE_INTEGER li; if (!GetFileSizeEx(hFile, &li)) { CloseHandle(hFile); hFile = INVALID_HANDLE_VALUE; return false; }
size = (size_t)li.QuadPart;
if (size == 0) { ptr = nullptr; CloseHandle(hFile); hFile = INVALID_HANDLE_VALUE; return true; }
if (size >= THRESHOLD) {
hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
if (!hMap) { CloseHandle(hFile); hFile = INVALID_HANDLE_VALUE; return false; }
ptr = (const char*)MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0); isMapped = true; return !!ptr;
}
else {
heapBuffer.resize(size); DWORD bytesRead = 0;
if (ReadFile(hFile, heapBuffer.data(), (DWORD)size, &bytesRead, NULL) && bytesRead == size) {
ptr = heapBuffer.data(); isMapped = false; CloseHandle(hFile); hFile = INVALID_HANDLE_VALUE; return true;
}
CloseHandle(hFile); hFile = INVALID_HANDLE_VALUE; return false;
}
}
void close() {
if (isMapped && ptr) { UnmapViewOfFile(ptr); ptr = nullptr; }
if (hMap) { CloseHandle(hMap); hMap = NULL; }
if (hFile != INVALID_HANDLE_VALUE) { CloseHandle(hFile); hFile = INVALID_HANDLE_VALUE; }
heapBuffer.clear(); ptr = nullptr; size = 0; isMapped = false;
}
~MappedFile() { close(); }
};
#include <unordered_set>
#include <unordered_map>
#include <cwctype>
struct SyntaxDef {
std::unordered_set<std::string> keywords;
std::unordered_set<std::string> literals;
std::string commentLine;
std::string commentBlockStart;
std::string commentBlockEnd;
std::vector<std::pair<std::regex, D2D1::ColorF>> regexes;
};
class CustomTextRenderer : public IDWriteTextRenderer {
public:
ID2D1DeviceContext* rend;
ID2D1SolidColorBrush* defaultBrush;
CustomTextRenderer(ID2D1DeviceContext* r, ID2D1SolidColorBrush* b) : rend(r), defaultBrush(b) {}
IFACEMETHOD(DrawGlyphRun)(void*, FLOAT baselineOriginX, FLOAT baselineOriginY, DWRITE_MEASURING_MODE measuringMode, DWRITE_GLYPH_RUN const* glyphRun, DWRITE_GLYPH_RUN_DESCRIPTION const*, IUnknown* clientDrawingEffect) override {
ID2D1SolidColorBrush* brush = defaultBrush;
if (clientDrawingEffect) clientDrawingEffect->QueryInterface(__uuidof(ID2D1SolidColorBrush), (void**)&brush);
rend->DrawGlyphRun(D2D1::Point2F(baselineOriginX, baselineOriginY), glyphRun, brush, measuringMode);
if (clientDrawingEffect && brush != defaultBrush) brush->Release();
return S_OK;
}
IFACEMETHOD(DrawUnderline)(void*, FLOAT, FLOAT, DWRITE_UNDERLINE const*, IUnknown*) override { return E_NOTIMPL; }
IFACEMETHOD(DrawStrikethrough)(void*, FLOAT, FLOAT, DWRITE_STRIKETHROUGH const*, IUnknown*) override { return E_NOTIMPL; }
IFACEMETHOD(DrawInlineObject)(void*, FLOAT, FLOAT, IDWriteInlineObject*, BOOL, BOOL, IUnknown*) override { return E_NOTIMPL; }
IFACEMETHOD(IsPixelSnappingDisabled)(void*, BOOL* disabled) override { *disabled = FALSE; return S_OK; }
IFACEMETHOD(GetCurrentTransform)(void*, DWRITE_MATRIX* m) override { rend->GetTransform((D2D1_MATRIX_3X2_F*)m); return S_OK; }
IFACEMETHOD(GetPixelsPerDip)(void*, FLOAT* pixelsPerDip) override { *pixelsPerDip = 1.0f; return S_OK; }
IFACEMETHODIMP_(ULONG) AddRef() { return 1; }
IFACEMETHODIMP_(ULONG) Release() { return 1; }
IFACEMETHODIMP QueryInterface(REFIID riid, void** ppv) {
if (riid == __uuidof(IDWriteTextRenderer) || riid == __uuidof(IDWritePixelSnapping) || riid == __uuidof(IUnknown)) { *ppv = this; return S_OK; }
*ppv = NULL; return E_NOINTERFACE;
}
};
struct CustomDialogButton {
int id = 0;
std::wstring text;
int width = 0;
int height = 0;
int x = 0;
int y = 0;
HWND hwnd = NULL;
bool isDefault = false;
bool isCancel = false;
};
struct CustomTaskDialogData {
HWND hDlg = NULL;
HWND hwndParent = NULL;
bool isDarkMode = false;
UINT dpi = 96;
std::wstring title;
std::wstring instruction;
std::wstring content;
bool hasContent = false;
HICON hIcon = NULL;
int iconSize = 32;
HFONT hFontInstruction = NULL;
HFONT hFontContent = NULL;
HFONT hFontButton = NULL;
int padX = 0;
int padY = 0;
int textLeft = 0;
int iconTextGap = 0;
int textGap = 0;
int instW = 0;
int instH = 0;
int contentW = 0;
int contentH = 0;
int upperHeight = 0;
int lowerHeight = 0;
int clientWidth = 0;
int clientHeight = 0;
std::vector<CustomDialogButton> buttons;
int resultId = 0;
int cancelId = IDCANCEL;
HWND hDefaultBtn = NULL;
};
static std::wstring GetStandardButtonText(int id) {
UINT resId = 0;
switch (id) {
case IDOK: resId = IDS_BTN_OK; break;
case IDYES: resId = IDS_BTN_YES; break;
case IDNO: resId = IDS_BTN_NO; break;
case IDCANCEL: resId = IDS_BTN_CANCEL; break;
}
if (resId != 0) {
std::wstring s = GetResString(resId);
if (!s.empty()) return s;
}
switch (id) {
case IDOK: return L"OK";
case IDYES: return L"縺ッ縺・&Y)";
case IDNO: return L"縺・>縺・&N)";
case IDCANCEL: return L"繧ュ繝」繝ウ繧サ繝ォ";
case IDRETRY: return L"蜀崎ゥヲ陦・&R)";
case IDCLOSE: return L"髢峨§繧・&C)";
default: return L"";
}
}
static LRESULT CALLBACK DarkButtonSubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData) {
CustomTaskDialogData* pData = (CustomTaskDialogData*)dwRefData;
switch (uMsg) {
case WM_MOUSEMOVE: {
BOOL isHovered = (BOOL)(INT_PTR)GetPropW(hWnd, L"MiuHover");
if (!isHovered) {
SetPropW(hWnd, L"MiuHover", (HANDLE)(INT_PTR)1);
TRACKMOUSEEVENT tme = { sizeof(TRACKMOUSEEVENT), TME_LEAVE, hWnd, 0 };
TrackMouseEvent(&tme);
InvalidateRect(hWnd, NULL, FALSE);
}
break;
}
case WM_MOUSELEAVE: {
SetPropW(hWnd, L"MiuHover", (HANDLE)(INT_PTR)0);
InvalidateRect(hWnd, NULL, FALSE);
break;
}
case WM_LBUTTONDOWN: {
SetPropW(hWnd, L"MiuPressed", (HANDLE)(INT_PTR)1);
InvalidateRect(hWnd, NULL, FALSE);
break;
}
case WM_LBUTTONUP: {
SetPropW(hWnd, L"MiuPressed", (HANDLE)(INT_PTR)0);
InvalidateRect(hWnd, NULL, FALSE);
break;
}
case WM_SETFOCUS:
case WM_KILLFOCUS: {
InvalidateRect(hWnd, NULL, FALSE);
break;
}
case WM_ERASEBKGND:
return 1;
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
RECT rc;
GetClientRect(hWnd, &rc);
HDC memDC = CreateCompatibleDC(hdc);
HBITMAP memBmp = CreateCompatibleBitmap(hdc, rc.right, rc.bottom);
HBITMAP oldBmp = (HBITMAP)SelectObject(memDC, memBmp);
COLORREF parentBg = (COLORREF)(INT_PTR)GetPropW(hWnd, L"MiuParentBg");
if (parentBg == 0) parentBg = RGB(32, 32, 32);
HBRUSH hbrBg = CreateSolidBrush(parentBg);
FillRect(memDC, &rc, hbrBg);
DeleteObject(hbrBg);
BOOL isHovered = (BOOL)(INT_PTR)GetPropW(hWnd, L"MiuHover");
BOOL isPressed = (BOOL)(INT_PTR)GetPropW(hWnd, L"MiuPressed");
if (SendMessageW(hWnd, BM_GETSTATE, 0, 0) & BST_PUSHED) {
isPressed = TRUE;
}
bool isFocused = (GetFocus() == hWnd);
bool isDefault = (GetWindowLongPtrW(hWnd, GWL_STYLE) & BS_DEFPUSHBUTTON) != 0;
COLORREF bgCol;
COLORREF borderCol;
if (isPressed) {
bgCol = RGB(40, 40, 40);
borderCol = RGB(80, 80, 80);
} else if (isHovered) {
bgCol = RGB(65, 65, 65);
borderCol = RGB(120, 120, 120);
} else {
bgCol = RGB(50, 50, 50);
borderCol = isDefault ? RGB(0, 120, 215) : RGB(85, 85, 85);
}
if (isFocused && !isPressed) {
borderCol = RGB(76, 194, 255);
}
HBRUSH hbrBtn = CreateSolidBrush(bgCol);
HPEN hPen = CreatePen(PS_SOLID, 1, borderCol);
HBRUSH oldBrush = (HBRUSH)SelectObject(memDC, hbrBtn);
HPEN oldPen = (HPEN)SelectObject(memDC, hPen);
UINT dpi = pData ? pData->dpi : GetDpiForWindow(hWnd);
if (dpi == 0) dpi = 96;
int radius = MulDiv(4, dpi, 96);
RoundRect(memDC, rc.left, rc.top, rc.right, rc.bottom, radius * 2, radius * 2);
SelectObject(memDC, oldBrush);
SelectObject(memDC, oldPen);
DeleteObject(hbrBtn);
DeleteObject(hPen);
WCHAR text[128];
GetWindowTextW(hWnd, text, ARRAYSIZE(text));
HFONT hFont = pData ? pData->hFontButton : (HFONT)SendMessageW(hWnd, WM_GETFONT, 0, 0);
if (hFont) SelectObject(memDC, hFont);
SetBkMode(memDC, TRANSPARENT);
SetTextColor(memDC, isPressed ? RGB(200, 200, 200) : RGB(255, 255, 255));
DrawTextW(memDC, text, -1, &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
BitBlt(hdc, 0, 0, rc.right, rc.bottom, memDC, 0, 0, SRCCOPY);
SelectObject(memDC, oldBmp);
DeleteObject(memBmp);
DeleteDC(memDC);
EndPaint(hWnd, &ps);
return 0;
}
case WM_NCDESTROY:
RemovePropW(hWnd, L"MiuHover");
RemovePropW(hWnd, L"MiuPressed");
RemovePropW(hWnd, L"MiuParentBg");
RemoveWindowSubclass(hWnd, DarkButtonSubclassProc, uIdSubclass);
break;
}
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}
static LRESULT CALLBACK DarkCheckboxSubclass(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData) {
switch (uMsg) {
case WM_MOUSEMOVE: {
BOOL isHovered = (BOOL)(INT_PTR)GetPropW(hWnd, L"MiuHover");
if (!isHovered) {
SetPropW(hWnd, L"MiuHover", (HANDLE)(INT_PTR)1);
TRACKMOUSEEVENT tme = { sizeof(TRACKMOUSEEVENT), TME_LEAVE, hWnd, 0 };
TrackMouseEvent(&tme);
InvalidateRect(hWnd, NULL, FALSE);
}
break;
}
case WM_MOUSELEAVE: {
SetPropW(hWnd, L"MiuHover", (HANDLE)(INT_PTR)0);
InvalidateRect(hWnd, NULL, FALSE);
break;
}
case WM_LBUTTONUP:
case WM_KEYUP: {
LRESULT res = DefSubclassProc(hWnd, uMsg, wParam, lParam);
InvalidateRect(hWnd, NULL, FALSE);
return res;
}
case BM_SETCHECK: {
LRESULT res = DefSubclassProc(hWnd, uMsg, wParam, lParam);
InvalidateRect(hWnd, NULL, FALSE);
return res;
}
case WM_SETFOCUS:
case WM_KILLFOCUS: {
InvalidateRect(hWnd, NULL, FALSE);
break;
}
case WM_ERASEBKGND:
return 1;
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
RECT rc;
GetClientRect(hWnd, &rc);
HDC memDC = CreateCompatibleDC(hdc);
HBITMAP memBmp = CreateCompatibleBitmap(hdc, rc.right, rc.bottom);
HBITMAP oldBmp = (HBITMAP)SelectObject(memDC, memBmp);
COLORREF parentBg = (COLORREF)(INT_PTR)GetPropW(hWnd, L"MiuParentBg");
if (parentBg == 0) parentBg = RGB(32, 32, 32);
HBRUSH hbrBg = CreateSolidBrush(parentBg);
FillRect(memDC, &rc, hbrBg);
DeleteObject(hbrBg);
BOOL isChecked = (SendMessageW(hWnd, BM_GETCHECK, 0, 0) == BST_CHECKED);
BOOL isHovered = (BOOL)(INT_PTR)GetPropW(hWnd, L"MiuHover");
bool isFocused = (GetFocus() == hWnd);
UINT dpi = GetDpiForWindow(hWnd);
if (dpi == 0) dpi = 96;
int boxSize = MulDiv(14, dpi, 96);
int boxY = (rc.bottom - boxSize) / 2;
int boxX = 0;
RECT rcBox = { boxX, boxY, boxX + boxSize, boxY + boxSize };
COLORREF boxBg = isChecked ? (isHovered ? RGB(0, 140, 240) : RGB(0, 120, 215)) : (isHovered ? RGB(45, 45, 45) : RGB(35, 35, 35));
COLORREF boxBorder = isFocused ? RGB(76, 194, 255) : (isHovered ? RGB(140, 140, 140) : RGB(100, 100, 100));
HBRUSH hbrBox = CreateSolidBrush(boxBg);
HPEN hPenBox = CreatePen(PS_SOLID, 1, boxBorder);
HBRUSH oldBr = (HBRUSH)SelectObject(memDC, hbrBox);
HPEN oldP = (HPEN)SelectObject(memDC, hPenBox);
int r = MulDiv(2, dpi, 96);
RoundRect(memDC, rcBox.left, rcBox.top, rcBox.right, rcBox.bottom, r * 2, r * 2);
SelectObject(memDC, oldBr);
SelectObject(memDC, oldP);
DeleteObject(hbrBox);
DeleteObject(hPenBox);
if (isChecked) {
HPEN hCheckPen = CreatePen(PS_SOLID, MulDiv(2, dpi, 96), RGB(255, 255, 255));
HPEN oldCp = (HPEN)SelectObject(memDC, hCheckPen);
int x1 = rcBox.left + MulDiv(3, dpi, 96);
int y1 = rcBox.top + MulDiv(7, dpi, 96);
int x2 = rcBox.left + MulDiv(6, dpi, 96);
int y2 = rcBox.top + MulDiv(10, dpi, 96);
int x3 = rcBox.left + MulDiv(11, dpi, 96);
int y3 = rcBox.top + MulDiv(4, dpi, 96);
MoveToEx(memDC, x1, y1, NULL);
LineTo(memDC, x2, y2);
LineTo(memDC, x3, y3);
SelectObject(memDC, oldCp);
DeleteObject(hCheckPen);
}
WCHAR text[128];
GetWindowTextW(hWnd, text, ARRAYSIZE(text));
HFONT hFont = (HFONT)SendMessageW(hWnd, WM_GETFONT, 0, 0);
if (hFont) SelectObject(memDC, hFont);
SetBkMode(memDC, TRANSPARENT);
SetTextColor(memDC, RGB(230, 230, 230));
RECT rcText = { rcBox.right + MulDiv(6, dpi, 96), 0, rc.right, rc.bottom };
DrawTextW(memDC, text, -1, &rcText, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
if (isFocused) {
RECT rcFocus = rcText;
DrawTextW(memDC, text, -1, &rcFocus, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
InflateRect(&rcFocus, 1, 1);
DrawFocusRect(memDC, &rcFocus);
}
BitBlt(hdc, 0, 0, rc.right, rc.bottom, memDC, 0, 0, SRCCOPY);
SelectObject(memDC, oldBmp);
DeleteObject(memBmp);
DeleteDC(memDC);
EndPaint(hWnd, &ps);
return 0;
}
case WM_NCDESTROY:
RemovePropW(hWnd, L"MiuHover");
RemovePropW(hWnd, L"MiuParentBg");
RemoveWindowSubclass(hWnd, DarkCheckboxSubclass, uIdSubclass);