我在用Python制作“夜之光”的应用程序。我使用windows来使用Gamma斜坡来完成我的任务。我使用来自EnumDisplayDevicesW用户32.dll的来获取连接到我的PC上的信息和显示器的数量。
我只有一个显示器连接到我的桌面,但输出提供了两个显示器的信息。
这是我的密码。我正在使用Python,并通过ctypes模块访问WinAPI。
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'
while True:
INFO = DISPLAY_DEVICEW() # struct object
INFO.cb = ctypes.sizeof(INFO) # setting 'cnSize' prior to calling 'EnumDisplayDevicesW'
if not EnumDisplayDevices(None, i, ctypes.byref(INFO), 0):
break # break as soon as False is returned by 'EnumDisplayDevices'
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()
代码返回的输出如下:
DeviceName: \\.\DISPLAY1
DeviceString: Intel(R) HD Graphics 510
StateFlags: 5
DeviceID: PCI\VEN_8086&DEV_1902&SUBSYS_D0001458&REV_06
DeviceKey: \Registry\Machine\System\CurrentControlSet\Control\Video\{C31A4E45-2A30-11EB-953B-92862920CE33}\0000
DeviceName: \\.\DISPLAY2
DeviceString: Intel(R) HD Graphics 510
StateFlags: 0
DeviceID: PCI\VEN_8086&DEV_1902&SUBSYS_D0001458&REV_06
DeviceKey: \Registry\Machine\System\CurrentControlSet\Control\Video\{C31A4E45-2A30-11EB-953B-92862920CE33}\0001
据我所知,第一个,即\\.\DISPLAY1
是我的,但为什么需要第二个?
我拥有一台带有标准三星显示器的桌面电脑。
任何帮助都会很有帮助。提前感谢!
发布于 2021-01-09 07:58:54
因此,在进行了所需的更改之后,下面是将所有显示监视器连接到所有显示适配器的最终代码。
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
"""
the following list 'displays', stores display adapter info in the following Structure:
'List containing Tuple of displayAdapterInfo and list of monitorInfo controlled by the adapter'
[
(dispAdapterInfo1, [Monitor1, Monitor2, . . . ]),
(dispAdapterInfo2, [Monitor1, Monitor2, . . . ]),
. . . .
]
Number of dispAdapter depends on the graphics driver, and number of Monitors per adapter depends on
number of monitors connected and controlled by adapter.
"""
displays = []
i = 0 # iteration variable for 'iDevNum'
while True:
DISP_INFO = DISPLAY_DEVICEW() # struct object for adapter info
DISP_INFO.cb = ctypes.sizeof(DISP_INFO) # setting 'cb' prior to calling 'EnumDisplayDevicesW'
if not EnumDisplayDevices(None, i, ctypes.byref(DISP_INFO), 0):
break # break as soon as False is returned by 'EnumDisplayDevices'
monitors = [] # stores list of monitors per adapter
j = 0
while True:
MONITR_INFO = DISPLAY_DEVICEW() # struct object for Monitor info
MONITR_INFO.cb = ctypes.sizeof(MONITR_INFO) # setting 'cb' prior to calling 'EnumDisplayDevicesW'
if not EnumDisplayDevices(DISP_INFO.DeviceName, j, ctypes.byref(MONITR_INFO), 0):
break # break as soon as False is returned by 'EnumDisplayDevices'
monitors.append(MONITR_INFO)
j += 1
displays.append((DISP_INFO, monitors)) # add the tuple (dispAdapterInfo, [MonitorsInfo])
i += 1
for display in displays:
if display[1]: # filter out the adapter with no monitor connected, i.e, empty list
print("Adapter object:", display[0])
print("List of Monitor objects :", display[1])
print()
现在,display是适配器对象,可以通过引用上面的DISPLAY_DEVICEW
类来获取信息,而display1是监视器的列表,它可以被迭代以获得监视器信息的对象,并通过引用DISPLAY_DEVICEW
类获取有关它的信息。
https://stackoverflow.com/questions/65624377
复制相似问题