/// <summary> /// デバイスコンテキストからモニタを取得 /// </summary> /// <param name="hDC"></param> /// <returns></returns> private static MonitorInfo GetMonitorFromHDC(IntPtr hDC) { var mi = new MONITORINFOEX(); var success = NativeMethods.GetMonitorInfo(hDC, mi); if (!success) { throw new InvalidOperationException(); } var dc = NativeMethods.CreateDC(mi.szDevice, mi.szDevice, null, IntPtr.Zero); var lpcbName = 0U; NativeMethods.GetICMProfile(dc, ref lpcbName, null); var sb = new StringBuilder((int)lpcbName); NativeMethods.GetICMProfile(dc, ref lpcbName, sb); NativeMethods.DeleteDC(dc); var profileUri = sb.ToString(); var context = new ColorContext(new Uri(profileUri)); return(new MonitorInfo { DeviceName = mi.szDevice, MonitorProfile = context, Bounds = new Rect( mi.rcMonitor.Left, mi.rcMonitor.Top, mi.rcMonitor.Right - mi.rcMonitor.Left, mi.rcMonitor.Bottom - mi.rcMonitor.Top), WorkingArea = new Rect( mi.rcWork.Left, mi.rcWork.Top, mi.rcWork.Right - mi.rcWork.Left, mi.rcWork.Bottom - mi.rcWork.Top), IsPrimary = mi.dwFlags == 1 }); }
internal static extern bool GetMonitorInfo(IntPtr hmonitor, [In, Out] MONITORINFOEX info);