c# - 显示计数(包括禁用的显示器)

标签 c#

大家好 stackoverflow.com 的新手。不确定这里会如何提问,但会尽力而为。已完成,对主题进行了相当多的研究,但找不到任何解决检测的方法。

示例案例: 尝试确定 Windows 7-10 系统中事件和禁用显示器的数量。

代码 GPU[可用性]:

        private int MonCount;
    [DllImport("User32.dll")]
    private static extern bool EnumDisplayDevices(
        string lpDevice, int iDevNum,
        ref DISPLAY_DEVICE lpDisplayDevice, int dwFlags);

    [StructLayout(LayoutKind.Sequential)]
    public struct DISPLAY_DEVICE
    {
        public int cb;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string DeviceName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        public string DeviceString;
        public int StateFlags;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        public string DeviceID;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        public string DeviceKey;

        public DISPLAY_DEVICE(int flags)
        {
            cb = 0;
            StateFlags = flags;
            DeviceName = new string((char)32, 32);
            DeviceString = new string((char)32, 128);
            DeviceID = new string((char)32, 128);
            DeviceKey = new string((char)32, 128);
            cb = Marshal.SizeOf(this);
        }
    }
    public void MonitorCheck()
    {
        DISPLAY_DEVICE lpDisplayDevice = new DISPLAY_DEVICE(0);     // OUT
        DISPLAY_DEVICE monitor_name = new DISPLAY_DEVICE(0);        // OUT

        int devNum = 0;
        while (EnumDisplayDevices(null, devNum, ref lpDisplayDevice, 0))
        {

            listBox1.Items.Add("\ndevNum =" + devNum);
            listBox1.Items.Add("cb =" + lpDisplayDevice.cb);
            listBox1.Items.Add("DeviceID =" + lpDisplayDevice.DeviceID);
            listBox1.Items.Add("DeviceKey =" + lpDisplayDevice.DeviceKey);
            listBox1.Items.Add("DeviceName =" + lpDisplayDevice.DeviceName.Trim());
            listBox1.Items.Add("DeviceString =" + lpDisplayDevice.DeviceString.Trim());
            // Show monitor name:
            EnumDisplayDevices(lpDisplayDevice.DeviceName, 0, ref monitor_name, 0);
            listBox1.Items.Add("Monitor name =" + monitor_name.DeviceString.Trim());
            ++devNum;
        }
    }

来源:C# how to get the Windows monitor name

返回:图形适配器信息提供的所有可连接显示器。计数始终是显示适配器中可以连接多少台显示器。

代码事件显示:

Screen.AllScreens.Count();

返回:事件监视器计数。

问题:

如果 GPU 适配器有 3 个输出: * 以上 GPU[Availability] 代码返回:3 (无论有多少显示器连接到它。)

如果连接了 2 个显示器(例如:笔记本电脑显示器 + 外部 HDMI 显示器),但显示设置设置为:仅笔记本电脑显示器(外部 HDMI 显示器未激活)-> 事件显示代码返回:1

问题:

由于连接了非事件显示器和事件显示器(笔记本电脑显示器 + 非事件外接显示器),我该如何返回 2?

解决方案:

解决这个问题的想法很少:

  1. 通过扩展它们来激活所有显示并简单地重做:Screen.AllScreens.Count();

想法: 比较 GPU[Availability] 输出的输出并计算所有给出任何值为 monitor_name.DeviceString.Trim() 的输出(但是,我已经在具有 NVIDIA 适配器的桌面上对此进行了测试:结果为空,但在笔记本电脑 Intel HD 适配器:输出始终是“Generic PnP-Monitor”,因此,这不是解决方案)。

最佳答案

愚蠢的我...从没想过使用所有 3 种方法来解决问题。 (似乎很难让代码在论坛的代码标签中工作。

  • 可连接的显示器数量上限
  • 当前连接的显示器
  • 当前活跃的展示

解决方案:

嗯,似乎无法将我的代码附加到 stackoverflow 回复中,所以:

Pastebin: Monitors counting

关于c# - 显示计数(包括禁用的显示器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37501512/

相关文章:

c# - 如何观察由于 C# 中另一个等待任务失败而未等待的任务?

c# - Windows Server Task Scheduler 需要将文件放在 System32 文件夹中吗?

c# - 默认发起成员(member)的结果是什么! C# .net 中的语句?

c# - 将 Linq 查询返回到 KeyValuePair 的列表中

c# - 如果队列未锁定,多线程的性能与全局队列的长度有关?

c# - 移除 Storyboard,使其动画回到原来的状态

c# - "System.InvalidCastException: Specified cast is not valid"- 日期时间

c# - 事件限制/排队 - react 性扩展?

c# - 创建一个由字符串命名的成员

c# - 为什么 var 被解析为 Double 而不是 Long?