public static IList <MonitorInfo> GetMonitorList() { List <MonitorInfo> col = new List <MonitorInfo>(); NativeMethods.MonitorEnumProc testDelegate = new NativeMethods.MonitorEnumProc( delegate(IntPtr hMonitor, IntPtr hdcMonitor, ref NativeMethods.Rect lprcMonitor, IntPtr dwData) { NativeMethods.MonitorInfo mi = new NativeMethods.MonitorInfo(); mi.size = (uint)Marshal.SizeOf(mi); bool success = NativeMethods.GetMonitorInfo(hMonitor, ref mi); if (success) { MonitorInfo di = new MonitorInfo(); di.ScreenWidth = (mi.monitor.Right - mi.monitor.Left); di.ScreenHeight = (mi.monitor.Bottom - mi.monitor.Top); di.MonitorArea = mi.monitor; di.WorkArea = mi.work; di.Availability = mi.flags.ToString(); col.Add(di); } return(true); }); NativeMethods.EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, testDelegate, IntPtr.Zero); return(col.AsReadOnly()); }
// ***** Static methods ***** /// <summary> /// Returns a list of connected monitors /// </summary> /// <returns></returns> public static IEnumerable <Monitor> GetAllMonitors() { var monitors = new List <Monitor>(); var proc = new NativeMethods.MonitorEnumProc((monPtr, hdc, lprc, lparam) => { monitors.Add(new Monitor(monPtr)); return(true); }); _ = NativeMethods.EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, proc, IntPtr.Zero); return(monitors); }
/// <summary>Returns a read-only collection containing the handle (HMONITOR) of all connected monitors.</summary> /// <returns>Returns a read-only collection containing the handle (HMONITOR) of all connected monitors.</returns> unsafe private static ReadOnlyCollection <IntPtr> GetMonitorHandles() { var handles = new List <IntPtr>(1); var callback = new NativeMethods.MonitorEnumProc( (IntPtr monitorHandle, IntPtr deviceContextHandle, Rect * monitor, IntPtr param) => { handles.Add(monitorHandle); return(true); } ); if (!NativeMethods.EnumDisplayMonitors(IntPtr.Zero, null, callback, IntPtr.Zero)) { handles.Clear(); } return(new ReadOnlyCollection <IntPtr>(handles)); }
public static extern bool EnumDisplayMonitors(HandleRef hdc, NativeMethods.COMRECT rcClip, NativeMethods.MonitorEnumProc lpfnEnum, IntPtr dwData);
public static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr rcClip, NativeMethods.MonitorEnumProc lpfnEnum, IntPtr dwData);
/// <summary>Returns a read-only collection containing the handle (HMONITOR) of all connected monitors.</summary> /// <returns>Returns a read-only collection containing the handle (HMONITOR) of all connected monitors.</returns> unsafe private static ReadOnlyCollection<IntPtr> GetMonitorHandles() { var handles = new List<IntPtr>( 1 ); var callback = new NativeMethods.MonitorEnumProc( ( IntPtr hMonitor, IntPtr hDeviceContext, Rect* monitor, IntPtr param ) => { handles.Add( hMonitor ); return true; } ); if( !NativeMethods.EnumDisplayMonitors( IntPtr.Zero, null, callback, IntPtr.Zero ) ) handles.Clear(); return new ReadOnlyCollection<IntPtr>( handles ); }