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); }