private static DisplayInfo ConvertMonitorInfoToDisplayInfo(IntPtr hMonitor, PInvoke.User32.MONITORINFOEX mi)
 {
     return(new DisplayInfo
     {
         ScreenWidth = mi.Monitor.right - mi.Monitor.left,
         ScreenHeight = mi.Monitor.bottom - mi.Monitor.top,
         DeviceName = new string(mi.DeviceName),
         WorkArea = mi.WorkArea,
         Availability = mi.Flags.ToString(),
         hMonitor = hMonitor
     });
 }
        public static DisplayInfo GetDisplay(IntPtr hwnd)
        {
            DisplayInfo di = null;
            IntPtr      hMonitor;

            PInvoke.RECT rc;
            PInvoke.User32.GetWindowRect(hwnd, out rc);
            hMonitor = PInvoke.User32.MonitorFromRect(ref rc, PInvoke.User32.MonitorOptions.MONITOR_DEFAULTTONEAREST);

            PInvoke.User32.MONITORINFOEX mi = new PInvoke.User32.MONITORINFOEX();
            mi.cbSize = Marshal.SizeOf(mi);
            bool success = PInvoke.User32.GetMonitorInfo(hMonitor, out mi);

            if (success)
            {
                di = ConvertMonitorInfoToDisplayInfo(hMonitor, mi);
            }
            return(di);
        }
        unsafe public static List <DisplayInfo> GetDisplays()
        {
            List <DisplayInfo> col = new();

            _ = EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero,
                                    delegate(IntPtr hMonitor, IntPtr hdcMonitor, ref PInvoke.RECT lprcMonitor, IntPtr dwData)
            {
                PInvoke.User32.MONITORINFOEX mi = new PInvoke.User32.MONITORINFOEX();
                mi.cbSize    = Marshal.SizeOf(mi);
                bool success = PInvoke.User32.GetMonitorInfo(hMonitor, out mi);
                if (success)
                {
                    DisplayInfo di = ConvertMonitorInfoToDisplayInfo(hMonitor, mi);
                    col.Add(di);
                }
                return(true);
            }, IntPtr.Zero);
            return(col);
        }