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

jez*_*zza 1 python win32gui windows-10

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

Mik*_*der 5

您可以使用 ctypes 中的 windll 使您的程序具有 DPI 感知能力:

from ctypes import windll

# Make program aware of DPI scaling
user32 = windll.user32
user32.SetProcessDPIAware()
Run Code Online (Sandbox Code Playgroud)

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