using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace FancyZonesEditor_DPI_netcore_test
{
public class MonitorsInfo
{
///
/// Rectangle
///
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
public int width
{
get
{
return right - left;
}
}
public int height
{
get
{
return bottom - top;
}
}
}
///
/// Monitor information.
///
[StructLayout(LayoutKind.Sequential)]
public struct MONITORINFO
{
public uint size;
public RECT monitor;
public RECT work;
public uint flags;
}
///
/// Monitor Enum Delegate
///
/// A handle to the display monitor.
/// A handle to a device context.
/// A pointer to a RECT structure.
/// Application-defined data that EnumDisplayMonitors passes directly to the enumeration function.
///
public delegate bool MonitorEnumDelegate(IntPtr hMonitor, IntPtr hdcMonitor,
ref RECT lprcMonitor, IntPtr dwData);
///
/// Enumerates through the display monitors.
///
/// A handle to a display device context that defines the visible region of interest.
/// A pointer to a RECT structure that specifies a clipping rectangle.
/// A pointer to a MonitorEnumProc application-defined callback function.
/// Application-defined data that EnumDisplayMonitors passes directly to the MonitorEnumProc function.
///
[DllImport("user32.dll")]
public static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip,
MonitorEnumDelegate lpfnEnum, IntPtr dwData);
///
/// Gets the monitor information.
///
/// A handle to the display monitor of interest.
/// A pointer to a MONITORINFO instance created by this method.
///
[DllImport("user32.dll")]
public static extern bool GetMonitorInfo(IntPtr hmon, ref MONITORINFO mi);
///
/// Monitor information with handle interface.
///
public interface IMonitorInfoWithHandle
{
IntPtr MonitorHandle { get; }
MONITORINFO MonitorInfo { get; }
}
///
/// Monitor information with handle.
///
public class MonitorInfoWithHandle : IMonitorInfoWithHandle
{
///
/// Gets the monitor handle.
///
///
/// The monitor handle.
///
public IntPtr MonitorHandle { get; private set; }
///
/// Gets the monitor information.
///
///
/// The monitor information.
///
public MONITORINFO MonitorInfo { get; private set; }
///
/// Initializes a new instance of the class.
///
/// The monitor handle.
/// The monitor information.
public MonitorInfoWithHandle(IntPtr monitorHandle, MONITORINFO monitorInfo)
{
MonitorHandle = monitorHandle;
MonitorInfo = monitorInfo;
}
}
///
/// Monitor Enum Delegate
///
/// A handle to the display monitor.
/// A handle to a device context.
/// A pointer to a RECT structure.
/// Application-defined data that EnumDisplayMonitors passes directly to the enumeration function.
///
public static bool MonitorEnum(IntPtr hMonitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr dwData)
{
var mi = new MONITORINFO();
mi.size = (uint)Marshal.SizeOf(mi);
GetMonitorInfo(hMonitor, ref mi);
// Add to monitor info
_monitorInfos.Add(new MonitorInfoWithHandle(hMonitor, mi));
return true;
}
///
/// Gets the monitors.
///
///
public static MonitorInfoWithHandle[] GetMonitors()
{
_monitorInfos = new List();
EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, MonitorEnum, IntPtr.Zero);
return _monitorInfos.ToArray();
}
private static List _monitorInfos;
}
}