@@ -128,7 +128,39 @@ static PyObject*
128
128
mpl_SetDpiAwareness (PyObject * module )
129
129
{
130
130
#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
+ }
132
164
#endif
133
165
Py_RETURN_NONE ;
134
166
}
@@ -163,8 +195,8 @@ static PyMethodDef functions[] = {
163
195
{"Win32_SetDpiAwareness" ,
164
196
(PyCFunction )mpl_SetDpiAwareness , METH_NOARGS ,
165
197
"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." },
168
200
{NULL , NULL }}; // sentinel.
169
201
static PyModuleDef util_module = {
170
202
PyModuleDef_HEAD_INIT , "_c_internal_utils" , "" , 0 , functions , NULL , NULL , NULL , NULL };
0 commit comments