Member 8099285 Ответов: 2

Проблема с экраном AllScreens


Здравствуйте, у меня возникла эта проблема, у меня есть некоторый код, который должен работать в службе windows, чтобы подсчитать количество экранов, подключенных к ПК, я использую экран.AllScreens для получения количества экранов, во всяком случае, он работает просто отлично, если я вызываю код из консольного приложения, но когда я использую тот же код в winservice, монитор подсчитывается на экране.AllScreens всегда 1, без нервов есть два монитора, подключенных друг к другу.
Ты хоть представляешь, что здесь происходит?

2 Ответов

Рейтинг:
2

Member 11019682

string mesEnabledMoni = "";
int EnabledMoniId= 1;
foreach (Screen screen in Screen.AllScreens)
{
	string str = String.Format("Monitor {0}: {1} x {2} @ {3},{4}\n", EnabledMoniId , screen.Bounds.Width,
		screen.Bounds.Height, screen.Bounds.X, screen.Bounds.Y);
	mesEnabledMoni += str;
	EnabledMoniId ++;
}

MessageBox.Show(mesEnabledMoni, "EnumDisp");


Система.Окна.Формы.Класс экрана для получения информации обо всех включенных мониторах


Dave Kreskowiak

На этот вопрос был дан ответ семь лет назад.

Вы должны вернуться и перечитать вопрос. Этот код будет отлично работать в обычном консольном приложении или Windows Forms.

ОН НЕ БУДЕТ РАБОТАТЬ В ПРИЛОЖЕНИИ-СЛУЖБЕ. Причина в том, что службы работают под другим рабочим столом. Этот рабочий стол не имеет ничего общего с количеством подключенных мониторов.

Рейтинг:
0

Gordon Beeming (RSA)

попробуйте использовать вызов PInvoke[^] где обычные методы фреймворка подводят вас,

пример этого приведен ниже с использованием кода из http://g4ac.co.za/zp3Dz[^]

[DllImport("user32.dll")]
        static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip,
           MonitorEnumDelegate lpfnEnum, IntPtr dwData);
        delegate bool MonitorEnumDelegate(IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData);
        [StructLayout(LayoutKind.Sequential)]
        public struct Rect
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }
        /// <summary>
        /// The struct that contains the display information
        /// </summary>
        public class DisplayInfo
        {
            public string Availability { get; set; }
            public string ScreenHeight { get; set; }
            public string ScreenWidth { get; set; }
            public Rect MonitorArea { get; set; }
            public Rect WorkArea { get; set; }
        }
        /// <summary>
        /// Collection of display information
        /// </summary>
        public class DisplayInfoCollection : List<DisplayInfo>
        {
        }
        [DllImport("user32.dll")]
        static extern bool GetMonitorInfo(IntPtr hMonitor, ref MonitorInfoEx lpmi);
        [DllImport("user32.dll")]
        static extern bool GetMonitorInfo(IntPtr hMonitor, ref MonitorInfo lpmi);
        // size of a device name string
        private const int CCHDEVICENAME = 32;
        /// <summary>
        /// The MONITORINFOEX structure contains information about a display monitor.
        /// The GetMonitorInfo function stores information into a MONITORINFOEX structure or a MONITORINFO structure.
        /// The MONITORINFOEX structure is a superset of the MONITORINFO structure. The MONITORINFOEX structure adds a string member to contain a name
        /// for the display monitor.
        /// </summary>
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        internal struct MonitorInfoEx
        {
            /// <summary>
            /// The size, in bytes, of the structure. Set this member to sizeof(MONITORINFOEX) (72) before calling the GetMonitorInfo function.
            /// Doing so lets the function determine the type of structure you are passing to it.
            /// </summary>
            public int Size;
            /// <summary>
            /// A RECT structure that specifies the display monitor rectangle, expressed in virtual-screen coordinates.
            /// Note that if the monitor is not the primary display monitor, some of the rectangle's coordinates may be negative values.
            /// </summary>
            public Rect Monitor;
            /// <summary>
            /// A RECT structure that specifies the work area rectangle of the display monitor that can be used by applications,
            /// expressed in virtual-screen coordinates. Windows uses this rectangle to maximize an application on the monitor.
            /// The rest of the area in rcMonitor contains system windows such as the task bar and side bars.
            /// Note that if the monitor is not the primary display monitor, some of the rectangle's coordinates may be negative values.
            /// </summary>
            public Rect WorkArea;
            /// <summary>
            /// The attributes of the display monitor.
            ///
            /// This member can be the following value:
            ///   1 : MONITORINFOF_PRIMARY
            /// </summary>
            public uint Flags;
            /// <summary>
            /// A string that specifies the device name of the monitor being used. Most applications have no use for a display monitor name,
            /// and so can save some bytes by using a MONITORINFO structure.
            /// </summary>
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
            public string DeviceName;
            public void Init()
            {
                this.Size = 72;
                this.DeviceName = string.Empty;
            }
        }
        [StructLayout(LayoutKind.Sequential)]
        internal struct MonitorInfo
        {
            /// <summary>
            /// The size, in bytes, of the structure. Set this member to sizeof(MONITORINFO) (40) before calling the GetMonitorInfo function.
            /// Doing so lets the function determine the type of structure you are passing to it.
            /// </summary>
            public int Size;
            /// <summary>
            /// A RECT structure that specifies the display monitor rectangle, expressed in virtual-screen coordinates.
            /// Note that if the monitor is not the primary display monitor, some of the rectangle's coordinates may be negative values.
            /// </summary>
            public Rect Monitor;
            /// <summary>
            /// A RECT structure that specifies the work area rectangle of the display monitor that can be used by applications,
            /// expressed in virtual-screen coordinates. Windows uses this rectangle to maximize an application on the monitor.
            /// The rest of the area in rcMonitor contains system windows such as the task bar and side bars.
            /// Note that if the monitor is not the primary display monitor, some of the rectangle's coordinates may be negative values.
            /// </summary>
            public Rect WorkArea;
            /// <summary>
            /// The attributes of the display monitor.
            ///
            /// This member can be the following value:
            ///   1 : MONITORINFOF_PRIMARY
            /// </summary>
            public uint Flags;
            public void Init()
            {
                this.Size = 40;
            }
        }
        /// <summary>
        /// Returns the number of Displays using the Win32 functions
        /// </summary>
        /// <returns>collection of Display Info</returns>
        public DisplayInfoCollection GetDisplays()
        {
            DisplayInfoCollection col = new DisplayInfoCollection();
            EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero,
                delegate(IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData)
                {
                    MonitorInfo mi = new MonitorInfo();
                    mi.Size = (int)Marshal.SizeOf(mi);
                    bool success = GetMonitorInfo(hMonitor, ref mi);
                    if (success)
                    {
                        DisplayInfo di = new DisplayInfo();
                        di.ScreenWidth = (mi.Monitor.right - mi.Monitor.left).ToString();
                        di.ScreenHeight = (mi.Monitor.bottom - mi.Monitor.top).ToString();
                        di.MonitorArea = mi.Monitor;
                        di.WorkArea = mi.WorkArea;
                        di.Availability = mi.Flags.ToString();
                        col.Add(di);
                    }
                    return true;
                }, IntPtr.Zero);
            return col;
        }
        public int GetTotalMonitors()
        {
            return GetDisplays().Count;
        }



Ваш метод получения итоговой суммы-это GetTotalMonitors()