Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d1573e6

Browse files
SergeyKalutskyBoboTiG
authored andcommitted
[backport] Windows: fix DPI awarness on Windows 10
1 parent 56b59f1 commit d1573e6

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
@@ -6,6 +6,7 @@
66

77
from __future__ import division
88

9+
import sys
910
import ctypes
1011
from ctypes.wintypes import (
1112
BOOL,
@@ -80,19 +81,7 @@ def __init__(self):
8081
self.user32 = ctypes.WinDLL("user32")
8182
self.gdi32 = ctypes.WinDLL("gdi32")
8283
self._set_cfunctions()
83-
84-
# Set DPI aware to capture full screen on Hi-DPI monitors
85-
try:
86-
# Windows 8.1+
87-
# Automatically scale for DPI changes
88-
self.user32.SetProcessDpiAwareness(
89-
self.user32.PROCESS_PER_MONITOR_DPI_AWARE
90-
)
91-
except AttributeError:
92-
try:
93-
self.user32.SetProcessDPIAware()
94-
except AttributeError:
95-
pass # Windows XP doesn't have SetProcessDPIAware
84+
self._set_dpi_awareness()
9685

9786
self._srcdc = self.user32.GetWindowDC(0)
9887
self._memdc = self.gdi32.CreateCompatibleDC(self._srcdc)
@@ -185,6 +174,21 @@ def close(self):
185174
except AttributeError:
186175
pass
187176

177+
def _set_dpi_awareness(self):
178+
""" Set DPI aware to capture full screen on Hi-DPI monitors. """
179+
180+
version = sys.getwindowsversion()[:2]
181+
if version >= (6, 3):
182+
# Windows 8.1+
183+
# Here 2 = PROCESS_PER_MONITOR_DPI_AWARE, which means:
184+
# per monitor DPI aware. This app checks for the DPI when it is
185+
# created and adjusts the scale factor whenever the DPI changes.
186+
# These applications are not automatically scaled by the system.
187+
ctypes.windll.shcore.SetProcessDpiAwareness(2)
188+
elif (6, 0) <= version < (6, 3):
189+
# Windows Vista, 7, 8 and Server 2012
190+
self.user32.SetProcessDPIAware()
191+
188192
@property
189193
def monitors(self):
190194
# type: () -> List[Dict[str, int]]

0 commit comments

Comments
 (0)