[英]Query Windows 10/8 Monitor Scaling from Python Script?
I have written an application in python 3.6 and would like to run a command to see what the current scaling for a monitor is in Windows 10 or 8 - something like how the following returns screen resolution:我在 python 3.6 中编写了一个应用程序,并想运行一个命令来查看 Windows 10 或 8 中监视器的当前缩放比例 - 类似于以下返回屏幕分辨率的方式:
user32 = ctypes.windll.user32
screensize_l = user32.GetSystemMetrics(0)
screensize_w = user32.GetSystemMetrics(1)
I understand that the easiest way to do this may be to make my application DPI aware, but doing so causes lots of additional problems in my application - so I would like to avoid this option.我知道最简单的方法可能是让我的应用程序知道 DPI,但这样做会在我的应用程序中导致许多额外的问题 - 所以我想避免这个选项。
I have looked in the windows documentation and thought "GetDpiForMonitor" or "GetScaleFactorForMonitor" may be what I am looking for, but don't know how to implement these commands.我查看了 windows 文档并认为“GetDpiForMonitor”或“GetScaleFactorForMonitor”可能是我正在寻找的,但不知道如何实现这些命令。
I already use both win32api and ctypes so anything relying on either of these would be fine - any help would be greatly appreciated!我已经同时使用了 win32api 和 ctypes,所以任何依赖于其中任何一个的都可以 - 任何帮助将不胜感激!
On Windows 10, the following code (you need pywin32
):在 Windows 10 上,以下代码(您需要pywin32
):
import ctypes
import win32api
PROCESS_PER_MONITOR_DPI_AWARE = 2
MDT_EFFECTIVE_DPI = 0
def print_dpi():
shcore = ctypes.windll.shcore
monitors = win32api.EnumDisplayMonitors()
hresult = shcore.SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE)
assert hresult == 0
dpiX = ctypes.c_uint()
dpiY = ctypes.c_uint()
for i, monitor in enumerate(monitors):
shcore.GetDpiForMonitor(
monitor[0].handle,
MDT_EFFECTIVE_DPI,
ctypes.byref(dpiX),
ctypes.byref(dpiY)
)
print(
f"Monitor {i} (hmonitor: {monitor[0]}) = dpiX: {dpiX.value}, dpiY: {dpiY.value}"
)
if __name__ == "__main__":
print_dpi()
gives the following output on my machine with 3 monitors and 1 scaled to 125%:在我的机器上提供以下输出,其中有 3 台显示器,其中 1 台缩放至 125%:
Monitor 0 (hmonitor: <PyHANDLE:65539>) = dpiX: 96, dpiY: 96
Monitor 1 (hmonitor: <PyHANDLE:65537>) = dpiX: 120, dpiY: 120
Monitor 2 (hmonitor: <PyHANDLE:65541>) = dpiX: 96, dpiY: 96
If you do not want to set your own application as a dpi-aware per monitor, you could try launching some variant of the above code in another process using the multiprocessing
module and retrieve the results.如果您不想将自己的应用程序设置为每个监视器的 dpi 感知,您可以尝试使用multiprocessing
模块在另一个进程中启动上述代码的某些变体并检索结果。
hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.