Skip to content

Commit 2579f11

Browse files
SergeyKalutskyBoboTiG
authored andcommitted
Windows: fix DPI awarness on Windows 10
Fixes BoboTiG#116.
1 parent 9b23ed5 commit 2579f11

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

mss/windows.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Source: https://github.com/BoboTiG/python-mss
44
"""
55

6+
import sys
67
import ctypes
78
from ctypes.wintypes import (
89
BOOL,
@@ -96,19 +97,7 @@ def __init__(self, **_):
9697
self.user32 = ctypes.WinDLL("user32")
9798
self.gdi32 = ctypes.WinDLL("gdi32")
9899
self._set_cfunctions()
99-
100-
# Set DPI aware to capture full screen on Hi-DPI monitors
101-
try:
102-
# Windows 8.1+
103-
# Automatically scale for DPI changes
104-
self.user32.SetProcessDpiAwareness(
105-
self.user32.PROCESS_PER_MONITOR_DPI_AWARE
106-
)
107-
except AttributeError:
108-
try:
109-
self.user32.SetProcessDPIAware()
110-
except AttributeError:
111-
pass # Windows XP doesn't have SetProcessDPIAware
100+
self._set_dpi_awareness()
112101

113102
self._srcdc = self.user32.GetWindowDC(0)
114103
self._memdc = self.gdi32.CreateCompatibleDC(self._srcdc)
@@ -201,6 +190,21 @@ def close(self):
201190
except AttributeError:
202191
pass
203192

193+
def _set_dpi_awareness(self):
194+
""" Set DPI aware to capture full screen on Hi-DPI monitors. """
195+
196+
version = sys.getwindowsversion()[:2]
197+
if version >= (6, 3):
198+
# Windows 8.1+
199+
# Here 2 = PROCESS_PER_MONITOR_DPI_AWARE, which means:
200+
# per monitor DPI aware. This app checks for the DPI when it is
201+
# created and adjusts the scale factor whenever the DPI changes.
202+
# These applications are not automatically scaled by the system.
203+
ctypes.windll.shcore.SetProcessDpiAwareness(2)
204+
elif (6, 0) <= version < (6, 3):
205+
# Windows Vista, 7, 8 and Server 2012
206+
self.user32.SetProcessDPIAware()
207+
204208
@property
205209
def monitors(self):
206210
# type: () -> Monitors

0 commit comments

Comments
 (0)