forked from bruderstein/PythonScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdpiManagerV2.cpp
More file actions
209 lines (182 loc) · 5.31 KB
/
dpiManagerV2.cpp
File metadata and controls
209 lines (182 loc) · 5.31 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
// This file is part of Notepad++ project
// Copyright (c) 2024 ozone10 and Notepad++ team
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// at your option any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "dpiManagerV2.h"
#include <commctrl.h>
template <typename P>
bool ptrFn(HMODULE handle, P& pointer, const char* name)
{
auto p = reinterpret_cast<P>(::GetProcAddress(handle, name));
if (p != nullptr)
{
pointer = p;
return true;
}
return false;
}
using fnGetDpiForSystem = UINT (WINAPI*)(VOID);
using fnGetDpiForWindow = UINT (WINAPI*)(HWND hwnd);
using fnGetSystemMetricsForDpi = int (WINAPI*)(int nIndex, UINT dpi);
using fnSystemParametersInfoForDpi = BOOL (WINAPI*)(UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWinIni, UINT dpi);
using fnSetThreadDpiAwarenessContext = DPI_AWARENESS_CONTEXT (WINAPI*)(DPI_AWARENESS_CONTEXT dpiContext);
using fnAdjustWindowRectExForDpi = BOOL (WINAPI*)(LPRECT lpRect, DWORD dwStyle, BOOL bMenu, DWORD dwExStyle, UINT dpi);
fnGetDpiForSystem _fnGetDpiForSystem = nullptr;
fnGetDpiForWindow _fnGetDpiForWindow = nullptr;
fnGetSystemMetricsForDpi _fnGetSystemMetricsForDpi = nullptr;
fnSystemParametersInfoForDpi _fnSystemParametersInfoForDpi = nullptr;
fnSetThreadDpiAwarenessContext _fnSetThreadDpiAwarenessContext = nullptr;
fnAdjustWindowRectExForDpi _fnAdjustWindowRectExForDpi = nullptr;
void DPIManagerV2::initDpiAPI()
{
if (NppDarkMode::isWindows10())
{
HMODULE hUser32 = ::GetModuleHandleW(L"user32.dll");
if (hUser32 != nullptr)
{
ptrFn(hUser32, _fnGetDpiForSystem, "GetDpiForSystem");
ptrFn(hUser32, _fnGetDpiForWindow, "GetDpiForWindow");
ptrFn(hUser32, _fnGetSystemMetricsForDpi, "GetSystemMetricsForDpi");
ptrFn(hUser32, _fnSystemParametersInfoForDpi, "SystemParametersInfoForDpi");
ptrFn(hUser32, _fnSetThreadDpiAwarenessContext, "SetThreadDpiAwarenessContext");
ptrFn(hUser32, _fnAdjustWindowRectExForDpi, "AdjustWindowRectExForDpi");
}
}
}
int DPIManagerV2::getSystemMetricsForDpi(int nIndex, UINT dpi)
{
if (_fnGetSystemMetricsForDpi != nullptr)
{
return _fnGetSystemMetricsForDpi(nIndex, dpi);
}
return DPIManagerV2::scale(::GetSystemMetrics(nIndex), dpi);
}
DPI_AWARENESS_CONTEXT DPIManagerV2::setThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT dpiContext)
{
if (_fnSetThreadDpiAwarenessContext != nullptr)
{
return _fnSetThreadDpiAwarenessContext(dpiContext);
}
return NULL;
}
BOOL DPIManagerV2::adjustWindowRectExForDpi(LPRECT lpRect, DWORD dwStyle, BOOL bMenu, DWORD dwExStyle, UINT dpi)
{
if (_fnAdjustWindowRectExForDpi != nullptr)
{
return _fnAdjustWindowRectExForDpi(lpRect, dwStyle, bMenu, dwExStyle, dpi);
}
return FALSE;
}
UINT DPIManagerV2::getDpiForSystem()
{
if (_fnGetDpiForSystem != nullptr)
{
return _fnGetDpiForSystem();
}
UINT dpi = USER_DEFAULT_SCREEN_DPI;
HDC hdc = ::GetDC(nullptr);
if (hdc != nullptr)
{
dpi = ::GetDeviceCaps(hdc, LOGPIXELSX);
::ReleaseDC(nullptr, hdc);
}
return dpi;
}
UINT DPIManagerV2::getDpiForWindow(HWND hWnd)
{
if (_fnGetDpiForWindow != nullptr)
{
const auto dpi = _fnGetDpiForWindow(hWnd);
if (dpi > 0)
{
return dpi;
}
}
return getDpiForSystem();
}
void DPIManagerV2::setPositionDpi(LPARAM lParam, HWND hWnd, UINT flags)
{
const auto prcNewWindow = reinterpret_cast<RECT*>(lParam);
::SetWindowPos(hWnd,
nullptr,
prcNewWindow->left,
prcNewWindow->top,
prcNewWindow->right - prcNewWindow->left,
prcNewWindow->bottom - prcNewWindow->top,
flags);
}
LOGFONT DPIManagerV2::getDefaultGUIFontForDpi(UINT dpi, FontType type)
{
int result = 0;
LOGFONT lf{};
NONCLIENTMETRICS ncm{};
ncm.cbSize = sizeof(NONCLIENTMETRICS);
if (_fnSystemParametersInfoForDpi != nullptr
&& (_fnSystemParametersInfoForDpi(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0, dpi) != FALSE))
{
result = 2;
}
else if (::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0) != FALSE)
{
result = 1;
}
if (result > 0)
{
switch (type)
{
case FontType::menu:
{
lf = ncm.lfMenuFont;
break;
}
case FontType::status:
{
lf = ncm.lfStatusFont;
break;
}
case FontType::caption:
{
lf = ncm.lfCaptionFont;
break;
}
case FontType::smcaption:
{
lf = ncm.lfSmCaptionFont;
break;
}
//case FontType::message:
default:
{
lf = ncm.lfMessageFont;
break;
}
}
}
else // should not happen, fallback
{
auto hf = static_cast<HFONT>(::GetStockObject(DEFAULT_GUI_FONT));
::GetObject(hf, sizeof(LOGFONT), &lf);
}
if (result < 2)
{
lf.lfHeight = scaleFont(lf.lfHeight, dpi);
}
return lf;
}
void DPIManagerV2::loadIcon(HINSTANCE hinst, const wchar_t* pszName, int cx, int cy, HICON* phico, UINT fuLoad)
{
if (::LoadIconWithScaleDown(hinst, pszName, cx, cy, phico) != S_OK)
{
*phico = static_cast<HICON>(::LoadImage(hinst, pszName, IMAGE_ICON, cx, cy, fuLoad));
}
}