8000 Set Windows DPI awareness to best possible setting. · matplotlib/matplotlib@8c5b462 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8c5b462

Browse files
committed
Set Windows DPI awareness to best possible setting.
1 parent 361d018 commit 8c5b462

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

src/_c_internal_utils.c

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,39 @@ static PyObject*
128128
mpl_SetDpiAwareness(PyObject* module)
129129
{
130130
#ifdef _WIN32
131-
SetProcessDPIAware();
131+
#ifdef _DPI_AWARENESS_CONTEXTS_
132+
// These functions and options were added in later Windows 10 updates, so
133+
// must be loaded dynamically.
134+
typedef BOOL (WINAPI *IsValidDpiAwarenessContext_t)(DPI_AWARENESS_CONTEXT);
135+
typedef BOOL (WINAPI *SetProcessDpiAwarenessContext_t)(DPI_AWARENESS_CONTEXT);
136+
137+
HMODULE user32 = LoadLibrary("user32.dll");
138+
IsValidDpiAwarenessContext_t IsValidDpiAwarenessContextPtr =
139+
(IsValidDpiAwarenessContext_t)GetProcAddress(
140+
user32, "IsValidDpiAwarenessContext");
141+
SetProcessDpiAwarenessContext_t SetProcessDpiAwarenessContextPtr =
142+
(SetProcessDpiAwarenessContext_t)GetProcAddress(
143+
user32, "SetProcessDpiAwarenessContext");
144+
if (IsValidDpiAwarenessContextPtr != NULL && SetProcessDpiAwarenessContextPtr != NULL) {
145+
if (IsValidDpiAwarenessContextPtr(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2)) {
146+
// Added in Creators Update of Windows 10.
147+
SetProcessDpiAwarenessContextPtr(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
148+
} else if (IsValidDpiAwarenessContextPtr(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE)) {
149+
// Added in Windows 10.
150+
SetProcessDpiAwarenessContextPtr(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE);
151+
} else if (IsValidDpiAwarenessContextPtr(DPI_AWARENESS_CONTEXT_SYSTEM_AWARE)) {
152+
// Added in Windows 10.
153+
SetProcessDpiAwarenessContextPtr(DPI_AWARENESS_CONTEXT_SYSTEM_AWARE);
154+
}
155+
FreeLibrary(user32);
156+
} else {
157+
FreeLibrary(user32);
158+
#else
159+
{
160+
#endif
161+
// Added in Windows Vista.
162+
SetProcessDPIAware();
163+
}
132164
#endif
133165
Py_RETURN_NONE;
134166
}
@@ -163,8 +195,8 @@ static PyMethodDef functions[] = {
163195
{"Win32_SetDpiAwareness",
164196
(PyCFunction)mpl_SetDpiAwareness, METH_NOARGS,
165197
"Win32_SetDpiAwareness()\n--\n\n"
166-
"Set Windows' process DPI awareness to be enabled. On non-Windows\n"
167-
"platforms, does nothing."},
198+
"Set Windows' process DPI awareness to best option available.\n"
199+
"On non-Windows platforms, does nothing."},
168200
{NULL, NULL}}; // sentinel.
169201
static PyModuleDef util_module = {
170202
PyModuleDef_HEAD_INIT, "_c_internal_utils", "", 0, functions, NULL, NULL, NULL, NULL};

0 commit comments

Comments
 (0)
0