@@ -319,6 +319,8 @@ PyImaging_DisplayModeWin32(PyObject* self, PyObject* args)
319
319
/* -------------------------------------------------------------------- */
320
320
/* Windows screen grabber */
321
321
322
+ typedef HANDLE (__stdcall* Func_SetThreadDpiAwarenessContext )(HANDLE );
323
+
322
324
PyObject *
323
325
PyImaging_GrabScreenWin32 (PyObject * self , PyObject * args )
324
326
{
@@ -329,6 +331,9 @@ PyImaging_GrabScreenWin32(PyObject* self, PyObject* args)
329
331
HDC screen , screen_copy ;
330
332
DWORD rop ;
331
333
PyObject * buffer ;
334
+ HANDLE dpiAwareness ;
335
+ HMODULE user32 ;
336
+ Func_SetThreadDpiAwarenessContext SetThreadDpiAwarenessContext_function ;
332
337
333
338
if (!PyArg_ParseTuple (args , "|ii" , & includeLayeredWindows , & all_screens ))
334
339
return NULL ;
@@ -339,6 +344,17 @@ PyImaging_GrabScreenWin32(PyObject* self, PyObject* args)
339
344
screen = CreateDC ("DISPLAY" , NULL , NULL , NULL );
340
345
screen_copy = CreateCompatibleDC (screen );
341
346
347
+ // added in Windows 10 (1607)
348
+ // loaded dynamically to avoid link errors
349
+ user32 = LoadLibraryA ("User32.dll" );
350
+ SetThreadDpiAwarenessContext_function =
351
+ (Func_SetThreadDpiAwarenessContext )
352
+ GetProcAddress (user32 , "SetThreadDpiAwarenessContext" );
353
+ if (SetThreadDpiAwarenessContext_function != NULL ) {
354
+ // DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = ((DPI_CONTEXT_HANDLE)-3)
355
+ dpiAwareness = SetThreadDpiAwarenessContext_function ((HANDLE ) - 3 );
356
+ }
357
+
342
358
if (all_screens ) {
343
359
x = GetSystemMetrics (SM_XVIRTUALSCREEN );
344
360
y = GetSystemMetrics (SM_YVIRTUALSCREEN );
@@ -349,6 +365,12 @@ PyImaging_GrabScreenWin32(PyObject* self, PyObject* args)
349
365
height = GetDeviceCaps (screen , VERTRES );
350
366
}
351
367
368
+ if (SetThreadDpiAwarenessContext_function != NULL ) {
369
+ SetThreadDpiAwarenessContext_function (dpiAwareness );
370
+ }
371
+
372
+ FreeLibrary (user32 );
373
+
352
374
bitmap = CreateCompatibleBitmap (screen , width , height );
353
375
if (!bitmap )
354
376
goto error ;
0 commit comments