Python 中影响 win32gui GetWindowRect 的 DPI 缩放级别

DPI Scaling Level affecting win32gui GetWindowRect in Python

在 Python 中调用 GetWindowRect() 时,如果使用的 DPI 缩放级别不是 100%,则它 returns 的值不准确。有什么方法可以解决这个问题,或者进行数学调整吗?

您可以使用来自 ctypes 的 windll 让您的程序感知 DPI:

from ctypes import windll

# Make program aware of DPI scaling
user32 = windll.user32
user32.SetProcessDPIAware()

从那时起,像 GetWindowRect() 这样的调用应该 return 正确的值。前阵子我试图让 Pyautogui 给我正确的屏幕截图时偶然发现了这个解决方案。