<i id='0rByK'><tr id='0rByK'><dt id='0rByK'><q id='0rByK'><span id='0rByK'><b id='0rByK'><form id='0rByK'><ins id='0rByK'></ins><ul id='0rByK'></ul><sub id='0rByK'></sub></form><legend id='0rByK'></legend><bdo id='0rByK'><pre id='0rByK'><center id='0rByK'></center></pre></bdo></b><th id='0rByK'></th></span></q></dt></tr></i><div id='0rByK'><tfoot id='0rByK'></tfoot><dl id='0rByK'><fieldset id='0rByK'></fieldset></dl></div>
    1. <tfoot id='0rByK'></tfoot>

      <small id='0rByK'></small><noframes id='0rByK'>

      <legend id='0rByK'><style id='0rByK'><dir id='0rByK'><q id='0rByK'></q></dir></style></legend>
      • <bdo id='0rByK'></bdo><ul id='0rByK'></ul>

    2. Screen.AllScreen 没有给出正确的监视器计数

      时间:2023-10-24
      • <legend id='3bqqD'><style id='3bqqD'><dir id='3bqqD'><q id='3bqqD'></q></dir></style></legend>

        • <bdo id='3bqqD'></bdo><ul id='3bqqD'></ul>
          <i id='3bqqD'><tr id='3bqqD'><dt id='3bqqD'><q id='3bqqD'><span id='3bqqD'><b id='3bqqD'><form id='3bqqD'><ins id='3bqqD'></ins><ul id='3bqqD'></ul><sub id='3bqqD'></sub></form><legend id='3bqqD'></legend><bdo id='3bqqD'><pre id='3bqqD'><center id='3bqqD'></center></pre></bdo></b><th id='3bqqD'></th></span></q></dt></tr></i><div id='3bqqD'><tfoot id='3bqqD'></tfoot><dl id='3bqqD'><fieldset id='3bqqD'></fieldset></dl></div>

          <small id='3bqqD'></small><noframes id='3bqqD'>

            <tbody id='3bqqD'></tbody>
              <tfoot id='3bqqD'></tfoot>

                本文介绍了Screen.AllScreen 没有给出正确的监视器计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我在我的程序中做这样的事情:

                I am doing something like this in my program:

                Int32 currentMonitorCount = Screen.AllScreens.Length;
                
                if  (currentMonitorCount < 2)
                {
                   //Put app in single screen mode.
                }
                else
                {
                   //Put app in dual screen mode.
                }
                

                我的应用程序识别当前连接了多少显示器是非常重要的.

                It is VERY important my application recognizes how many monitors are currently connected.

                但是,在我插入/拔出显示器几次后,Screen.AllScreens.Length 总是返回2".

                However, after I plug/unplug the monitor a couple of times, Screen.AllScreens.Length always returns '2'.

                我的显示器知道它没有连接(它已进入省电"模式),并且控制面板知道它没有连接(它只显示一个显示器).

                My monitor knows it's not connected (it has entered 'power save' mode), and the control panel knows that it's not connected (it shows only one monitor).

                那么我错过了什么?如何确定只有一台显示器?

                So what am I missing? How do I figure out that there's only one monitor?

                推荐答案

                我查看了源代码(请记住,我们可以使用 MS Symbol 服务器做到这一点).AllScreens 使用非托管 API 在第一次访问时获取屏幕,然后将结果存储在静态变量中以供以后使用.

                I had a look at the source (remember we can do that using the MS Symbol servers). AllScreens uses an unmanaged API to get the screens on the first access, then stores the result in a static variable for later use.

                这样做的结果是,如果程序运行时监视器的数量发生了变化;那么 Screen.AllScreens 将不会接受更改.

                The consequence of this, is that if the number of monitors changes while your program is running; then Screen.AllScreens will not pick up the change.

                解决此问题的最简单方法可能是调用 非托管 API 直接.(或者你可能是邪恶的,并在询问之前使用反射将静态 screens 字段设置为 null.不要那样做).

                The easiest way to get around this would probably be to call the unmanaged API directly. (Or you could be evil, and use reflection to set the static screens field to null before asking. Don't do that).

                如果您只需要知道计数,请在执行 P/Invoke 路由之前检查您是否可以使用 System.Windows.Forms.SystemInformation.MonitorCount(如评论中所建议的那样).这会直接调用 GetSystemMetrics,它可能已正确更新.

                If you just need to know the count, check whether you can use System.Windows.Forms.SystemInformation.MonitorCount (as suggested in the comments) before going the P/Invoke route. This calls GetSystemMetrics directly, and it is probably correctly updated.

                如果您发现需要使用 P/Invoke 来执行此操作,这里有一个完整示例,演示了 C# 中非托管 API 的用法:

                If you find you need to do it using P/Invoke, here is a complete example that demonstrates the usage of the unmanaged API from C#:

                using System;
                using System.Runtime.InteropServices;
                
                class Program
                {
                    public static void Main()
                    {
                        int monCount = 0;
                        Rect r = new Rect();
                        MonitorEnumProc callback = (IntPtr hDesktop, IntPtr hdc, ref Rect 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");
                
                    }
                    [DllImport("user32")]
                    private static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lpRect, MonitorEnumProc callback, int dwData);
                
                    private delegate bool MonitorEnumProc(IntPtr hDesktop, IntPtr hdc, ref Rect pRect, int dwData);
                
                    [StructLayout(LayoutKind.Sequential)]
                    private struct Rect
                    {
                        public int left;
                        public int top;
                        public int right;
                        public int bottom;
                    }
                }
                

                这篇关于Screen.AllScreen 没有给出正确的监视器计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何创建一个有边框但没有标题栏的表单?(如 Windows 7 上的音量控制) 下一篇:判断windows当前是否正在播放声音

                相关文章

                <tfoot id='kWHkV'></tfoot>
              1. <small id='kWHkV'></small><noframes id='kWHkV'>

                  <bdo id='kWHkV'></bdo><ul id='kWHkV'></ul>

                  1. <legend id='kWHkV'><style id='kWHkV'><dir id='kWHkV'><q id='kWHkV'></q></dir></style></legend>
                    <i id='kWHkV'><tr id='kWHkV'><dt id='kWHkV'><q id='kWHkV'><span id='kWHkV'><b id='kWHkV'><form id='kWHkV'><ins id='kWHkV'></ins><ul id='kWHkV'></ul><sub id='kWHkV'></sub></form><legend id='kWHkV'></legend><bdo id='kWHkV'><pre id='kWHkV'><center id='kWHkV'></center></pre></bdo></b><th id='kWHkV'></th></span></q></dt></tr></i><div id='kWHkV'><tfoot id='kWHkV'></tfoot><dl id='kWHkV'><fieldset id='kWHkV'></fieldset></dl></div>