private static string GetPrimaryMonitorDeviceName() { string name = null; if (SafeNativeMethods.GetSystemMetrics(NativeConstants.SM_CMONITORS) != 0) { NativeStructs.POINT point = new NativeStructs.POINT { x = 0, y = 0 }; IntPtr hMonitor = SafeNativeMethods.MonitorFromPoint(point, NativeConstants.MONITOR_DEFAULTTOPRIMARY); NativeStructs.MONITORINFOEX monitorInfo = new NativeStructs.MONITORINFOEX { cbSize = (uint)Marshal.SizeOf(typeof(NativeStructs.MONITORINFOEX)) }; if (SafeNativeMethods.GetMonitorInfoW(hMonitor, ref monitorInfo)) { name = monitorInfo.szDeviceName.TrimEnd('\0'); } } return(name); }
/// <summary> /// /// </summary> /// <param name="monitor"></param> /// <param name="hdc"></param> /// <param name="lprcMonitor"></param> /// <param name="lParam"></param> /// <returns></returns> private static bool MonitorEnumProcCallback(IntPtr monitor, IntPtr hdc, IntPtr lprcMonitor, IntPtr lParam) { NativeStructs.MONITORINFOEX info = new NativeStructs.MONITORINFOEX(); NativeMethods.GetMonitorInfo(monitor, info); _currentMonitors.Add(info); if ((info.dwFlags & NativeConstants.MONITORINFOF_PRIMARY) == NativeConstants.MONITORINFOF_PRIMARY) { _primaryMonitor = info; } return(true); }
/// <summary> /// /// </summary> /// <returns></returns> public static NativeStructs.MONITORINFOEX[] GetAllMonitors(out NativeStructs.MONITORINFOEX primaryMonitor) { NativeStructs.RECT empty = NativeStructs.RECT.Empty; NativeStructs.MONITORINFOEX[] monitorArray = null; lock (_globalLock) { _currentMonitors = new List <NativeStructs.MONITORINFOEX>();; NativeMethods.EnumDisplayMonitors(IntPtr.Zero, null, new NativeMethods.MonitorEnumProc(MultiMonitorHelper.MonitorEnumProcCallback), IntPtr.Zero); if (_currentMonitors.Count <= 0) { throw new InvalidOperationException("No Monitor found."); } monitorArray = _currentMonitors.ToArray(); primaryMonitor = _primaryMonitor; } return(monitorArray); }