-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathguisup.c
4851 lines (4119 loc) · 122 KB
/
guisup.c
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
/*
* Copyright (c) 2022 Winsider Seminars & Solutions, Inc. All rights reserved.
*
* This file is part of System Informer.
*
* Authors:
*
* wj32 2009-2016
* dmex 2017-2024
*
*/
#include <ph.h>
#include <apiimport.h>
#include <guisup.h>
#include <guisupview.h>
#include <mapimg.h>
#include <mapldr.h>
#include <settings.h>
#include <guisupp.h>
#include <math.h>
#include <commoncontrols.h>
#include <shellscalingapi.h>
#include <wincodec.h>
BOOLEAN NTAPI PhpWindowContextHashtableEqualFunction(
_In_ PVOID Entry1,
_In_ PVOID Entry2
);
ULONG NTAPI PhpWindowContextHashtableHashFunction(
_In_ PVOID Entry
);
BOOLEAN NTAPI PhpWindowCallbackHashtableEqualFunction(
_In_ PVOID Entry1,
_In_ PVOID Entry2
);
ULONG NTAPI PhpWindowCallbackHashtableHashFunction(
_In_ PVOID Entry
);
typedef struct _PH_WINDOW_PROPERTY_CONTEXT
{
ULONG PropertyHash;
HWND WindowHandle;
PVOID Context;
} PH_WINDOW_PROPERTY_CONTEXT, *PPH_WINDOW_PROPERTY_CONTEXT;
HFONT PhApplicationFont = NULL;
HFONT PhTreeWindowFont = NULL;
HFONT PhMonospaceFont = NULL;
LONG PhSystemDpi = 96;
PH_INTEGER_PAIR PhSmallIconSize = { 16, 16 };
PH_INTEGER_PAIR PhLargeIconSize = { 32, 32 };
static PH_INITONCE SharedIconCacheInitOnce = PH_INITONCE_INIT;
static PPH_HASHTABLE SharedIconCacheHashtable;
static PH_QUEUED_LOCK SharedIconCacheLock = PH_QUEUED_LOCK_INIT;
static PPH_HASHTABLE WindowCallbackHashTable = NULL;
static PH_QUEUED_LOCK WindowCallbackListLock = PH_QUEUED_LOCK_INIT;
static PPH_HASHTABLE WindowContextHashTable = NULL;
static PH_QUEUED_LOCK WindowContextListLock = PH_QUEUED_LOCK_INIT;
static _OpenThemeDataForDpi OpenThemeDataForDpi_I = NULL;
static _OpenThemeData OpenThemeData_I = NULL;
static _CloseThemeData CloseThemeData_I = NULL;
static _SetWindowTheme SetWindowTheme_I = NULL;
static _IsThemeActive IsThemeActive_I = NULL;
static _IsThemePartDefined IsThemePartDefined_I = NULL;
static _GetThemeClass GetThemeClass_I = NULL;
static _GetThemeColor GetThemeColor_I = NULL;
static _GetThemeInt GetThemeInt_I = NULL;
static _GetThemePartSize GetThemePartSize_I = NULL;
static _DrawThemeBackground DrawThemeBackground_I = NULL;
static _DrawThemeTextEx DrawThemeTextEx_I = NULL;
static _AllowDarkModeForWindow AllowDarkModeForWindow_I = NULL; // Win10-RS5 (uxtheme.dll ordinal 133)
static _IsDarkModeAllowedForWindow IsDarkModeAllowedForWindow_I = NULL; // Win10-RS5 (uxtheme.dll ordinal 137)
static _ShouldAppsUseDarkMode ShouldAppsUseDarkMode_I = NULL; // Win10-RS5 (uxtheme.dll ordinal 132)
// Win10-RS5 (uxtheme.dll ordinal 135)
// Win10 build 17763: AllowDarkModeForApp(BOOL)
// Win10 build 18334: SetPreferredAppMode(enum PreferredAppMode)
static _SetPreferredAppMode SetPreferredAppMode_I = NULL;
static _GetDpiForMonitor GetDpiForMonitor_I = NULL; // win81+
static _GetDpiForWindow GetDpiForWindow_I = NULL; // win10rs1+
static _GetDpiForSystem GetDpiForSystem_I = NULL; // win10rs1+
//static _GetDpiForSession GetDpiForSession_I = NULL; // ordinal 2713
static _GetSystemMetricsForDpi GetSystemMetricsForDpi_I = NULL;
static _SystemParametersInfoForDpi SystemParametersInfoForDpi_I = NULL;
static _CreateMRUList CreateMRUList_I = NULL;
static _AddMRUString AddMRUString_I = NULL;
static _EnumMRUList EnumMRUList_I = NULL;
static _FreeMRUList FreeMRUList_I = NULL;
VOID PhGuiSupportInitialization(
VOID
)
{
PVOID baseAddress;
WindowCallbackHashTable = PhCreateHashtable(
sizeof(PH_PLUGIN_WINDOW_CALLBACK_REGISTRATION),
PhpWindowCallbackHashtableEqualFunction,
PhpWindowCallbackHashtableHashFunction,
10
);
WindowContextHashTable = PhCreateHashtable(
sizeof(PH_WINDOW_PROPERTY_CONTEXT),
PhpWindowContextHashtableEqualFunction,
PhpWindowContextHashtableHashFunction,
10
);
if (baseAddress = PhLoadLibrary(L"uxtheme.dll"))
{
OpenThemeDataForDpi_I = PhGetDllBaseProcedureAddress(baseAddress, "OpenThemeDataForDpi", 0);
OpenThemeData_I = PhGetDllBaseProcedureAddress(baseAddress, "OpenThemeData", 0);
CloseThemeData_I = PhGetDllBaseProcedureAddress(baseAddress, "CloseThemeData", 0);
SetWindowTheme_I = PhGetDllBaseProcedureAddress(baseAddress, "SetWindowTheme", 0);
IsThemeActive_I = PhGetDllBaseProcedureAddress(baseAddress, "IsThemeActive", 0);
IsThemePartDefined_I = PhGetDllBaseProcedureAddress(baseAddress, "IsThemePartDefined", 0);
GetThemeColor_I = PhGetDllBaseProcedureAddress(baseAddress, "GetThemeColor", 0);
GetThemeInt_I = PhGetDllBaseProcedureAddress(baseAddress, "GetThemeInt", 0);
GetThemePartSize_I = PhGetDllBaseProcedureAddress(baseAddress, "GetThemePartSize", 0);
DrawThemeBackground_I = PhGetDllBaseProcedureAddress(baseAddress, "DrawThemeBackground", 0);
if (WindowsVersion >= WINDOWS_11)
{
GetThemeClass_I = PhGetDllBaseProcedureAddress(baseAddress, NULL, 74);
}
if (WindowsVersion >= WINDOWS_10_RS5)
{
AllowDarkModeForWindow_I = PhGetDllBaseProcedureAddress(baseAddress, NULL, 133);
IsDarkModeAllowedForWindow_I = PhGetDllBaseProcedureAddress(baseAddress, NULL, 137);
ShouldAppsUseDarkMode_I = PhGetDllBaseProcedureAddress(baseAddress, NULL, 132);
SetPreferredAppMode_I = PhGetDllBaseProcedureAddress(baseAddress, NULL, 135);
}
}
if (WindowsVersion >= WINDOWS_8_1)
{
if (baseAddress = PhLoadLibrary(L"shcore.dll"))
{
GetDpiForMonitor_I = PhGetDllBaseProcedureAddress(baseAddress, "GetDpiForMonitor", 0);
}
}
if (WindowsVersion >= WINDOWS_10_RS1)
{
if (baseAddress = PhLoadLibrary(L"user32.dll"))
{
GetDpiForWindow_I = PhGetDllBaseProcedureAddress(baseAddress, "GetDpiForWindow", 0);
GetDpiForSystem_I = PhGetDllBaseProcedureAddress(baseAddress, "GetDpiForSystem", 0);
GetSystemMetricsForDpi_I = PhGetDllBaseProcedureAddress(baseAddress, "GetSystemMetricsForDpi", 0);
SystemParametersInfoForDpi_I = PhGetDllBaseProcedureAddress(baseAddress, "SystemParametersInfoForDpi", 0);
}
}
PhGuiSupportUpdateSystemMetrics(NULL);
}
VOID PhGuiSupportUpdateSystemMetrics(
_In_opt_ HWND WindowHandle
)
{
PhSystemDpi = WindowHandle ? PhGetWindowDpi(WindowHandle) : PhGetSystemDpi();
PhSmallIconSize.X = PhGetSystemMetrics(SM_CXSMICON, PhSystemDpi);
PhSmallIconSize.Y = PhGetSystemMetrics(SM_CYSMICON, PhSystemDpi);
PhLargeIconSize.X = PhGetSystemMetrics(SM_CXICON, PhSystemDpi);
PhLargeIconSize.Y = PhGetSystemMetrics(SM_CYICON, PhSystemDpi);
}
VOID PhInitializeFont(
_In_ HWND WindowHandle
)
{
LONG windowDpi = PhGetWindowDpi(WindowHandle);
HFONT oldFont = PhApplicationFont;
if (
!(PhApplicationFont = PhCreateFont(L"Microsoft Sans Serif", 8, FW_NORMAL, DEFAULT_PITCH, windowDpi)) &&
!(PhApplicationFont = PhCreateFont(L"Tahoma", 8, FW_NORMAL, DEFAULT_PITCH, windowDpi))
)
{
PhApplicationFont = PhCreateMessageFont(windowDpi);
}
if (oldFont) DeleteFont(oldFont);
}
VOID PhInitializeMonospaceFont(
_In_ HWND WindowHandle
)
{
LONG windowDpi = PhGetWindowDpi(WindowHandle);
HFONT oldFont = PhMonospaceFont;
if (
!(PhMonospaceFont = PhCreateFont(L"Lucida Console", 9, FW_DONTCARE, FF_MODERN, windowDpi)) &&
!(PhMonospaceFont = PhCreateFont(L"Courier New", 9, FW_DONTCARE, FF_MODERN, windowDpi)) &&
!(PhMonospaceFont = PhCreateFont(NULL, 9, FW_DONTCARE, FF_MODERN, windowDpi))
)
{
PhMonospaceFont = GetStockFont(SYSTEM_FIXED_FONT);
}
if (oldFont) DeleteFont(oldFont);
}
/**
* The PhGetScreenDC function retrieves a handle to a device context (DC) for the entire screen.
*
* \return The handle to the requested stock object.
*/
HDC PhGetScreenDC(
VOID
)
{
static HDC hdc = nullptr;
if (hdc == NULL)
{
hdc = GetDC(NULL);
}
return hdc;
}
/**
* The PhGetStockBrush function retrieves a handle to one of the stock pens, brushes, fonts, or palettes.
*
* \param Index The type of stock object.
* \return The handle to the requested stock object.
*/
HGDIOBJ PhGetStockObject(
_In_ LONG Index
)
{
static HBRUSH brush[STOCK_LAST + 1] = { nullptr };
assert(Index <= STOCK_LAST);
if (brush[Index] == NULL)
{
brush[Index] = GetStockObject(Index);
}
return brush[Index];
}
/**
* Opens the theme data for the specified window handle, class list, and window DPI.
*
* \param WindowHandle The handle to the window for which to open the theme data. Can be NULL.
* \param ClassList The class list of the theme to open.
* \param WindowDpi The DPI of the window.
* \return The handle to the opened theme data, or NULL if the theme data could not be opened.
*/
HTHEME PhOpenThemeData(
_In_opt_ HWND WindowHandle,
_In_ PCWSTR ClassList,
_In_ LONG WindowDpi
)
{
if (OpenThemeDataForDpi_I && WindowDpi)
{
return OpenThemeDataForDpi_I(WindowHandle, ClassList, WindowDpi);
}
if (OpenThemeData_I)
{
return OpenThemeData_I(WindowHandle, ClassList);
}
return NULL;
}
VOID PhCloseThemeData(
_In_ HTHEME ThemeHandle
)
{
if (CloseThemeData_I)
{
CloseThemeData_I(ThemeHandle);
}
}
VOID PhSetControlTheme(
_In_ HWND Handle,
_In_opt_ PCWSTR Theme
)
{
if (SetWindowTheme_I)
{
SetWindowTheme_I(Handle, Theme, NULL);
}
}
BOOLEAN PhIsThemeActive(
VOID
)
{
if (!IsThemeActive_I)
return FALSE;
return !!IsThemeActive_I();
}
BOOLEAN PhIsThemePartDefined(
_In_ HTHEME ThemeHandle,
_In_ LONG PartId,
_In_ LONG StateId
)
{
if (!IsThemePartDefined_I)
return FALSE;
return !!IsThemePartDefined_I(ThemeHandle, PartId, StateId);
}
_Success_(return)
BOOLEAN PhGetThemeClass(
_In_ HTHEME ThemeHandle,
_Out_writes_z_(ClassLength) PWSTR Class,
_In_ ULONG ClassLength
)
{
if (!GetThemeClass_I)
return FALSE;
return SUCCEEDED(GetThemeClass_I(ThemeHandle, Class, ClassLength));
}
_Success_(return)
BOOLEAN PhGetThemeColor(
_In_ HTHEME ThemeHandle,
_In_ LONG PartId,
_In_ LONG StateId,
_In_ LONG PropId,
_Out_ COLORREF* Color
)
{
if (!GetThemeColor_I)
return FALSE;
return SUCCEEDED(GetThemeColor_I(ThemeHandle, PartId, StateId, PropId, Color));
}
_Success_(return)
BOOLEAN PhGetThemeInt(
_In_ HTHEME ThemeHandle,
_In_ LONG PartId,
_In_ LONG StateId,
_In_ LONG PropId,
_Out_ PLONG Value
)
{
if (!GetThemeInt_I)
return FALSE;
return SUCCEEDED(GetThemeInt_I(ThemeHandle, PartId, StateId, PropId, Value));
}
_Success_(return)
BOOLEAN PhGetThemePartSize(
_In_ HTHEME ThemeHandle,
_In_opt_ HDC hdc,
_In_ LONG PartId,
_In_ LONG StateId,
_In_opt_ LPCRECT Rect,
_In_ THEMEPARTSIZE Flags,
_Out_ PSIZE Size
)
{
if (!GetThemePartSize_I)
return FALSE;
return SUCCEEDED(GetThemePartSize_I(ThemeHandle, hdc, PartId, StateId, Rect, Flags, Size));
}
BOOLEAN PhDrawThemeBackground(
_In_ HTHEME ThemeHandle,
_In_ HDC hdc,
_In_ LONG PartId,
_In_ LONG StateId,
_In_ LPCRECT Rect,
_In_opt_ LPCRECT ClipRect
)
{
if (!DrawThemeBackground_I)
return FALSE;
return SUCCEEDED(DrawThemeBackground_I(ThemeHandle, hdc, PartId, StateId, Rect, ClipRect));
}
BOOLEAN PhDrawThemeTextEx(
_In_ HTHEME ThemeHandle,
_In_ HDC hdc,
_In_ LONG PartId,
_In_ LONG StateId,
_In_reads_(cchText) LPCWSTR Text,
_In_ LONG cchText,
_In_ ULONG TextFlags,
_Inout_ LPRECT Rect,
_In_opt_ const PVOID Options // DTTOPTS*
)
{
if (!DrawThemeTextEx_I)
DrawThemeTextEx_I = PhGetModuleProcAddress(L"uxtheme.dll", "DrawThemeTextEx");
if (!DrawThemeTextEx_I)
return FALSE;
return SUCCEEDED(DrawThemeTextEx_I(ThemeHandle, hdc, PartId, StateId, Text, cchText, TextFlags, Rect, Options));
}
BOOLEAN PhAllowDarkModeForWindow(
_In_ HWND WindowHandle,
_In_ BOOL Enabled
)
{
if (!AllowDarkModeForWindow_I)
return FALSE;
return !!AllowDarkModeForWindow_I(WindowHandle, Enabled);
}
BOOLEAN PhIsDarkModeAllowedForWindow(
_In_ HWND WindowHandle
)
{
if (!IsDarkModeAllowedForWindow_I)
return FALSE;
return !!IsDarkModeAllowedForWindow_I(WindowHandle);
}
BOOLEAN PhShouldAppsUseDarkMode(
VOID
)
{
if (!ShouldAppsUseDarkMode_I)
return FALSE;
return !!ShouldAppsUseDarkMode_I();
}
BOOLEAN PhIsThemeSupportEnabled(
VOID
)
{
if (PhGetIntegerSetting(L"EnableThemeSupport"))
{
if (PhGetIntegerSetting(L"EnableThemeUseWindowsTheme"))
return PhShouldAppsUseDarkMode();
else
return TRUE;
}
else
{
return FALSE;
}
}
BOOLEAN PhSetPreferredAppMode(
_In_ PreferredAppMode AppMode
)
{
if (!SetPreferredAppMode_I)
return FALSE;
return !!SetPreferredAppMode_I(AppMode);
}
// rev from EtwRundown.dll!EtwpLogDPISettingsInfo (dmex)
//LONG PhGetUserOrMachineDpi(
// VOID
// )
//{
// static PH_STRINGREF machineKeyName = PH_STRINGREF_INIT(L"System\\CurrentControlSet\\Hardware Profiles\\Current\\Software\\Fonts");
// static PH_STRINGREF userKeyName = PH_STRINGREF_INIT(L"Control Panel\\Desktop");
// HANDLE keyHandle;
// LONG dpi = 0;
//
// if (NT_SUCCESS(PhOpenKey(&keyHandle, KEY_QUERY_VALUE, PH_KEY_USERS, &userKeyName, 0)))
// {
// dpi = PhQueryRegistryUlongZ(keyHandle, L"LogPixels");
// NtClose(keyHandle);
// }
//
// if (dpi == 0)
// {
// if (NT_SUCCESS(PhOpenKey(&keyHandle, KEY_QUERY_VALUE, PH_KEY_LOCAL_MACHINE, &machineKeyName, 0)))
// {
// dpi = PhQueryRegistryUlongZ(keyHandle, L"LogPixels");
// NtClose(keyHandle);
// }
// }
//
// return dpi;
//}
BOOLEAN PhGetWindowRect(
_In_ HWND WindowHandle,
_Out_ LPRECT WindowRect
)
{
// Note: GetWindowRect can return success with either invalid (0,0) or empty rects (40,40) and in some cases
// this results in unwanted clipping, performance issues with the CreateCompatibleBitmap double buffering and
// issues with MonitorFromRect layout and DPI queries, so ignore the return status and check the rect (dmex)
GetWindowRect(WindowHandle, WindowRect);
if (PhRectEmpty(WindowRect))
return FALSE;
return TRUE;
}
BOOLEAN PhIsHungAppWindow(
_In_ HWND WindowHandle
)
{
return !!IsHungAppWindow(WindowHandle);
}
HWND PhGetShellWindow(
VOID
)
{
return GetShellWindow();
}
BOOLEAN PhCheckWindowThreadDesktop(
_In_ HWND WindowHandle,
_In_ HANDLE ThreadId
)
{
typedef LOGICAL (WINAPI* CheckWindowThreadDesktop)(
_In_ HWND WindowHandle,
_In_ ULONG ThreadId
);
static PH_INITONCE initOnce = PH_INITONCE_INIT;
static CheckWindowThreadDesktop CheckWindowThreadDesktop_I = NULL;
if (PhBeginInitOnce(&initOnce))
{
PVOID baseAddress;
if (baseAddress = PhLoadLibrary(L"user32.dll"))
{
CheckWindowThreadDesktop_I = PhGetDllBaseProcedureAddress(baseAddress, "CheckWindowThreadDesktop", 0);
}
PhEndInitOnce(&initOnce);
}
if (!CheckWindowThreadDesktop_I)
return FALSE;
return !!CheckWindowThreadDesktop_I(WindowHandle, HandleToUlong(ThreadId));
}
LONG PhGetDpi(
_In_ LONG Number,
_In_ LONG DpiValue
)
{
return PhMultiplyDivideSigned(Number, DpiValue, USER_DEFAULT_SCREEN_DPI);
}
LONG PhGetMonitorDpi(
_In_ LPCRECT rect
)
{
return PhGetDpiValue(NULL, rect);
}
LONG PhGetSystemDpi(
VOID
)
{
LONG dpi;
dpi = PhGetTaskbarDpi();
if (dpi == 0)
{
dpi = PhGetDpiValue(NULL, NULL);
}
if (dpi == 0)
{
dpi = USER_DEFAULT_SCREEN_DPI;
}
return dpi;
}
// rev from GetDpiForShellUIComponent (dmex)
//LONG PhGetShellDpi(
// VOID
// )
//{
// static HWND trayWindow = NULL;
// HWND windowHandle;
// LONG dpi = 0;
//
// if (IsWindow(trayWindow))
// {
// windowHandle = trayWindow;
// }
// else
// {
// windowHandle = trayWindow = FindWindow(L"Shell_TrayWnd", NULL);
// }
//
// if (windowHandle)
// {
// dpi = HandleToLong(GetProp(windowHandle, L"TaskbarDPI_NotificationArea"));
// }
//
// //if (dpi == 0)
// //{
// // dpi = GetDpiForShellUIComponent(SHELL_UI_COMPONENT_NOTIFICATIONAREA);
// //}
//
// if (dpi == 0)
// {
// dpi = USER_DEFAULT_SCREEN_DPI;
// }
//
// return dpi;
//}
LONG PhGetTaskbarDpi(
VOID
)
{
LONG dpi = 0;
HWND windowHandle;
RECT windowRect = { 0 };
if (windowHandle = PhGetShellWindow())
{
GetWindowRect(windowHandle, &windowRect);
}
if (PhRectEmpty(&windowRect))
{
if (windowHandle = GetDesktopWindow())
{
GetWindowRect(windowHandle, &windowRect);
}
}
//if (PhRectEmpty(&windowRect))
//{
// APPBARDATA appbarData = { sizeof(APPBARDATA) };
//
// if (SHAppBarMessage(ABM_GETTASKBARPOS, &appbarData))
// {
// windowRect = appbarData.rc;
// }
//}
if (!PhRectEmpty(&windowRect))
{
dpi = PhGetMonitorDpi(&windowRect);
}
if (dpi == 0)
{
dpi = PhGetDpiValue(NULL, NULL);
}
//if (dpi == 0)
//{
// dpi = PhGetShellDpi();
//}
if (dpi == 0)
{
dpi = USER_DEFAULT_SCREEN_DPI;
}
return dpi;
}
LONG PhGetWindowDpi(
_In_ HWND WindowHandle
)
{
LONG dpi = 0;
RECT windowRect;
if (PhGetWindowRect(WindowHandle, &windowRect))
{
dpi = PhGetDpiValue(NULL, &windowRect);
}
if (dpi == 0)
{
dpi = PhGetDpiValue(WindowHandle, NULL);
}
if (dpi == 0)
{
dpi = USER_DEFAULT_SCREEN_DPI;
}
return dpi;
}
LONG PhGetDpiValue(
_In_opt_ HWND WindowHandle,
_In_opt_ LPCRECT Rect
)
{
if (Rect || WindowHandle)
{
// Windows 10 (RS1)
if (GetDpiForWindow_I && WindowHandle)
{
LONG dpi;
if (dpi = GetDpiForWindow_I(WindowHandle))
{
return dpi;
}
}
// Windows 8.1
if (GetDpiForMonitor_I)
{
HMONITOR monitor;
LONG dpi_x;
LONG dpi_y;
if (Rect)
monitor = MonitorFromRect(Rect, MONITOR_DEFAULTTONEAREST);
else
monitor = MonitorFromWindow(WindowHandle, MONITOR_DEFAULTTONEAREST);
if (HR_SUCCESS(GetDpiForMonitor_I(monitor, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y)))
{
return dpi_x;
}
}
}
// Windows 10 (RS1)
if (GetDpiForSystem_I)
{
return GetDpiForSystem_I();
}
// Windows 7 and Windows 8
{
HDC screenHdc;
LONG dpi_x;
if (screenHdc = GetDC(NULL))
{
dpi_x = GetDeviceCaps(screenHdc, LOGPIXELSX);
ReleaseDC(NULL, screenHdc);
return dpi_x;
}
}
return USER_DEFAULT_SCREEN_DPI;
}
/**
* Retrieves the system metrics for the specified index.
*
* \param Index The index of the system metric to retrieve.
* \param DpiValue The DPI value to use for retrieving the system metric. If not provided, the default DPI value will be used.
* \return The value of the system metric.
*/
LONG PhGetSystemMetrics(
_In_ LONG Index,
_In_opt_ LONG DpiValue
)
{
if (GetSystemMetricsForDpi_I && DpiValue)
{
return GetSystemMetricsForDpi_I(Index, DpiValue);
}
return GetSystemMetrics(Index);
}
BOOLEAN PhGetSystemSafeBootMode(
VOID
)
{
if (WindowsVersion < WINDOWS_NEW)
{
return !!USER_SHARED_DATA->SafeBootMode;
}
return !!PhGetSystemMetrics(SM_CLEANBOOT, 0);
}
BOOL PhGetSystemParametersInfo(
_In_ LONG Action,
_In_ ULONG Param1,
_Pre_maybenull_ _Post_valid_ PVOID Param2,
_In_opt_ LONG DpiValue
)
{
if (SystemParametersInfoForDpi_I && DpiValue)
{
return SystemParametersInfoForDpi_I(Action, Param1, Param2, 0, DpiValue);
}
return SystemParametersInfo(Action, Param1, Param2, 0);
}
VOID PhGetSizeDpiValue(
_Inout_ PRECT rect,
_In_ LONG DpiValue,
_In_ BOOLEAN isUnpack
)
{
PH_RECTANGLE rectangle;
LONG numerator;
LONG denominator;
if (DpiValue == USER_DEFAULT_SCREEN_DPI)
return;
if (isUnpack)
{
numerator = DpiValue;
denominator = USER_DEFAULT_SCREEN_DPI;
}
else
{
numerator = USER_DEFAULT_SCREEN_DPI;
denominator = DpiValue;
}
rectangle.Left = rect->left;
rectangle.Top = rect->top;
rectangle.Width = rect->right - rect->left;
rectangle.Height = rect->bottom - rect->top;
if (rectangle.Left)
rectangle.Left = PhMultiplyDivideSigned(rectangle.Left, numerator, denominator);
if (rectangle.Top)
rectangle.Top = PhMultiplyDivideSigned(rectangle.Top, numerator, denominator);
if (rectangle.Width)
rectangle.Width = PhMultiplyDivideSigned(rectangle.Width, numerator, denominator);
if (rectangle.Height)
rectangle.Height = PhMultiplyDivideSigned(rectangle.Height, numerator, denominator);
rect->left = rectangle.Left;
rect->top = rectangle.Top;
rect->right = rectangle.Left + rectangle.Width;
rect->bottom = rectangle.Top + rectangle.Height;
}
LONG PhAddListViewColumn(
_In_ HWND ListViewHandle,
_In_ LONG Index,
_In_ LONG DisplayIndex,
_In_ LONG SubItemIndex,
_In_ LONG Format,
_In_ LONG Width,
_In_ PCWSTR Text
)
{
LVCOLUMN column;
LONG dpiValue;
dpiValue = PhGetWindowDpi(ListViewHandle);
memset(&column, 0, sizeof(LVCOLUMN));
column.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM | LVCF_ORDER;
column.fmt = Format;
column.cx = Width < 0 ? -Width : PhGetDpi(Width, dpiValue);
column.pszText = (PWSTR)Text;
column.iSubItem = SubItemIndex;
column.iOrder = DisplayIndex;
return ListView_InsertColumn(ListViewHandle, Index, &column);
}
LONG PhAddListViewItem(
_In_ HWND ListViewHandle,
_In_ LONG Index,
_In_ PCWSTR Text,
_In_opt_ PVOID Param
)
{
LVITEM item;
item.mask = LVIF_TEXT | LVIF_PARAM;
item.iItem = Index;
item.iSubItem = 0;
item.pszText = (PWSTR)Text;
item.lParam = (LPARAM)Param;
return ListView_InsertItem(ListViewHandle, &item);
}
LONG PhFindListViewItemByFlags(
_In_ HWND ListViewHandle,
_In_ LONG StartIndex,
_In_ ULONG Flags
)
{
return ListView_GetNextItem(ListViewHandle, StartIndex, Flags);
}
LONG PhFindListViewItemByParam(
_In_ HWND ListViewHandle,
_In_ LONG StartIndex,
_In_opt_ PVOID Param
)
{
LVFINDINFO findInfo;
findInfo.flags = LVFI_PARAM;
findInfo.lParam = (LPARAM)Param;
return ListView_FindItem(ListViewHandle, StartIndex, &findInfo);
}
_Success_(return)
BOOLEAN PhGetListViewItemImageIndex(
_In_ HWND ListViewHandle,
_In_ LONG Index,
_Out_ PLONG ImageIndex
)
{
LVITEM item;
item.mask = LVIF_IMAGE;
item.iItem = Index;
item.iSubItem = 0;
if (!ListView_GetItem(ListViewHandle, &item))
return FALSE;
*ImageIndex = item.iImage;
return TRUE;
}
_Success_(return)
BOOLEAN PhGetListViewItemParam(
_In_ HWND ListViewHandle,
_In_ LONG Index,
_Outptr_ PVOID *Param
)
{
LVITEM item;
item.mask = LVIF_PARAM;
item.iItem = Index;
item.iSubItem = 0;
if (!ListView_GetItem(ListViewHandle, &item))
return FALSE;
*Param = (PVOID)item.lParam;
return TRUE;
}
BOOLEAN PhSetListViewItemParam(
_In_ HWND ListViewHandle,
_In_ LONG Index,
_In_ PVOID Param
)
{
LVITEM item;
item.mask = LVIF_PARAM;
item.iItem = Index;
item.lParam = (LPARAM)Param;
return !!ListView_SetItem(ListViewHandle, &item);
}
VOID PhRemoveListViewItem(
_In_ HWND ListViewHandle,
_In_ LONG Index
)
{
ListView_DeleteItem(ListViewHandle, Index);
}
VOID PhSetListViewItemImageIndex(