private void WmGetMinMaxInfo(System.IntPtr hwnd, System.IntPtr lParam) { _MinMaxInfo mmi = (_MinMaxInfo)Marshal.PtrToStructure(lParam, typeof(_MinMaxInfo)); // Adjust the maximized size and position to fit the work area of the correct monitor int MONITOR_DEFAULTTONEAREST = 0x00000002; System.IntPtr monitor = UnsafeNativeMethods.MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); if (monitor != System.IntPtr.Zero) { _MonitorInfo monitorInfo = new _MonitorInfo(); UnsafeNativeMethods.GetMonitorInfo(monitor, monitorInfo); _Rect rcWorkArea = monitorInfo.rcWork; _Rect rcMonitorArea = monitorInfo.rcMonitor; mmi.ptMaxPosition.X = Math.Abs(rcWorkArea.left - rcMonitorArea.left); mmi.ptMaxPosition.Y = Math.Abs(rcWorkArea.top - rcMonitorArea.top); var metroWindow = AssociatedObject as MetroWindow; var ignoreTaskBar = metroWindow != null && (metroWindow.IgnoreTaskbarOnMaximize); var x = ignoreTaskBar ? monitorInfo.rcMonitor.left : monitorInfo.rcWork.left; var y = ignoreTaskBar ? monitorInfo.rcMonitor.top : monitorInfo.rcWork.top; mmi.ptMaxSize.X = ignoreTaskBar ? Math.Abs(monitorInfo.rcMonitor.right - x) : Math.Abs(monitorInfo.rcWork.right - x); mmi.ptMaxSize.Y = ignoreTaskBar ? Math.Abs(monitorInfo.rcMonitor.bottom - y) : Math.Abs(monitorInfo.rcWork.bottom - y); // only do this on maximize if (!ignoreTaskBar && AssociatedObject.WindowState == WindowState.Maximized) { mmi.ptMaxTrackSize.X = mmi.ptMaxSize.X; mmi.ptMaxTrackSize.Y = mmi.ptMaxSize.Y; mmi = AdjustWorkingAreaForAutoHide(monitor, mmi); } } Marshal.StructureToPtr(mmi, lParam, true); }
/// <summary> /// This method handles the window size if the taskbar is set to auto-hide. /// </summary> private static _MinMaxInfo AdjustWorkingAreaForAutoHide(IntPtr monitorContainingApplication, _MinMaxInfo mmi) { IntPtr hwnd = UnsafeNativeMethods.FindWindow("Shell_TrayWnd", null); IntPtr monitorWithTaskbarOnIt = UnsafeNativeMethods.MonitorFromWindow(hwnd, Constants.MONITOR_DEFAULTTONEAREST); if (!monitorContainingApplication.Equals(monitorWithTaskbarOnIt)) { return(mmi); } var abd = new _AppBarData(); abd.cbSize = Marshal.SizeOf(abd); abd.hWnd = hwnd; UnsafeNativeMethods.SHAppBarMessage((int)_ABMsg.ABM_GETTASKBARPOS, ref abd); int uEdge = GetEdge(abd.rc); bool autoHide = Convert.ToBoolean(UnsafeNativeMethods.SHAppBarMessage((int)_ABMsg.ABM_GETSTATE, ref abd)); if (!autoHide) { return(mmi); } switch (uEdge) { case (int)_ABEdge.ABE_LEFT: mmi.ptMaxPosition.X += 2; mmi.ptMaxTrackSize.X -= 2; mmi.ptMaxSize.X -= 2; break; case (int)_ABEdge.ABE_RIGHT: mmi.ptMaxSize.X -= 2; mmi.ptMaxTrackSize.X -= 2; break; case (int)_ABEdge.ABE_TOP: mmi.ptMaxPosition.Y += 2; mmi.ptMaxTrackSize.Y -= 2; mmi.ptMaxSize.Y -= 2; break; case (int)_ABEdge.ABE_BOTTOM: mmi.ptMaxSize.Y -= 2; mmi.ptMaxTrackSize.Y -= 2; break; default: return(mmi); } return(mmi); }