c# - 如何在 C# 中获取连接到 PC 的显示器数量?

标签 c# wpf screen

我需要检测物理连接到计算机的显示器的数量(以确定屏幕配置是单一模式、扩展模式还是重复模式)。 System.Windows.Forms.Screen.AllScreens.LengthSystem.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/

相关文章:

c# - 确定变量使用的字节数

python - Tkinter 窗口位于右下角

c# - 创建自定义控件的最佳 WPF 面板

android - 支持 1.5 目标屏幕,无法使应用程序不缩放

安卓:Display.getRotation();

c# - 写入第三方 API

c# - Linq 对象引用未设置为对象的实例 - 内部集合为空

c# - 如何在 Visual Studio 2015 中更改语言版本

wpf - 我应该在模型层引用 MVVM 框架吗?

c# - 如何使用 WPF 中的按钮/代码隐藏实例同步或访问 2 个 UserControl?