internal static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi);
// TODO: Move this, and associated features to a separate class so it can be used from other windows as well. private void WmGetMinMaxInfo(IntPtr hwnd, IntPtr lParam) { var mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO)); // Adjust the maximized size and position to fit the work area of the correct monitor const int monitorDefaulttonearest = 0x00000002; var monitor = MonitorFromWindow(hwnd, monitorDefaulttonearest); if (monitor != IntPtr.Zero) { var monitorInfo = new MONITORINFO(); GetMonitorInfo(monitor, monitorInfo); var rcWorkArea = monitorInfo.rcWork; var rcMonitorArea = monitorInfo.rcMonitor; var source = PresentationSource.FromVisual(this); if (null == source) { this.LogWarning("Unable to get PresentationSource for this window"); return; } var compositionTarget = source.CompositionTarget; if (null == compositionTarget) { this.LogWarning("Unable to get CompositionTarget for this window"); return; } var dpiX = compositionTarget.TransformToDevice.M11; var dpiY = compositionTarget.TransformToDevice.M22; mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.left - rcMonitorArea.left); mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top); mmi.ptMaxSize.x = (int)Math.Abs(rcWorkArea.right - rcWorkArea.left * dpiX); mmi.ptMaxSize.y = (int)Math.Abs(rcWorkArea.bottom - rcWorkArea.top * dpiY); } Marshal.StructureToPtr(mmi, lParam, true); }