Repository navigation
Expand file tree
/
Copy pathMainWnd.Icons.cpp
More file actions
1911 lines (1782 loc) · 78.4 KB
/
Copy pathMainWnd.Icons.cpp
File metadata and controls
1911 lines (1782 loc) · 78.4 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
// FastFile - Shell icon and thumbnail extraction, caches, worker thread
// Implements CMainWnd members moved out of the original monolithic MainWnd.cpp.
// Behaviour is unchanged; declarations live in MainWnd.h.
#include "MainWndInternal.h"
bool CMainWnd::IsImageExtension(const std::wstring& name)
{
size_t dot = name.find_last_of(L'.');
if (dot == std::wstring::npos) return false;
std::wstring ext = name.substr(dot);
for (auto& ch : ext) ch = static_cast<wchar_t>(towlower(ch));
return ext == L".png" || ext == L".jpg" || ext == L".jpeg"
|| ext == L".bmp" || ext == L".gif" || ext == L".tif" || ext == L".tiff"
|| ext == L".webp" || ext == L".ico";
}
bool CMainWnd::IsVideoExtension(const std::wstring& name)
{
const wchar_t* ext = ::PathFindExtensionW(name.c_str());
if (!ext || !*ext) return false;
static const wchar_t* kExts[] = {
L".mp4", L".mkv", L".avi", L".wmv", L".mov", L".m4v",
L".webm", L".flv", L".mpeg", L".mpg", L".ts", L".m2ts",
L".3gp", L".asf", L".vob"
};
for (auto e : kExts) {
if (_wcsicmp(ext, e) == 0) return true;
}
return false;
}
bool CMainWnd::EnsureGdiplus()
{
using namespace Gdiplus;
// Called from the UI thread *and* the thumbnail worker, so the one-time startup has to be
// thread-safe: two racing GdiplusStartup calls leave GDI+ with a clobbered token and a
// half-initialised state, which showed up as rare access violations inside GDI+.
static std::once_flag once;
static bool ready = false;
std::call_once(once, [] {
GdiplusStartupInput input;
ULONG_PTR token = 0;
ready = (GdiplusStartup(&token, &input, nullptr) == Ok);
});
return ready;
}
bool CMainWnd::GetPngEncoderClsid(CLSID* pClsid)
{
if (!pClsid) return false;
using namespace Gdiplus;
// Both the UI thread and the thumbnail worker save PNGs, so resolve the encoder once
// instead of enumerating GDI+ encoders (which is not cheap and is shared state) per save.
static std::once_flag once;
static CLSID png = {};
static bool found = false;
std::call_once(once, [] {
UINT num = 0, size = 0;
GetImageEncodersSize(&num, &size);
if (size == 0) return;
std::vector<BYTE> buf(size);
auto* info = reinterpret_cast<ImageCodecInfo*>(buf.data());
GetImageEncoders(num, size, info);
for (UINT i = 0; i < num; ++i) {
if (wcscmp(info[i].MimeType, L"image/png") == 0) {
png = info[i].Clsid;
found = true;
return;
}
}
});
if (!found) return false;
*pClsid = png;
return true;
}
void CMainWnd::WipeDirectoryFiles(const std::wstring& dirNoSlash)
{
if (dirNoSlash.empty()) return;
WIN32_FIND_DATAW fd = {};
const std::wstring pattern = dirNoSlash + L"\\*";
HANDLE h = ::FindFirstFileW(pattern.c_str(), &fd);
if (h == INVALID_HANDLE_VALUE) return;
do {
if (fd.cFileName[0] == L'.' &&
(fd.cFileName[1] == 0 || (fd.cFileName[1] == L'.' && fd.cFileName[2] == 0)))
continue;
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
continue;
const std::wstring full = dirNoSlash + L"\\" + fd.cFileName;
::DeleteFileW(full.c_str());
} while (::FindNextFileW(h, &fd));
::FindClose(h);
}
// ---- Persistent icon cache ------------------------------------------------------------
// The PNG cache survives relaunches. Invalidation is by name, not by wiping:
// * kIconCacheVersion names the directory; a format change bumps it and the
// background maintenance removes the old directories / legacy flat files.
// * file / folder entries hash the source's size and last-write time into the name,
// so an edited file never reuses a previous launch's image.
// * items without a stable stamp (drive roots, virtual items) and preview scratch
// images are session files ("s_<tag>_"), removed by the next launch.
// FASTFILE_ICON_CACHE_DIR redirects the root (regression tests use their own folder
// so a test run never touches the user's cache).
const wchar_t* const CMainWnd::kIconCacheVersion = L"v9";
std::wstring CMainWnd::ResolveIconCacheRoot()
{
std::wstring root;
wchar_t env[MAX_PATH * 2] = {};
const DWORD n = ::GetEnvironmentVariableW(L"FASTFILE_ICON_CACHE_DIR", env, _countof(env));
if (n > 0 && n < _countof(env)) {
root = env;
} else {
wchar_t tmp[MAX_PATH] = {};
::GetTempPathW(MAX_PATH, tmp);
root = tmp;
if (!root.empty() && root.back() != L'\\') root.push_back(L'\\');
root += L"FastFileIconCache";
}
while (root.size() > 3 && (root.back() == L'\\' || root.back() == L'/')) root.pop_back();
return root;
}
void CMainWnd::InitIconCache()
{
m_iconCacheRoot = ResolveIconCacheRoot();
FILETIME now = {};
::GetSystemTimeAsFileTime(&now);
m_iconCacheSessionStart = (static_cast<ULONGLONG>(now.dwHighDateTime) << 32) | now.dwLowDateTime;
wchar_t tag[32] = {};
swprintf_s(tag, L"%08lX%08lX", ::GetCurrentProcessId(), static_cast<unsigned long>(m_iconCacheSessionStart >> 20));
m_iconSessionTag = tag;
const std::wstring dir = m_iconCacheRoot + L"\\" + kIconCacheVersion;
::SHCreateDirectoryExW(nullptr, dir.c_str(), nullptr);
m_iconCacheDir = dir + L"\\";
// Trim off the UI thread; it only touches files of other versions, other sessions,
// or entries older than the age cap, never this session's fresh files.
const std::wstring root = m_iconCacheRoot;
const ULONGLONG start = m_iconCacheSessionStart;
std::thread([root, start]() {
::SetThreadPriority(::GetCurrentThread(), THREAD_PRIORITY_LOWEST);
MaintainIconCache(root, kIconCacheVersion, start, kIconCacheMaxAgeDays, kIconCacheMaxBytes);
}).detach();
}
CMainWnd::IconCacheTrimResult CMainWnd::MaintainIconCache(const std::wstring& root,
const std::wstring& version, ULONGLONG sessionStart, int maxAgeDays, ULONGLONG maxBytes)
{
IconCacheTrimResult result;
if (root.empty()) return result;
auto isCacheImage = [](const wchar_t* name) {
const wchar_t* ext = ::PathFindExtensionW(name);
return _wcsicmp(ext, L".png") == 0 || _wcsicmp(ext, L".bmp") == 0;
};
auto stamp = [](const FILETIME& t) { return (static_cast<ULONGLONG>(t.dwHighDateTime) << 32) | t.dwLowDateTime; };
WIN32_FIND_DATAW fd = {};
// Legacy flat layout (<= v8, written straight into the root) and other versions.
HANDLE h = ::FindFirstFileW((root + L"\\*").c_str(), &fd);
if (h != INVALID_HANDLE_VALUE) {
do {
const std::wstring name = fd.cFileName;
if (name == L"." || name == L"..") continue;
const std::wstring full = root + L"\\" + name;
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
const bool versionDir = name.size() > 1 && (name[0] == L'v' || name[0] == L'V')
&& std::all_of(name.begin() + 1, name.end(), [](wchar_t c) { return c >= L'0' && c <= L'9'; });
if (versionDir && _wcsicmp(name.c_str(), version.c_str()) != 0) {
WipeDirectoryFiles(full);
if (::RemoveDirectoryW(full.c_str())) ++result.removedVersionDirs;
}
} else if (isCacheImage(fd.cFileName) && ::DeleteFileW(full.c_str())) {
++result.removedLegacy;
}
} while (::FindNextFileW(h, &fd));
::FindClose(h);
}
// Current version: session leftovers, age cap, then size cap (oldest first).
const std::wstring dir = root + L"\\" + version;
struct Entry { std::wstring path; ULONGLONG time; ULONGLONG bytes; };
std::vector<Entry> entries;
const ULONGLONG ageLimit = static_cast<ULONGLONG>(maxAgeDays) * 24ull * 3600ull * 10000000ull;
h = ::FindFirstFileW((dir + L"\\*").c_str(), &fd);
if (h != INVALID_HANDLE_VALUE) {
do {
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue;
const std::wstring full = dir + L"\\" + fd.cFileName;
const ULONGLONG written = stamp(fd.ftLastWriteTime);
const bool session = wcsncmp(fd.cFileName, L"s_", 2) == 0;
if (session && written < sessionStart) {
if (::DeleteFileW(full.c_str())) ++result.removedSession;
continue;
}
if (!session && maxAgeDays > 0 && sessionStart > written && sessionStart - written > ageLimit) {
if (::DeleteFileW(full.c_str())) ++result.removedAged;
continue;
}
const ULONGLONG bytes = (static_cast<ULONGLONG>(fd.nFileSizeHigh) << 32) | fd.nFileSizeLow;
entries.push_back({full, written, bytes});
result.keptBytes += bytes;
} while (::FindNextFileW(h, &fd));
::FindClose(h);
}
if (maxBytes > 0 && result.keptBytes > maxBytes) {
std::sort(entries.begin(), entries.end(), [](const Entry& a, const Entry& b) { return a.time < b.time; });
const ULONGLONG target = maxBytes / 4 * 3;
for (const Entry& e : entries) {
if (result.keptBytes <= target) break;
if (e.time >= sessionStart) continue; // written by this launch
if (::DeleteFileW(e.path.c_str())) { result.keptBytes -= e.bytes; ++result.removedOverCap; }
}
}
return result;
}
std::wstring CMainWnd::IconCacheLeaf(const std::wstring& key, const std::wstring& source, const wchar_t* tail) const
{
std::wstring stamped = key;
bool stable = false;
// Drive roots can be a different medium next launch; virtual items have no stamp.
if (source.size() > 3 && source.compare(0, 2, L"::") != 0) {
WIN32_FILE_ATTRIBUTE_DATA data = {};
if (::GetFileAttributesExW(source.c_str(), GetFileExInfoStandard, &data)) {
wchar_t buf[64] = {};
swprintf_s(buf, L"|%08lX%08lX|%08lX%08lX", data.nFileSizeHigh, data.nFileSizeLow,
data.ftLastWriteTime.dwHighDateTime, data.ftLastWriteTime.dwLowDateTime);
stamped += buf;
stable = true;
}
} else if (source.empty()) {
stable = true; // stock / glyph / command art: the key fully describes the image
}
const unsigned long long h = static_cast<unsigned long long>(std::hash<std::wstring>{}(stamped));
wchar_t name[128] = {};
if (stable)
swprintf_s(name, L"%016llX_%s", h, tail);
else
swprintf_s(name, L"s_%s_%016llX_%s", m_iconSessionTag.c_str(), h, tail);
return name;
}
std::wstring CMainWnd::SessionCacheFile(const wchar_t* leaf) const
{
return m_iconCacheDir + L"s_" + m_iconSessionTag + L"_" + leaf;
}
// Render an HICON 1:1 into a top-down 32bpp BGRA buffer.
// DrawIconEx *scaling* is unfiltered (the driver does a plain StretchBlt), which is
// where the jagged list / tile / preview icons came from. So we always rasterise at
// the icon's own bitmap size and leave any resampling to GDI+ below.
bool CMainWnd::RenderIconToArgbBuffer(HICON hIcon, int w, int h, std::vector<BYTE>& out)
{
out.clear();
if (!hIcon || w <= 0 || h <= 0 || w > 8192 || h > 8192) return false;
HDC hdc = ::GetDC(nullptr);
HDC mem = ::CreateCompatibleDC(hdc);
BITMAPINFO bi = {};
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bi.bmiHeader.biWidth = w;
bi.bmiHeader.biHeight = -h; // top-down
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 32;
bi.bmiHeader.biCompression = BI_RGB;
void* bits = nullptr;
HBITMAP dib = ::CreateDIBSection(hdc, &bi, DIB_RGB_COLORS, &bits, nullptr, 0);
if (!dib || !bits) {
if (dib) ::DeleteObject(dib);
::DeleteDC(mem);
::ReleaseDC(nullptr, hdc);
return false;
}
HGDIOBJ old = ::SelectObject(mem, dib);
// Zero-fill; DrawIconEx writes real per-pixel alpha. Do NOT force A=255.
::ZeroMemory(bits, static_cast<size_t>(w) * static_cast<size_t>(h) * 4u);
::SetBkMode(mem, TRANSPARENT);
::DrawIconEx(mem, 0, 0, hIcon, w, h, 0, nullptr, DI_NORMAL);
// Mask-style icons may leave A=0 on every pixel. Promote colored pixels to
// opaque only; keep transparent holes (A=0). Never force A=255 on all pixels.
{
DWORD* px = static_cast<DWORD*>(bits);
const int n = w * h;
bool anyAlpha = false;
for (int i = 0; i < n; ++i) {
const BYTE a = static_cast<BYTE>((px[i] >> 24) & 0xFFu);
if (a != 0) { anyAlpha = true; break; }
}
if (!anyAlpha) {
for (int i = 0; i < n; ++i) {
if ((px[i] & 0x00FFFFFFu) != 0)
px[i] |= 0xFF000000u;
}
}
}
const size_t bytes = static_cast<size_t>(w) * static_cast<size_t>(h) * 4u;
out.assign(static_cast<const BYTE*>(bits), static_cast<const BYTE*>(bits) + bytes);
::SelectObject(mem, old);
::DeleteObject(dib);
::DeleteDC(mem);
::ReleaseDC(nullptr, hdc);
return true;
}
// GDI+ HighQualityBicubic resample of a packed top-down 32bpp ARGB buffer.
bool CMainWnd::ResizeArgbBuffer(const std::vector<BYTE>& src, int sw, int sh,
int dw, int dh, std::vector<BYTE>& dst)
{
using namespace Gdiplus;
dst.clear();
if (sw <= 0 || sh <= 0 || dw <= 0 || dh <= 0) return false;
if (src.size() < static_cast<size_t>(sw) * static_cast<size_t>(sh) * 4u) return false;
if (!EnsureGdiplus()) return false;
Bitmap s(sw, sh, sw * 4, PixelFormat32bppARGB,
const_cast<BYTE*>(src.data()));
if (s.GetLastStatus() != Ok) return false;
Bitmap d(dw, dh, PixelFormat32bppARGB);
if (d.GetLastStatus() != Ok) return false;
{
Graphics g(&d);
if (g.GetLastStatus() != Ok) return false;
g.SetInterpolationMode(InterpolationModeHighQualityBicubic);
g.SetPixelOffsetMode(PixelOffsetModeHighQuality);
g.SetCompositingMode(CompositingModeSourceCopy);
g.SetCompositingQuality(CompositingQualityHighQuality);
g.Clear(Color(0, 0, 0, 0));
g.DrawImage(&s, Rect(0, 0, dw, dh), 0, 0, sw, sh, UnitPixel);
}
BitmapData bd = {};
Rect lockRc(0, 0, dw, dh);
if (d.LockBits(&lockRc, ImageLockModeRead, PixelFormat32bppARGB, &bd) != Ok)
return false;
const BYTE* p = static_cast<const BYTE*>(bd.Scan0);
dst.assign(static_cast<size_t>(dw) * static_cast<size_t>(dh) * 4u, 0);
for (int y = 0; y < dh; ++y) {
::memcpy(dst.data() + static_cast<size_t>(y) * static_cast<size_t>(dw) * 4u,
p + static_cast<ptrdiff_t>(y) * bd.Stride,
static_cast<size_t>(dw) * 4u);
}
d.UnlockBits(&bd);
return true;
}
bool CMainWnd::SaveIconToPng(HICON hIcon, const std::wstring& pngPath, int cx, int cy)
{
if (!hIcon || cx <= 0 || cy <= 0 || pngPath.empty()) return false;
using namespace Gdiplus;
if (!EnsureGdiplus()) return false;
// Native size of the icon bitmap. Rendering 1:1 and letting GDI+ resample keeps
// edges smooth when the Shell has no image list at exactly the requested size
// (e.g. 192px tiles come from the 384px JUMBO list).
int natW = cx, natH = cy;
{
ICONINFO ii = {};
if (::GetIconInfo(hIcon, &ii)) {
BITMAP bm = {};
if (ii.hbmColor && ::GetObject(ii.hbmColor, sizeof(bm), &bm) == sizeof(bm)
&& bm.bmWidth > 0 && bm.bmHeight > 0) {
natW = bm.bmWidth;
natH = bm.bmHeight;
}
if (ii.hbmColor) ::DeleteObject(ii.hbmColor);
if (ii.hbmMask) ::DeleteObject(ii.hbmMask);
}
}
if (natW <= 0 || natH <= 0 || natW > 8192 || natH > 8192) {
natW = cx;
natH = cy;
}
std::vector<BYTE> px;
if (!RenderIconToArgbBuffer(hIcon, natW, natH, px)) return false;
if (natW != cx || natH != cy) {
std::vector<BYTE> scaled;
if (ResizeArgbBuffer(px, natW, natH, cx, cy, scaled) && !scaled.empty()) {
px.swap(scaled);
} else if (!RenderIconToArgbBuffer(hIcon, cx, cy, px)) {
return false; // fall back to plain DrawIconEx scaling
}
}
// Bind scan0 so PNG encoder keeps true alpha (DuiLib needs A<255 somewhere for AlphaBlend).
Bitmap bmp(cx, cy, cx * 4, PixelFormat32bppARGB, px.data());
if (bmp.GetLastStatus() != Ok) return false;
CLSID clsidPng = {};
if (!GetPngEncoderClsid(&clsidPng)) return false;
return bmp.Save(pngPath.c_str(), &clsidPng, nullptr) == Ok;
}
// Trim fully transparent borders from a thumb PNG. Shell/GDI+ thumbs are produced inside
// a square box with transparent bands, and cropping those lets the icon view draw each
// picture at its own aspect ratio.
bool CMainWnd::CropPngToContentAlpha(const std::wstring& pngPath)
{
if (pngPath.empty()) return false;
using namespace Gdiplus;
if (!EnsureGdiplus()) return false;
int cw = 0, ch = 0, cx = 0, cy = 0;
{
Bitmap bmp(pngPath.c_str());
if (bmp.GetLastStatus() != Ok) return false;
const int w = bmp.GetWidth();
const int h = bmp.GetHeight();
if (w <= 0 || h <= 0) return false;
BitmapData bd = {};
Rect rc(0, 0, w, h);
if (bmp.LockBits(&rc, ImageLockModeRead, PixelFormat32bppARGB, &bd) != Ok)
return false;
int minX = w, minY = h, maxX = -1, maxY = -1;
for (int y = 0; y < h; ++y) {
const BYTE* row = static_cast<const BYTE*>(bd.Scan0)
+ static_cast<ptrdiff_t>(y) * bd.Stride;
for (int x = 0; x < w; ++x) {
if (row[x * 4 + 3] >= 8) {
if (x < minX) minX = x;
if (x > maxX) maxX = x;
if (y < minY) minY = y;
if (y > maxY) maxY = y;
}
}
}
bmp.UnlockBits(&bd);
if (maxX < 0) return false; // fully transparent
if (minX == 0 && minY == 0 && maxX == w - 1 && maxY == h - 1)
return true; // nothing to trim
cx = minX; cy = minY;
cw = maxX - minX + 1;
ch = maxY - minY + 1;
}
const std::wstring tmp = pngPath + L".crop.png";
bool written = false;
{
Bitmap src(pngPath.c_str());
if (src.GetLastStatus() != Ok) return false;
Bitmap dst(cw, ch, PixelFormat32bppARGB);
if (dst.GetLastStatus() != Ok) return false;
{
Graphics g(&dst);
if (g.GetLastStatus() != Ok) return false;
g.SetInterpolationMode(InterpolationModeHighQualityBicubic);
g.SetPixelOffsetMode(PixelOffsetModeHighQuality);
g.Clear(Color(0, 0, 0, 0));
g.DrawImage(&src, Rect(0, 0, cw, ch), cx, cy, cw, ch, UnitPixel);
}
CLSID clsidPng = {};
if (!GetPngEncoderClsid(&clsidPng)) return false;
::DeleteFileW(tmp.c_str());
written = (dst.Save(tmp.c_str(), &clsidPng, nullptr) == Ok);
}
if (!written) { ::DeleteFileW(tmp.c_str()); return false; }
::DeleteFileW(pngPath.c_str());
if (!::MoveFileW(tmp.c_str(), pngPath.c_str())) {
::DeleteFileW(tmp.c_str());
return false;
}
return true;
}
bool CMainWnd::SaveImageThumbnailPng(const std::wstring& srcPath, const std::wstring& pngPath, int cx, int cy)
{
using namespace Gdiplus;
if (!EnsureGdiplus()) return false;
Bitmap src(srcPath.c_str());
if (src.GetLastStatus() != Ok)
return false;
Bitmap dst(cx, cy, PixelFormat32bppARGB);
Graphics g(&dst);
g.SetInterpolationMode(InterpolationModeHighQualityBicubic);
// Transparent letterbox (PNG true alpha) — DuiLib AlphaBlend; no white halo / black pocket.
g.Clear(Color(0, 0, 0, 0));
const int sw = src.GetWidth();
const int sh = src.GetHeight();
if (sw <= 0 || sh <= 0) return false;
double scale = (std::min)(static_cast<double>(cx) / sw, static_cast<double>(cy) / sh);
int dw = static_cast<int>(sw * scale);
int dh = static_cast<int>(sh * scale);
int ox = (cx - dw) / 2;
int oy = (cy - dh) / 2;
g.DrawImage(&src, ox, oy, dw, dh);
CLSID clsidPng = {};
if (!GetPngEncoderClsid(&clsidPng)) return false;
return dst.Save(pngPath.c_str(), &clsidPng, nullptr) == Ok;
}
std::wstring CMainWnd::PeekCachedIconBmp(const std::wstring& path, bool isDir, int cx)
{
if (cx < 16) cx = 16;
if (cx > 256) cx = 256;
wchar_t cacheKey[32] = {};
swprintf_s(cacheKey, L"@%d", cx);
std::wstring key = path + cacheKey;
{
std::lock_guard<std::mutex> lock(m_iconCacheMutex);
auto it = m_iconCache.find(key);
if (it != m_iconCache.end() && ::PathFileExistsW(it->second.c_str()))
return it->second;
}
wchar_t tail[32] = {};
swprintf_s(tail, L"%s_%d.png", isDir ? L"d" : L"f", cx);
std::wstring bmpPath = m_iconCacheDir + IconCacheLeaf(key, path, tail);
if (::PathFileExistsW(bmpPath.c_str())) {
std::lock_guard<std::mutex> lock(m_iconCacheMutex);
m_iconCache[key] = bmpPath;
return bmpPath;
}
return {};
}
std::wstring CMainWnd::GetShellIconBmp(const std::wstring& path, bool isDir, int cx)
{
if (cx < 16) cx = 16;
if (cx > 256) cx = 256;
wchar_t cacheKey[32] = {};
swprintf_s(cacheKey, L"@%d", cx);
std::wstring key = path + cacheKey;
{
std::lock_guard<std::mutex> lock(m_iconCacheMutex);
auto it = m_iconCache.find(key);
if (it != m_iconCache.end() && ::PathFileExistsW(it->second.c_str()))
return it->second;
}
wchar_t tail[32] = {};
swprintf_s(tail, L"%s_%d.png", isDir ? L"d" : L"f", cx);
std::wstring bmpPath = m_iconCacheDir + IconCacheLeaf(key, path, tail);
if (::PathFileExistsW(bmpPath.c_str())) {
std::lock_guard<std::mutex> lock(m_iconCacheMutex);
m_iconCache[key] = bmpPath;
return bmpPath;
}
// Content thumbs (transparent letterbox PNG) only for real image/video files.
// Chrome/tree/details/folder/drive: HICON only — never SIIGBF_ICONONLY (black pocket).
const bool wantContentThumb = (cx > 32) && !isDir
&& (IsImageExtension(path) || IsVideoExtension(path));
if (wantContentThumb && ::PathFileExistsW(path.c_str())
&& ExtractShellItemImage(path, cx, cx, bmpPath)) {
std::lock_guard<std::mutex> lock(m_iconCacheMutex);
m_iconCache[key] = bmpPath;
return bmpPath;
}
if (ExtractShellIconSized(path, isDir, cx, bmpPath)) {
std::lock_guard<std::mutex> lock(m_iconCacheMutex);
m_iconCache[key] = bmpPath;
return bmpPath;
}
if (wantContentThumb && IsImageExtension(path)) {
if (SaveImageThumbnailPng(path, bmpPath, cx, cx)) {
std::lock_guard<std::mutex> lock(m_iconCacheMutex);
m_iconCache[key] = bmpPath;
return bmpPath;
}
}
return {};
}
std::wstring CMainWnd::GetShellFileIconBmp(const std::wstring& path, bool isDir, int cx)
{
if (cx < 16) cx = 16;
if (cx > 256) cx = 256;
wchar_t cacheKey[32] = {};
swprintf_s(cacheKey, L"@%d", cx);
std::wstring key = path + L"#ico" + cacheKey;
{
std::lock_guard<std::mutex> lock(m_iconCacheMutex);
auto it = m_iconCache.find(key);
if (it != m_iconCache.end() && ::PathFileExistsW(it->second.c_str()))
return it->second;
}
wchar_t tail[32] = {};
swprintf_s(tail, L"%s_%d_ico.png", isDir ? L"d" : L"f", cx);
std::wstring bmpPath = m_iconCacheDir + IconCacheLeaf(key, path, tail);
if (::PathFileExistsW(bmpPath.c_str())) {
std::lock_guard<std::mutex> lock(m_iconCacheMutex);
m_iconCache[key] = bmpPath;
return bmpPath;
}
if (ExtractShellIconSized(path, isDir, cx, bmpPath)) {
std::lock_guard<std::mutex> lock(m_iconCacheMutex);
m_iconCache[key] = bmpPath;
return bmpPath;
}
return {};
}
bool CMainWnd::SaveHBitmapToPng(HBITMAP hbm, const std::wstring& pngPath)
{
if (!hbm || pngPath.empty()) return false;
using namespace Gdiplus;
if (!EnsureGdiplus()) return false;
Bitmap bmp(hbm, nullptr);
if (bmp.GetLastStatus() != Ok) return false;
CLSID clsidPng = {};
if (!GetPngEncoderClsid(&clsidPng)) return false;
return bmp.Save(pngPath.c_str(), &clsidPng, nullptr) == Ok;
}
bool CMainWnd::LetterboxHBitmapToPng(HBITMAP hbm, int cx, int cy, const std::wstring& pngPath)
{
if (!hbm || cx <= 0 || cy <= 0 || pngPath.empty()) return false;
using namespace Gdiplus;
if (!EnsureGdiplus()) return false;
Bitmap src(hbm, nullptr);
if (src.GetLastStatus() != Ok) return false;
const int sw = src.GetWidth();
const int sh = src.GetHeight();
if (sw <= 0 || sh <= 0) return false;
Bitmap dst(cx, cy, PixelFormat32bppARGB);
Graphics g(&dst);
g.SetInterpolationMode(InterpolationModeHighQualityBicubic);
// Transparent letterbox (or ColorContent white) — PNG true alpha for AlphaBlend.
g.Clear(Color(0, 0, 0, 0));
const double scale = (std::min)(static_cast<double>(cx) / sw, static_cast<double>(cy) / sh);
const int dw = (std::max)(1, static_cast<int>(sw * scale));
const int dh = (std::max)(1, static_cast<int>(sh * scale));
const int ox = (cx - dw) / 2;
const int oy = (cy - dh) / 2;
g.DrawImage(&src, ox, oy, dw, dh);
CLSID clsidPng = {};
if (!GetPngEncoderClsid(&clsidPng)) return false;
return dst.Save(pngPath.c_str(), &clsidPng, nullptr) == Ok;
}
// Localized shell display name for a path (SIGDN_NORMALDISPLAY). Known folders come back in
// the UI language - "D:\Users\...\Pictures" shows as "图片" - which is what Explorer's
// navigation pane does. Empty result means "fall back to the leaf name".
std::wstring CMainWnd::GetShellDisplayName(const std::wstring& path) const
{
if (path.empty()) return std::wstring();
IShellItem* psi = nullptr;
if (FAILED(::SHCreateItemFromParsingName(path.c_str(), nullptr, IID_PPV_ARGS(&psi))) || !psi)
return std::wstring();
std::wstring name;
PWSTR psz = nullptr;
if (SUCCEEDED(psi->GetDisplayName(SIGDN_NORMALDISPLAY, &psz)) && psz) {
name = psz;
::CoTaskMemFree(psz);
}
psi->Release();
return name;
}
bool CMainWnd::ExtractShellItemImage(const std::wstring& path, int cx, int cy, const std::wstring& pngPath)
{
// Content thumbs only (image/video). Never SIIGBF_ICONONLY — folders/drives use HICON.
if (path.empty() || cx <= 0 || cy <= 0 || pngPath.empty()) return false;
IShellItem* psi = nullptr;
HRESULT hr = ::SHCreateItemFromParsingName(path.c_str(), nullptr, IID_PPV_ARGS(&psi));
if (FAILED(hr) || !psi) return false;
IShellItemImageFactory* pFactory = nullptr;
hr = psi->QueryInterface(IID_PPV_ARGS(&pFactory));
psi->Release();
if (FAILED(hr) || !pFactory) return false;
SIZE sz = { cx, cy };
HBITMAP hbm = nullptr;
hr = pFactory->GetImage(sz, SIIGBF_RESIZETOFIT | SIIGBF_BIGGERSIZEOK, &hbm);
pFactory->Release();
if (FAILED(hr) || !hbm) return false;
const bool ok = LetterboxHBitmapToPng(hbm, cx, cy, pngPath);
::DeleteObject(hbm);
if (!ok) return false;
// Tighten: the icon view fits the thumb by its own aspect ratio.
CropPngToContentAlpha(pngPath);
return true;
}
bool CMainWnd::ExtractShellIconSized(const std::wstring& path, bool isDir, int cx, const std::wstring& bmpPath)
{
if (cx < 8) cx = 8;
if (cx > 512) cx = 512;
SHFILEINFOW sfi = {};
// Prefer real path lookup so Known Folders (Desktop/Documents/Downloads)
// keep their special Shell icons. USEFILEATTRIBUTES only as fallback.
UINT flags = SHGFI_SYSICONINDEX;
DWORD attrs = 0;
DWORD_PTR ok = 0;
if (::PathFileExistsW(path.c_str())) {
ok = ::SHGetFileInfoW(path.c_str(), 0, &sfi, sizeof(sfi), flags);
}
if (!ok) {
flags = SHGFI_SYSICONINDEX | SHGFI_USEFILEATTRIBUTES;
attrs = isDir ? FILE_ATTRIBUTE_DIRECTORY : FILE_ATTRIBUTE_NORMAL;
ok = ::SHGetFileInfoW(path.c_str(), attrs, &sfi, sizeof(sfi), flags);
}
// Pick the system image list by its *actual* icon size. The old fixed thresholds
// (16/32/48/256) grabbed SHIL_LARGE - 48px at 150% DPI - for a 24px slot, and SHIL_JUMBO
// (384px) for a 72px slot, so every icon was squashed by DrawIconEx without filtering.
// The real sizes scale with DPI (24 / 48 / 72 / 384 here), so an exact match usually
// exists and no resampling is needed at all.
static const int kLists[] = { SHIL_SMALL, SHIL_LARGE, SHIL_EXTRALARGE, SHIL_JUMBO };
IImageList* best = nullptr;
int bestSize = 0;
for (int shil : kLists) {
IImageList* piml = nullptr;
if (FAILED(::SHGetImageList(shil, IID_IImageList, reinterpret_cast<void**>(&piml))) || !piml)
continue;
int w = 0, h = 0;
if (FAILED(piml->GetIconSize(&w, &h)) || w <= 0) {
piml->Release();
continue;
}
const bool better = (best == nullptr)
|| (bestSize < cx && w > bestSize) // grow towards the target size
|| (w >= cx && (bestSize < cx || w < bestSize)); // then the smallest that covers it
if (better) {
if (best) best->Release();
best = piml;
bestSize = w;
} else {
piml->Release();
}
}
if (best) {
HICON hIcon = nullptr;
HRESULT hr = best->GetIcon(sfi.iIcon, ILD_TRANSPARENT, &hIcon);
best->Release();
if (SUCCEEDED(hr) && hIcon) {
bool saved = SaveIconToPng(hIcon, bmpPath, cx, cx);
::DestroyIcon(hIcon);
if (saved) return true;
}
}
sfi = {};
flags = SHGFI_ICON | ((cx <= 16) ? SHGFI_SMALLICON : SHGFI_LARGEICON);
if (isDir) flags |= SHGFI_USEFILEATTRIBUTES;
ok = ::SHGetFileInfoW(path.c_str(), isDir ? FILE_ATTRIBUTE_DIRECTORY : 0,
&sfi, sizeof(sfi), flags);
if (!ok || !sfi.hIcon)
return false;
bool saved = SaveIconToPng(sfi.hIcon, bmpPath, cx, cx);
::DestroyIcon(sfi.hIcon);
return saved;
}
void CMainWnd::ApplyControlForeIcon(CControlUI* ctrl, const std::wstring& bmp,
int iconPx, int destX, int destY, bool clearText)
{
if (!ctrl || bmp.empty() || iconPx <= 0) return;
CDuiString imgAttr;
imgAttr.Format(_T("file='%s' dest='%d,%d,%d,%d'"),
bmp.c_str(), destX, destY, destX + iconPx, destY + iconPx);
ctrl->SetAttribute(_T("foreimage"), imgAttr.GetData());
ctrl->SetAttribute(_T("hotforeimage"), imgAttr.GetData());
if (clearText)
ctrl->SetText(_T(""));
}
bool CMainWnd::ExtractStockIconSized(int siid, int cx, const std::wstring& bmpPath)
{
if (cx < 16) cx = 16;
SHSTOCKICONINFO sii = {};
sii.cbSize = sizeof(sii);
// Prefer ICONLOCATION + sized extract for DPI-correct glyphs
if (SUCCEEDED(::SHGetStockIconInfo(static_cast<SHSTOCKICONID>(siid), SHGSI_ICONLOCATION, &sii))
&& sii.szPath[0] != L'\0') {
HICON hIcon = nullptr;
if (SUCCEEDED(::SHDefExtractIconW(sii.szPath, sii.iIcon, 0, &hIcon, nullptr, static_cast<UINT>(cx)))
&& hIcon) {
const bool saved = SaveIconToPng(hIcon, bmpPath, cx, cx);
::DestroyIcon(hIcon);
if (saved) return true;
}
}
sii = {};
sii.cbSize = sizeof(sii);
const UINT fl = SHGSI_ICON | ((cx <= 16) ? SHGSI_SMALLICON : SHGSI_LARGEICON);
if (FAILED(::SHGetStockIconInfo(static_cast<SHSTOCKICONID>(siid), fl, &sii)) || !sii.hIcon)
return false;
const bool saved = SaveIconToPng(sii.hIcon, bmpPath, cx, cx);
::DestroyIcon(sii.hIcon);
return saved;
}
bool CMainWnd::ExtractModuleIconSized(const wchar_t* moduleFile, int index, int cx, const std::wstring& bmpPath)
{
if (!moduleFile || index < 0 || cx <= 0) return false;
wchar_t sys[MAX_PATH] = {};
UINT n = ::GetSystemDirectoryW(sys, MAX_PATH);
if (n == 0 || n >= MAX_PATH) return false;
std::wstring full = sys;
if (!full.empty() && full.back() != L'\\') full.push_back(L'\\');
full += moduleFile;
HICON hIcon = nullptr;
UINT got = ::PrivateExtractIconsW(full.c_str(), index, cx, cx, &hIcon, nullptr, 1, LR_DEFAULTCOLOR);
if (got == 0 || !hIcon) {
// Fallback via ExtractIconEx (avoid identifier "small" — Windows headers macro it).
HICON hLarge = nullptr;
HICON hSmall = nullptr;
if (::ExtractIconExW(full.c_str(), index, &hLarge, &hSmall, 1) > 0) {
if (cx <= 16) {
hIcon = hSmall ? hSmall : hLarge;
} else {
hIcon = hLarge ? hLarge : hSmall;
}
if (hSmall && hSmall != hIcon) ::DestroyIcon(hSmall);
if (hLarge && hLarge != hIcon) ::DestroyIcon(hLarge);
}
}
if (!hIcon) return false;
const bool saved = SaveIconToPng(hIcon, bmpPath, cx, cx);
::DestroyIcon(hIcon);
return saved;
}
namespace {
// Windows icon fonts: Segoe Fluent Icons (Windows 11, C:\Windows\Fonts\SegoeIcons.ttf) is the
// Explorer command-bar set; Segoe MDL2 Assets (segmdl2.ttf) carries the same code points on
// Windows 10. Both put the em box at the cell origin (ascent = em, descent = 0).
int CALLBACK IconFontFound(const LOGFONTW*, const TEXTMETRICW*, DWORD, LPARAM found)
{
*reinterpret_cast<bool*>(found) = true;
return 0;
}
const wchar_t* CommandIconFace()
{
static int which = -1;
if (which < 0) {
which = 0;
HDC dc = ::GetDC(nullptr);
auto has = [&](const wchar_t* face) {
LOGFONTW lf = {};
lf.lfCharSet = DEFAULT_CHARSET;
wcscpy_s(lf.lfFaceName, face);
bool found = false;
::EnumFontFamiliesExW(dc, &lf, IconFontFound, reinterpret_cast<LPARAM>(&found), 0);
return found;
};
if (dc) {
if (has(L"Segoe Fluent Icons")) which = 1;
else if (has(L"Segoe MDL2 Assets")) which = 2;
::ReleaseDC(nullptr, dc);
}
}
return which == 1 ? L"Segoe Fluent Icons" : which == 2 ? L"Segoe MDL2 Assets" : nullptr;
}
// Rasterizes one icon-font glyph white-on-black with GDI and max-merges its coverage into
// `cov` (w x h, one byte per pixel). (x, y) is the top-left of the em box, emPx its size.
bool AddGlyphCoverage(const wchar_t* face, wchar_t glyph, int emPx, int x, int y,
int w, int h, std::vector<BYTE>& cov)
{
if (!face || emPx <= 0 || w <= 0 || h <= 0) return false;
if (cov.size() != size_t(w) * size_t(h)) cov.assign(size_t(w) * size_t(h), 0);
HDC screen = ::GetDC(nullptr);
if (!screen) return false;
HDC mem = ::CreateCompatibleDC(screen);
BITMAPINFO bi = {};
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bi.bmiHeader.biWidth = w;
bi.bmiHeader.biHeight = -h;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 32;
bi.bmiHeader.biCompression = BI_RGB;
void* bits = nullptr;
HBITMAP dib = ::CreateDIBSection(screen, &bi, DIB_RGB_COLORS, &bits, nullptr, 0);
bool ok = false;
if (dib && bits && mem) {
::ZeroMemory(bits, size_t(w) * size_t(h) * 4u);
HGDIOBJ oldBmp = ::SelectObject(mem, dib);
LOGFONTW lf = {};
lf.lfHeight = -emPx;
lf.lfWeight = FW_NORMAL;
lf.lfCharSet = DEFAULT_CHARSET;
lf.lfQuality = ANTIALIASED_QUALITY;
wcscpy_s(lf.lfFaceName, face);
HFONT font = ::CreateFontIndirectW(&lf);
HGDIOBJ oldFont = font ? ::SelectObject(mem, font) : nullptr;
::SetBkMode(mem, TRANSPARENT);
::SetTextColor(mem, RGB(255, 255, 255));
::SetTextAlign(mem, TA_TOP | TA_LEFT | TA_NOUPDATECP);
ok = ::TextOutW(mem, x, y, &glyph, 1) != FALSE;
::GdiFlush();
const DWORD* src = static_cast<const DWORD*>(bits);
for (size_t i = 0, n = size_t(w) * size_t(h); i < n; ++i) {
const DWORD p = src[i];
const BYTE c = (std::max)({ BYTE(p & 0xFF), BYTE((p >> 8) & 0xFF), BYTE((p >> 16) & 0xFF) });
if (c > cov[i]) cov[i] = c;
}
if (oldFont) ::SelectObject(mem, oldFont);
if (font) ::DeleteObject(font);
::SelectObject(mem, oldBmp);
}
if (dib) ::DeleteObject(dib);
if (mem) ::DeleteDC(mem);
::ReleaseDC(nullptr, screen);
return ok;
}
// Box-filters an s-times supersampled coverage buffer down to (w, h).
std::vector<float> DownsampleCoverage(const std::vector<BYTE>& hi, int w, int h, int s)
{
std::vector<float> out(size_t(w) * size_t(h), 0.0f);
if (hi.size() != size_t(w) * size_t(h) * size_t(s) * size_t(s)) return out;
const int hw = w * s;
const float norm = 1.0f / (255.0f * float(s * s));
for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x) {
unsigned sum = 0;
for (int j = 0; j < s; ++j) {
const BYTE* row = &hi[size_t(y * s + j) * size_t(hw) + size_t(x * s)];
for (int i = 0; i < s; ++i) sum += row[i];
}
out[size_t(y) * size_t(w) + size_t(x)] = float(sum) * norm;
}
return out;
}
} // namespace
bool CMainWnd::RenderGlyphToPng(wchar_t glyph, int px, COLORREF color, const std::wstring& pngPath)
{
if (px <= 0 || pngPath.empty()) return false;
if (!EnsureGdiplus()) return false;
const wchar_t* face = CommandIconFace();
if (!face) return false;
// Rasterize the em box 4x supersampled (white on black), box-filter to the exact size and
// rebuild straight-alpha pixels in the requested colour.
constexpr int s = 4;
std::vector<BYTE> hi;
if (!AddGlyphCoverage(face, glyph, px * s, 0, 0, px * s, px * s, hi)) return false;
const auto cov = DownsampleCoverage(hi, px, px, s);
const Gdiplus::ARGB rgb = (Gdiplus::ARGB(GetRValue(color)) << 16)
| (Gdiplus::ARGB(GetGValue(color)) << 8) | Gdiplus::ARGB(GetBValue(color));
std::vector<Gdiplus::ARGB> pixels(cov.size(), 0);
for (size_t i = 0; i < cov.size(); ++i)
if (cov[i] > 0.0f) pixels[i] = (Gdiplus::ARGB(std::lround(cov[i] * 255.0f)) << 24) | rgb;
return SaveArgbPng(pixels, px, px, pngPath);
}
std::wstring CMainWnd::GetGlyphIconBmp(wchar_t glyph, int px, COLORREF color)
{
if (px < 8) px = 8;
if (px > 128) px = 128;
wchar_t keybuf[96] = {};
swprintf_s(keybuf, L"glyph:%04X@%d#%06X", static_cast<unsigned>(glyph), px,
static_cast<unsigned>(color & 0x00FFFFFFu));
const std::wstring key = keybuf;
{
std::lock_guard<std::mutex> lock(m_iconCacheMutex);
auto it = m_iconCache.find(key);
if (it != m_iconCache.end() && ::PathFileExistsW(it->second.c_str()))
return it->second;
}
size_t h = std::hash<std::wstring>{}(key);
wchar_t name[96] = {};
swprintf_s(name, L"gly_%08X_%04X_%d_v2.png", static_cast<unsigned>(h & 0xFFFFFFFFu),
static_cast<unsigned>(glyph), px);
std::wstring bmpPath = m_iconCacheDir + name;
if (::PathFileExistsW(bmpPath.c_str())) {
std::lock_guard<std::mutex> lock(m_iconCacheMutex);
m_iconCache[key] = bmpPath;
return bmpPath;
}
if (RenderGlyphToPng(glyph, px, color, bmpPath)) {
std::lock_guard<std::mutex> lock(m_iconCacheMutex);
m_iconCache[key] = bmpPath;
return bmpPath;