private void WmGetMinMaxInfo(IntPtr hwnd, 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;
IntPtr monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
if (monitor != IntPtr.Zero)
{
MONITORINFO monitorInfo = new MONITORINFO();
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);
mmi.ptMaxSize.x = Math.Abs(rcWorkArea.right - rcWorkArea.left);
mmi.ptMaxSize.y = Math.Abs(rcWorkArea.bottom - rcWorkArea.top);
// Convert pixel sizes based on current DPI settings.
// Window.MinHeight/MinWidth are 1/96th of an inch.
PresentationSource presentationSource = PresentationSource.FromVisual(this);
if (presentationSource != null)
{
if (presentationSource.CompositionTarget != null)
{
Point minSize = presentationSource.CompositionTarget.TransformToDevice.Transform(new Point(MinWidth, MinHeight));
mmi.ptMinTrackSize.x = (int)minSize.X;
mmi.ptMinTrackSize.y = (int)minSize.Y;
}
}
}
Marshal.StructureToPtr(mmi, lParam, true);
}