<blockquote>
<p>I have only one monitor connected to my desktop, but the output is giving information of two monitors.</p>
</blockquote>
<p>运行此代码不会告诉您有两个“监视器”,而是两个“适配器”</p>
<p>根据<a href="https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enumdisplaydevicesw" rel="nofollow noreferrer">^{<cd1>}</a>:</p>
<blockquote>
<p>To get information on the display adapter, call EnumDisplayDevices with lpDevice set to NULL. For example, DISPLAY_DEVICE.DeviceString contains the adapter name.</p>
<p>To obtain information on a display monitor, first call EnumDisplayDevices with lpDevice set to NULL. Then call EnumDisplayDevices with lpDevice set to DISPLAY_DEVICE.DeviceName from the first call to EnumDisplayDevices and with iDevNum set to zero. Then DISPLAY_DEVICE.DeviceString is the monitor name.</p>
</blockquote>
<p>因此,如果您需要获取监视器信息,您需要拨打:</p>
<pre><code>EnumDisplayDevices(INFO.DeviceName,j,ctypes.byref(Monitor_INFO),0):
</code></pre>
<p>以下是一个示例:</p>
<pre><code>import ctypes
from ctypes import wintypes
class DISPLAY_DEVICEW(ctypes.Structure):
_fields_ = [
('cb', wintypes.DWORD),
('DeviceName', wintypes.WCHAR * 32),
('DeviceString', wintypes.WCHAR * 128),
('StateFlags', wintypes.DWORD),
('DeviceID', wintypes.WCHAR * 128),
('DeviceKey', wintypes.WCHAR * 128)
]
if __name__ == '__main__':
EnumDisplayDevices = ctypes.windll.user32.EnumDisplayDevicesW # get the function address
EnumDisplayDevices.restype = ctypes.c_bool # set return type to BOOL
displays = [] # to store display information
i = 0 # iteration variable for 'iDevNum'
j = 0
while True:
INFO = DISPLAY_DEVICEW() # struct object
INFO.cb = ctypes.sizeof(INFO) # setting 'cnSize' prior to calling 'EnumDisplayDevicesW'
Monitor_INFO = DISPLAY_DEVICEW()
Monitor_INFO.cb = ctypes.sizeof(Monitor_INFO)
if not EnumDisplayDevices(None, i, ctypes.byref(INFO), 0):
break # break as soon as False is returned by 'EnumDisplayDevices'
#j = 0
while EnumDisplayDevices(INFO.DeviceName,j,ctypes.byref(Monitor_INFO),0):
print("monitor name:\t\t",Monitor_INFO.DeviceName,'\n\n')
j+=1
displays.append(INFO) # append information to the list
i += 1
# display information in a sequential form
for x in displays:
print('DeviceName:\t\t', x.DeviceName)
print("DeviceString:\t", x.DeviceString)
print("StateFlags:\t\t", x.StateFlags)
print("DeviceID:\t\t", x.DeviceID)
print("DeviceKey:\t\t", x.DeviceKey)
print(), print()
</code></pre>