private void WmGetMinMaxInfo(System.IntPtr hwnd, System.IntPtr lParam) { NativeMethods.MINMAXINFO mmi = (NativeMethods.MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(NativeMethods.MINMAXINFO)); // Adjust the maximized size and position to fit the work area of the correct monitor System.IntPtr monitor = NativeMethods.GetMonitorFromWindow(hwnd); // MinHeight and MinWidth need to be scaled from Virtual Pixels back to Logical Pixels DpiScale dpiScale = VirtualPixelScale; if (monitor != System.IntPtr.Zero) { NativeMethods.MONITORINFO monitorInfo = new NativeMethods.MONITORINFO(); NativeMethods.GetMonitorInfo(monitor, monitorInfo); NativeMethods.RECT rcWorkArea = monitorInfo.rcWork; NativeMethods.RECT rcMonitorArea = monitorInfo.rcMonitor; mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.left - rcMonitorArea.left); mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top); mmi.ptMaxSize.x = DoubleToInt32(Math.Abs(rcWorkArea.right - rcWorkArea.left)); mmi.ptMaxSize.y = DoubleToInt32(Math.Abs(rcWorkArea.bottom - rcWorkArea.top)); // After much research, it appears that the MaxTrackSize is used for secondary monitors // while MaxSize is used for the primary monitor. mmi.ptMaxTrackSize.x = DoubleToInt32(Math.Abs(rcWorkArea.right - rcWorkArea.left)); mmi.ptMaxTrackSize.y = DoubleToInt32(Math.Abs(rcWorkArea.bottom - rcWorkArea.top)); mmi.ptMinTrackSize.x = GetMinWidthInScreenPixels(dpiScale); mmi.ptMinTrackSize.y = GetMinHeightInScreenPixels(dpiScale); } Marshal.StructureToPtr(mmi, lParam, true); }
public static Tuple <double, double, double, double> GetWorkingAreaForMonitor(IntPtr monitor) { NativeMethods.MONITORINFO monitorInfo = new NativeMethods.MONITORINFO(); NativeMethods.GetMonitorInfo(monitor, monitorInfo); NativeMethods.RECT rcWorkArea = monitorInfo.rcWork; NativeMethods.RECT rcMonitorArea = monitorInfo.rcMonitor; return(new Tuple <double, double, double, double>(rcWorkArea.left, rcWorkArea.top, rcWorkArea.width, rcWorkArea.height)); }
/// <summary> /// Returns the working area of the monitor that intersects most with the specified rectangle. /// If no monitor can be found, the closest monitor to the rectangle is returned. /// </summary> /// <param name="rectangle">The rectangle that is located on the monitor whose working area should be returned.</param> /// <returns>A rectangle defining the working area of the monitor closest to containing the specified rectangle.</returns> public static Rectangle GetWorkingArea(Rectangle rectangle) { NativeMethods.RECT rect = (NativeMethods.RECT)rectangle; IntPtr monitorhandle = NativeMethods.MonitorFromRect(ref rect, NativeMethods.MONITOR_DEFAULTTONEAREST); NativeMethods.MONITORINFO monitorinfo = new NativeMethods.MONITORINFO(); monitorinfo.cbSize = (uint)Marshal.SizeOf(monitorinfo); bool result = NativeMethods.GetMonitorInfo(monitorhandle, ref monitorinfo); if (!result) { throw new Exception("Failed to retrieve monitor information."); } return(monitorinfo.rcWork); }
private void WmGetMinMaxInfo(ref Message m) { var mmi = (NativeMethods.MINMAXINFO)Marshal.PtrToStructure(m.LParam, typeof(NativeMethods.MINMAXINFO)); IntPtr monitor = UnsafeNativeMethods.MonitorFromWindow(m.HWnd, NativeMethods.MONITOR_DEFAULTTONEAREST); if (monitor != IntPtr.Zero) { var mInfo = new NativeMethods.MONITORINFO(); mInfo.cbSize = Marshal.SizeOf(typeof(NativeMethods.MONITORINFO)); UnsafeNativeMethods.GetMonitorInfo(monitor, ref mInfo); mmi.ptMaxPosition.X = Math.Abs(mInfo.rcWork.left - mInfo.rcMonitor.left); mmi.ptMaxPosition.Y = Math.Abs(mInfo.rcWork.top - mInfo.rcMonitor.top); mmi.ptMaxSize.X = Math.Abs(mInfo.rcWork.right - mInfo.rcWork.left); mmi.ptMaxSize.Y = Math.Abs(mInfo.rcWork.bottom - mInfo.rcWork.top); } Marshal.StructureToPtr(mmi, m.LParam, true); }
internal static void FindMaximumSingleMonitorRectangle(NativeMethods.RECT windowRect, out NativeMethods.RECT screenSubRect, out NativeMethods.RECT monitorRect) { List <NativeMethods.RECT> rects = new List <NativeMethods.RECT>(); NativeMethods.EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, delegate(IntPtr hMonitor, IntPtr hdcMonitor, ref NativeMethods.RECT rect, IntPtr lpData) { NativeMethods.MONITORINFO mONITORINFO = default(NativeMethods.MONITORINFO); mONITORINFO.cbSize = (uint)Marshal.SizeOf(typeof(NativeMethods.MONITORINFO)); NativeMethods.GetMonitorInfo(hMonitor, ref mONITORINFO); rects.Add(mONITORINFO.rcWork); return(true); }, IntPtr.Zero); long num = 0L; screenSubRect = new NativeMethods.RECT { Left = 0, Right = 0, Top = 0, Bottom = 0 }; monitorRect = new NativeMethods.RECT { Left = 0, Right = 0, Top = 0, Bottom = 0 }; foreach (NativeMethods.RECT current in rects) { NativeMethods.RECT rECT = current; NativeMethods.RECT rECT2; NativeMethods.IntersectRect(out rECT2, ref rECT, ref windowRect); long num2 = (long)(rECT2.Width * rECT2.Height); if (num2 > num) { screenSubRect = rECT2; monitorRect = current; num = num2; } } }
/// <summary> /// Returns the working area of the monitor that intersects most with the specified rectangle. /// If no monitor can be found, the closest monitor to the rectangle is returned. /// </summary> /// <param name="rectangle">The rectangle that is located on the monitor whose working area should be returned.</param> /// <returns>A rectangle defining the working area of the monitor closest to containing the specified rectangle.</returns> public static Rect GetWorkingArea(Rect rectangle) { NativeMethods.RECT rect = (NativeMethods.RECT)rectangle; IntPtr monitorhandle = NativeMethods.MonitorFromRect(ref rect, NativeMethods.MONITOR_DEFAULTTONEAREST); NativeMethods.MONITORINFO monitorinfo = new NativeMethods.MONITORINFO(); monitorinfo.cbSize = (uint)Marshal.SizeOf(monitorinfo); bool result = NativeMethods.GetMonitorInfo(monitorhandle, ref monitorinfo); if (!result) throw new Exception("Failed to retrieve monitor information."); return monitorinfo.rcWork; }
internal static extern bool GetMonitorInfo(IntPtr hMonitor, ref NativeMethods.MONITORINFO monitorInfo);
public static extern bool GetMonitorInfo(IntPtr hMonitor, [In, Out] ref NativeMethods.MONITORINFO lpmi);
public static void GetMonitorInfo(IntPtr hMonitor, out NativeMethods.MONITORINFO mi) { mi = new NativeMethods.MONITORINFO(); ThrowLastError(SafeNativeMethods.GetMonitorInfo(hMonitor, ref mi)); }