我需要检测物理连接到计算机的显示器的数量(以确定屏幕配置是单一模式、扩展模式还是重复模式)。 System.Windows.Forms.Screen.AllScreens.Length
和 System.Windows.Forms.SystemInformation.MonitorCount
都返回虚拟屏幕(桌面)的计数。
因此,如果有 2 个以上的显示器连接到 PC,我可以使用此值在复制/扩展模式之间做出决定,但我无法决定是否只有一个物理显示器连接到 PC,因此屏幕配置是在单屏模式下。
最佳答案
尝试以下操作:
System.Management.ManagementObjectSearcher monitorObjectSearch = new System.Management.ManagementObjectSearcher("SELECT * FROM Win32_DesktopMonitor");
int Counter = monitorObjectSearch.Get().Count;
找到以下问题的答案:
WMI Get All Monitors Not Returning All Monitors
更新
尝试以下功能,它检测到拔下显示器:
private int GetActiveMonitors()
{
int Counter = 0;
System.Management.ManagementObjectSearcher monitorObjectSearch = new System.Management.ManagementObjectSearcher("SELECT * FROM Win32_DesktopMonitor");
foreach (ManagementObject Monitor in monitorObjectSearch.Get())
{
UInt16 Status = 0;
try
{
Status = (UInt16)Monitor["Availability"];
}
catch (Exception ex)
{
//Error handling if you want to
continue;
}
if (Status == 3)
Counter++;
}
return Counter;
}
以下是状态列表:https://msdn.microsoft.com/en-us/library/aa394122%28v=vs.85%29.aspx
也许您还需要增加其他状态码的计数器。查看链接以获取更多信息。
关于c# - 如何在 C# 中获取连接到 PC 的显示器数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31043829/