How to get the display names of multiple monitors with the Win32 API?

a 夏天 提交于 2019-12-01 03:23:37

问题


I have two monitors connected to my Windows PC -- one is a normal monitor and the other is a projector. Because Windows doesn't consistently assign one or the other as the primary monitor (in part because they aren't always both on when Windows boots), I need to programmatically detect which monitor is which.

The Control Panel shows the names of the monitors as "HP 2159" (the normal monitor) and "PROJECTOR" in the screen where you choose which is the primary monitor. That's the information I want to get in my program.

I can't find the right Win32 API function for this info. I've tried both EnumDisplayDevices and EnumDisplayMontiors. Both just give "DISPLAY1" and "DISPLAY2" as devices names. What should I be using to get the "HP 2159" and "PROJECTOR" info or something analogous?

UPDATE: Here's the Python code I'm using:

>>> import win32api
>>> monitors = win32api.EnumDisplayMonitors()
>>> win32api.GetMonitorInfo(monitors[0][0])
{'Device': '\\\\.\\DISPLAY1', 'Work': (0, 0, 1920, 1080), 'Flags': 1, 'Monitor': (0, 0, 1920, 1080)}
>>> win32api.GetMonitorInfo(monitors[1][0])
{'Device': '\\\\.\\DISPLAY2', 'Work': (1920, 0, 3360, 1080), 'Flags': 0, 'Monitor': (1920, 0, 3360, 1080)}

回答1:


The EnumDisplayMonitors passes a monitor handle to the MonitorEnumProc callback function. You can pass that handle to GetMonitorInfo, being sure to pass a pointer to a MonitorInfoEx structure and setting the cbSize member accordingly.

Upon return, the szDevice field in the MonitorInfoEx structure will contain the name of the monitor.



来源:https://stackoverflow.com/questions/22724802/how-to-get-the-display-names-of-multiple-monitors-with-the-win32-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!