/// <summary> /// Loops through all connected monitors and caches their display information /// into an array /// </summary> /// <returns>Display array</returns> public static Display[] GetDisplays() { List <Display> displays = new List <Display>(); MonitorEnumProc callback = (IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, int d) => { MonitorInfoEx info = new MonitorInfoEx(); info.Size = Marshal.SizeOf(info); if (User32Interop.GetMonitorInfo(hMonitor, ref info)) { Rectangle r = lprcMonitor.ToRectangle(); Display display = new Display(hMonitor, r, info.DeviceName, true); displays.Add(display); } return(true); }; bool result = User32Interop.EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, callback, 0); if (result) { return(displays.ToArray()); } return(null); }
/// <summary> /// Loops through all connected monitors and caches their display information /// into an array /// </summary> /// <returns>Display array</returns> public static Display[] GetDisplays() { List <Display> displays = new List <Display>(); MonitorEnumProc callback = (IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, int d) => { MonitorInfoEx info = new MonitorInfoEx(); info.Size = Marshal.SizeOf(info); if (User32Interop.GetMonitorInfo(hMonitor, ref info)) { Rectangle r = lprcMonitor.ToRectangle(); DISPLAY_DEVICE displayDevice = new DISPLAY_DEVICE(); displayDevice.cb = Marshal.SizeOf(displayDevice); const int EDD_GET_DEVICE_INTERFACE_NAME = 0x1; uint deviceIndex = 0; string deviceID = string.Empty; string deviceString = string.Empty; string monitorID = string.Empty; int displayIndex = -1; int monitorIndex = -1; while (EnumDisplayDevices(null, deviceIndex, ref displayDevice, 0)) { if (info.DeviceName == displayDevice.DeviceName) { DISPLAY_DEVICE monDisplayDevice = new DISPLAY_DEVICE(); monDisplayDevice.cb = Marshal.SizeOf(monDisplayDevice); uint monitorNum = 0; EnumDisplayDevices(displayDevice.DeviceName, monitorNum, ref monDisplayDevice, EDD_GET_DEVICE_INTERFACE_NAME); try { deviceID = monDisplayDevice.DeviceID; deviceString = monDisplayDevice.DeviceString; monitorID = deviceID.Substring(deviceID.IndexOf("DISPLAY#") + 8, 7); Int32.TryParse(info.DeviceName.Substring(info.DeviceName.IndexOf("DISPLAY") + 7), out displayIndex); Int32.TryParse( monDisplayDevice.DeviceName.Substring( monDisplayDevice.DeviceName.IndexOf("Monitor") + 7), out monitorIndex); monitorNum++; break; } catch (Exception) { } } deviceIndex++; } Display display = new Display(hMonitor, r, info.DeviceName, true, deviceID, deviceString, monitorID, displayIndex, monitorIndex); displays.Add(display); } return(true); }; //deviceIndex = 0; //while() //{ // LogManager.Log(displayDevice.DeviceID); // deviceIndex++; //} bool result = User32Interop.EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, callback, 0); if (result) { return(displays.ToArray()); } return(null); }