/
DcxDPIModule.cpp
401 lines (362 loc) · 12.8 KB
/
DcxDPIModule.cpp
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
#include "defines.h"
#include "DcxDPIModule.h"
#include "Dcx.h"
#pragma warning(push)
#pragma warning(disable: 26425) //warning C26425 : Assigning 'nullptr' to a static variable.
DcxDPIModule::~DcxDPIModule() noexcept
{
unload();
}
bool DcxDPIModule::load()
{
if (isUseable())
return false;
// DPIModule Loading
DCX_DEBUG(mIRCLinker::debug, __FUNCTIONW__, TEXT("Loading User32.DLL..."));
m_hModule = LoadLibrary(TEXT("User32.DLL"));
if (m_hModule)
{
// Get function pointers.
#pragma warning(push)
#pragma warning(disable: 4191)
#pragma warning(disable: 26493) //warning C26493 : Don't use C-style casts that would perform a static_cast downcast, const_cast, or reinterpret_cast. (type.4: http://go.microsoft.com/fwlink/p/?LinkID=620420)
#define STRINGIFY(proc) #proc
#define ASSIGN_PROC(proc) proc ## Ux = reinterpret_cast<decltype(::proc)*>(GetProcAddress(m_hModule, STRINGIFY(proc)));
ASSIGN_PROC(AdjustWindowRectExForDpi);
ASSIGN_PROC(AreDpiAwarenessContextsEqual);
ASSIGN_PROC(EnableNonClientDpiScaling);
ASSIGN_PROC(GetAwarenessFromDpiAwarenessContext);
ASSIGN_PROC(GetDialogControlDpiChangeBehavior);
ASSIGN_PROC(GetDialogDpiChangeBehavior);
ASSIGN_PROC(GetDpiAwarenessContextForProcess);
ASSIGN_PROC(GetDpiForSystem);
ASSIGN_PROC(GetDpiForWindow);
ASSIGN_PROC(GetDpiFromDpiAwarenessContext);
ASSIGN_PROC(GetSystemDpiForProcess);
ASSIGN_PROC(GetSystemMetricsForDpi);
ASSIGN_PROC(GetThreadDpiAwarenessContext);
ASSIGN_PROC(GetThreadDpiHostingBehavior);
ASSIGN_PROC(GetWindowDpiAwarenessContext);
ASSIGN_PROC(GetWindowDpiHostingBehavior);
ASSIGN_PROC(InheritWindowMonitor);
ASSIGN_PROC(IsValidDpiAwarenessContext);
ASSIGN_PROC(SetDialogControlDpiChangeBehavior);
ASSIGN_PROC(SetDialogDpiChangeBehavior);
ASSIGN_PROC(SetProcessDpiAwarenessContext);
ASSIGN_PROC(SetThreadCursorCreationScaling);
ASSIGN_PROC(SetThreadDpiAwarenessContext);
ASSIGN_PROC(SetThreadDpiHostingBehavior);
ASSIGN_PROC(SystemParametersInfoForDpi);
#pragma warning(pop)
if (GetDpiForSystemUx && GetDpiForWindowUx && GetSystemMetricsForDpiUx)
{
DCX_DEBUG(mIRCLinker::debug, __FUNCTIONW__, TEXT("Found Win10+ DPI Functions"));
}
else {
unload();
// silently fail...
DCX_DEBUG(mIRCLinker::debug, __FUNCTIONW__, TEXT("Missing DPI Functions"));
//throw Dcx::dcxException("There was a problem loading User32 Library");
}
}
return isUseable();
}
bool DcxDPIModule::unload() noexcept
{
if (isUseable())
{
GSL_SUPPRESS(lifetime.1) FreeLibrary(m_hModule);
m_hModule = nullptr;
}
// make sure all functions are nullptr
AdjustWindowRectExForDpiUx = nullptr;
AreDpiAwarenessContextsEqualUx = nullptr;
EnableNonClientDpiScalingUx = nullptr;
GetAwarenessFromDpiAwarenessContextUx = nullptr;
GetDialogControlDpiChangeBehaviorUx = nullptr;
GetDialogDpiChangeBehaviorUx = nullptr;
GetDpiAwarenessContextForProcessUx = nullptr;
GetDpiForSystemUx = nullptr;
GetDpiForWindowUx = nullptr;
GetDpiFromDpiAwarenessContextUx = nullptr;
GetSystemDpiForProcessUx = nullptr;
GetSystemMetricsForDpiUx = nullptr;
GetThreadDpiAwarenessContextUx = nullptr;
GetThreadDpiHostingBehaviorUx = nullptr;
GetWindowDpiAwarenessContextUx = nullptr;
GetWindowDpiHostingBehaviorUx = nullptr;
InheritWindowMonitorUx = nullptr;
IsValidDpiAwarenessContextUx = nullptr;
SetDialogControlDpiChangeBehaviorUx = nullptr;
SetDialogDpiChangeBehaviorUx = nullptr;
SetProcessDpiAwarenessContextUx = nullptr;
SetThreadCursorCreationScalingUx = nullptr;
SetThreadDpiAwarenessContextUx = nullptr;
SetThreadDpiHostingBehaviorUx = nullptr;
SystemParametersInfoForDpiUx = nullptr;
return isUseable();
}
#pragma warning(pop)
/// <summary>
/// Does the same as AdjustWindowRectEx() but taking a given dpi into account.
/// </summary>
/// <param name="lpRect"></param>
/// <param name="dwStyle"></param>
/// <param name="bMenu"></param>
/// <param name="dwExStyle"></param>
/// <param name="dpi"></param>
/// <returns></returns>
BOOL DcxDPIModule::dcxAdjustWindowRectExForDpi(_Inout_ LPRECT lpRect, _In_ DWORD dwStyle, _In_ BOOL bMenu, _In_ DWORD dwExStyle, _In_ UINT dpi) noexcept
{
if (AdjustWindowRectExForDpiUx)
return AdjustWindowRectExForDpiUx(lpRect, dwStyle, bMenu, dwExStyle, dpi);
return AdjustWindowRectEx(lpRect, dwStyle, bMenu, dwExStyle);
}
/// <summary>
/// Test if two contexts are the same.
/// </summary>
/// <param name="dpiContextA"></param>
/// <param name="dpiContextB"></param>
/// <returns></returns>
BOOL DcxDPIModule::dcxAreDpiAwarenessContextsEqual(_In_ DPI_AWARENESS_CONTEXT dpiContextA, _In_ DPI_AWARENESS_CONTEXT dpiContextB) noexcept
{
if (AreDpiAwarenessContextsEqualUx)
return AreDpiAwarenessContextsEqualUx(dpiContextA, dpiContextB);
SetLastError(ERROR_INVALID_FUNCTION);
return FALSE;
}
/// <summary>
/// Enable non client scaling.
/// </summary>
/// <param name="hwnd"></param>
/// <returns></returns>
BOOL DcxDPIModule::dcxEnableNonClientDpiScaling(_In_ HWND hwnd) noexcept
{
if (EnableNonClientDpiScalingUx)
return EnableNonClientDpiScalingUx(hwnd);
SetLastError(ERROR_INVALID_FUNCTION);
return FALSE;
}
/// <summary>
/// Get the awareness value from a given context.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
DPI_AWARENESS DcxDPIModule::dcxGetAwarenessFromDpiAwarenessContext(_In_ DPI_AWARENESS_CONTEXT value) noexcept
{
if (GetAwarenessFromDpiAwarenessContextUx)
return GetAwarenessFromDpiAwarenessContextUx(value);
SetLastError(ERROR_INVALID_FUNCTION);
return DPI_AWARENESS_INVALID;
}
/// <summary>
/// Get the behaviour of a given dialog control when the dpi changes.
/// </summary>
/// <param name="hWnd"></param>
/// <returns></returns>
DIALOG_CONTROL_DPI_CHANGE_BEHAVIORS DcxDPIModule::dcxGetDialogControlDpiChangeBehavior(HWND hWnd) noexcept
{
if (GetDialogControlDpiChangeBehaviorUx)
return GetDialogControlDpiChangeBehaviorUx(hWnd);
SetLastError(ERROR_INVALID_FUNCTION);
return DCDC_DEFAULT;
}
/// <summary>
/// Get the behaviour of a given dialog when the dpi changes.
/// </summary>
/// <param name="hDlg"></param>
/// <returns></returns>
DIALOG_DPI_CHANGE_BEHAVIORS DcxDPIModule::dcxGetDialogDpiChangeBehavior(HWND hDlg) noexcept
{
if (GetDialogDpiChangeBehaviorUx)
return GetDialogDpiChangeBehaviorUx(hDlg);
SetLastError(ERROR_INVALID_FUNCTION);
return DDC_DEFAULT;
}
/// <summary>
/// Get the awareness context for a given process.
/// </summary>
/// <param name="hProcess"></param>
/// <returns></returns>
DPI_AWARENESS_CONTEXT DcxDPIModule::dcxGetDpiAwarenessContextForProcess(HANDLE hProcess) noexcept
{
if (GetDpiAwarenessContextForProcessUx)
return GetDpiAwarenessContextForProcessUx(hProcess);
SetLastError(ERROR_INVALID_FUNCTION);
return nullptr;
}
/// <summary>
/// Get systems DPI
/// </summary>
/// <returns></returns>
UINT DcxDPIModule::dcxGetDpiForSystem() noexcept
{
if (GetDpiForSystemUx)
return GetDpiForSystemUx();
return 96;
}
/// <summary>
/// Get DPI for window
/// </summary>
/// <param name="hwnd"></param>
/// <returns></returns>
UINT DcxDPIModule::dcxGetDpiForWindow(_In_opt_ HWND hwnd) noexcept
{
if (hwnd && GetDpiForWindowUx)
return GetDpiForWindowUx(hwnd);
return dcxGetDpiForSystem();
}
/// <summary>
/// Get the dpi value for a given dpi awareness context.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
UINT DcxDPIModule::dcxGetDpiFromDpiAwarenessContext(_In_ DPI_AWARENESS_CONTEXT value) noexcept
{
if (GetDpiFromDpiAwarenessContextUx)
return GetDpiFromDpiAwarenessContextUx(value);
SetLastError(ERROR_INVALID_FUNCTION);
return 0;
}
/// <summary>
/// Get the system dpi setting for a given process.
/// Just returns 96 if the dpi function is not available.
/// </summary>
/// <param name="hProcess"></param>
/// <returns></returns>
UINT DcxDPIModule::dcxGetSystemDpiForProcess(HANDLE hProcess) noexcept
{
if (GetSystemDpiForProcessUx)
return GetSystemDpiForProcessUx(hProcess);
// return default dpi of 100% when no function available.
return 96;
}
/// <summary>
/// Get system metrics adjusted for specified DPI.
/// Falls back to GetSystemMetrics() if the dpi equiv is not available.
/// </summary>
/// <param name="nIndex"></param>
/// <param name="dpi"></param>
/// <returns></returns>
UINT DcxDPIModule::dcxGetSystemMetricsForDpi(_In_ int nIndex, _In_ UINT dpi) noexcept
{
if (GetSystemMetricsForDpiUx)
return GetSystemMetricsForDpiUx(nIndex, dpi);
return GetSystemMetrics(nIndex);
}
/// <summary>
/// Get the awareness context for this thread.
/// </summary>
/// <returns></returns>
DPI_AWARENESS_CONTEXT DcxDPIModule::dcxGetThreadDpiAwarenessContext() noexcept
{
if (GetThreadDpiAwarenessContextUx)
return GetThreadDpiAwarenessContextUx();
return nullptr;
}
/// <summary>
/// Get the dpi hosting behaviour for this thread.
/// </summary>
/// <returns></returns>
DPI_HOSTING_BEHAVIOR DcxDPIModule::dcxGetThreadDpiHostingBehavior() noexcept
{
if (GetThreadDpiHostingBehaviorUx)
return GetThreadDpiHostingBehaviorUx();
return DPI_HOSTING_BEHAVIOR_INVALID;
}
/// <summary>
/// Get the awareness context for the given window.
/// </summary>
/// <param name="hwnd"></param>
/// <returns></returns>
DPI_AWARENESS_CONTEXT DcxDPIModule::dcxGetWindowDpiAwarenessContext(_In_ HWND hwnd) noexcept
{
if (GetWindowDpiAwarenessContextUx)
return GetWindowDpiAwarenessContextUx(hwnd);
return nullptr;
}
/// <summary>
/// Get the dpi hosting behaviour for the given window.
/// </summary>
/// <param name="hwnd"></param>
/// <returns></returns>
DPI_HOSTING_BEHAVIOR DcxDPIModule::dcxGetWindowDpiHostingBehavior(_In_ HWND hwnd) noexcept
{
if (GetWindowDpiHostingBehaviorUx)
return GetWindowDpiHostingBehaviorUx(hwnd);
return DPI_HOSTING_BEHAVIOR_INVALID;
}
/// <summary>
/// Get the system parameter for a given dpi.
/// </summary>
/// <param name="uiAction">- The system-wide parameter to be retrieved. This function is only intended for use with SPI_GETICONTITLELOGFONT, SPI_GETICONMETRICS, or SPI_GETNONCLIENTMETRICS</param>
/// <param name="uiParam"></param>
/// <param name="pvParam"></param>
/// <param name="fWinIni"></param>
/// <param name="dpi">- The DPI to use for scaling the metric.</param>
/// <returns></returns>
BOOL DcxDPIModule::dcxSystemParametersInfoForDpi(_In_ UINT uiAction, _In_ UINT uiParam, _Inout_ PVOID pvParam, _In_ UINT fWinIni, _In_ UINT dpi) noexcept
{
if (SystemParametersInfoForDpiUx)
return SystemParametersInfoForDpiUx(uiAction, uiParam, pvParam, fWinIni, dpi);
return FALSE;
}
/// <summary>
/// Get the system metrics for a specific window, based on its current dpi.
/// </summary>
/// <param name="hwnd"></param>
/// <param name="nIndex"></param>
/// <returns></returns>
UINT DcxDPIModule::dcxGetWindowMetrics(_In_opt_ HWND hwnd, _In_ int nIndex) noexcept
{
const auto dpi = dcxGetDpiForWindow(hwnd);
return dcxGetSystemMetricsForDpi(nIndex, dpi);
}
BOOL DcxDPIModule::dcxSetDialogDpiChangeBehavior(HWND hDlg, _In_ DIALOG_DPI_CHANGE_BEHAVIORS mask, _In_ DIALOG_DPI_CHANGE_BEHAVIORS values) noexcept
{
if (SetDialogDpiChangeBehaviorUx)
return SetDialogDpiChangeBehaviorUx(hDlg, mask, values);
return FALSE;
}
BOOL DcxDPIModule::dcxSetProcessDpiAwarenessContext(_In_ DPI_AWARENESS_CONTEXT value) noexcept
{
if (SetProcessDpiAwarenessContextUx)
return SetProcessDpiAwarenessContextUx(value);
return FALSE;
}
UINT DcxDPIModule::dcxSetThreadCursorCreationScaling(_In_ UINT cursorDpi) noexcept
{
if (SetThreadCursorCreationScalingUx)
return SetThreadCursorCreationScalingUx(cursorDpi);
return 96;
}
DPI_AWARENESS_CONTEXT DcxDPIModule::dcxSetThreadDpiAwarenessContext(_In_ DPI_AWARENESS_CONTEXT dpiContext) noexcept
{
if (SetThreadDpiAwarenessContextUx)
return SetThreadDpiAwarenessContextUx(dpiContext);
return nullptr;
}
BOOL DcxDPIModule::dcxInheritWindowMonitor(_In_ HWND hwnd, _In_opt_ HWND hwndInherit) noexcept
{
if (InheritWindowMonitorUx)
return InheritWindowMonitorUx(hwnd, hwndInherit);
return FALSE;
}
BOOL DcxDPIModule::dcxIsValidDpiAwarenessContext(_In_ DPI_AWARENESS_CONTEXT value) noexcept
{
if (IsValidDpiAwarenessContextUx)
return IsValidDpiAwarenessContextUx(value);
return FALSE;
}
BOOL DcxDPIModule::dcxSetDialogControlDpiChangeBehavior(HWND hWnd, _In_ DIALOG_CONTROL_DPI_CHANGE_BEHAVIORS mask, _In_ DIALOG_CONTROL_DPI_CHANGE_BEHAVIORS values) noexcept
{
if (SetDialogControlDpiChangeBehaviorUx)
return SetDialogControlDpiChangeBehaviorUx(hWnd, mask, values);
return FALSE;
}
DPI_HOSTING_BEHAVIOR DcxDPIModule::dcxSetThreadDpiHostingBehavior(_In_ DPI_HOSTING_BEHAVIOR value) noexcept
{
if (SetThreadDpiHostingBehaviorUx)
return SetThreadDpiHostingBehaviorUx(value);
return DPI_HOSTING_BEHAVIOR_INVALID;
}