我需要检测物理连接到计算机的显示器数量(以确定屏幕配置是单一、扩展还是重复模式)。
两者都有
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;
以下问题的答案:
更新
尝试以下功能,它会检测到拔出显示器:
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
也许您还需要增加其他状态代码的计数器。检查链接以获取更多信息。
根据 Xanatos 的答案,我创建了简单的帮助程序类来检测屏幕配置:
using System;
using System.Management;
using System.Windows.Forms;
public static class ScreensConfigurationDetector
{
public static ScreensConfiguration GetConfiguration()
{
int physicalMonitors = GetActiveMonitors();
int virtualMonitors = Screen.AllScreens.Length;
if (physicalMonitors == 1)
{
return ScreensConfiguration.Single;
}
return physicalMonitors == virtualMonitors
? ScreensConfiguration.Extended
: ScreensConfiguration.DuplicateOrShowOnlyOne;
}
private static int GetActiveMonitors()
{
int counter = 0;
ManagementObjectSearcher monitorObjectSearch = new ManagementObjectSearcher("SELECT * FROM Win32_DesktopMonitor");
foreach (ManagementObject Monitor in monitorObjectSearch.Get())
{
try
{
if ((UInt16)Monitor["Availability"] == 3)
{
counter++;
}
}
catch (Exception)
{
continue;
}
}
return counter;
}
}
public enum ScreensConfiguration
{
Single,
Extended,
DuplicateOrShowOnlyOne
}
“SELECT * FROM Win32_DesktopMonitor”查询在 Vista 后可能无法正常工作。我花了几个小时寻找解决方案,但没有一个能够在 Windows 7 及更高版本上正确处理此问题。基于多米尼克的回答;
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_PnPEntity where service=\"monitor\"");
int numberOfMonitors = searcher.Get().Count;
将返回物理监视器的数量。通过这个改变,他的助手类也为我工作了。
我发现在 WPF 中获取监视器计数的最可靠方法是监听 WM_DISPLAYCHANGE 以了解 mionitors 何时连接和断开连接,然后使用 EnumDisplayMonitors 获取监视器计数。这是代码。
挂钩 WndProc 以获取 WM_DISPLAYCHANGE 消息。
public partial class MainWindow : IDisposable
{
...
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
source.AddHook(WndProc);
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_DISPLAYCHANGE)
{
int lparamInt = lParam.ToInt32();
uint width = (uint)(lparamInt & 0xffff);
uint height = (uint)(lparamInt >> 16);
int monCount = ScreenInformation.GetMonitorCount();
int winFormsMonCount = System.Windows.Forms.Screen.AllScreens.Length;
_viewModel.MonitorCountChanged(monCount);
}
return IntPtr.Zero;
}
获取显示计数
public class ScreenInformation
{
[StructLayout(LayoutKind.Sequential)]
private struct ScreenRect
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32")]
private static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lpRect, MonitorEnumProc callback, int dwData);
private delegate bool MonitorEnumProc(IntPtr hDesktop, IntPtr hdc, ref ScreenRect pRect, int dwData);
public static int GetMonitorCount()
{
int monCount = 0;
MonitorEnumProc callback = (IntPtr hDesktop, IntPtr hdc, ref ScreenRect prect, int d) => ++monCount > 0;
if (EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, callback, 0))
Console.WriteLine("You have {0} monitors", monCount);
else
Console.WriteLine("An error occured while enumerating monitors");
return monCount;
}
}
也许有点晚了,但这是我使用的:
[DllImport("User32.dll", EntryPoint = "GetSystemMetrics")]
internal extern static int fGetSystemMetrics(int metric);
const int SM_CMONITORS = 80;
fGetSystemMetrics(SM_CMONITORS);
编辑:请参阅https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getsystemmetrics