private static Monitorinfo MonitorInfoFromWindow(IntPtr hWnd)
        {
            var hMonitor    = NativeMethods.MonitorFromWindow(hWnd, 2);
            var monitorInfo = new Monitorinfo {
                CbSize = (uint)Marshal.SizeOf(typeof(Monitorinfo))
            };

            NativeMethods.GetMonitorInfo(hMonitor, ref monitorInfo);
            return(monitorInfo);
        }
        private void WnGetMinMaxInfo(IntPtr hwnd, IntPtr lparam)
        {
            var mmi     = (Minmaxinfo)Marshal.PtrToStructure(lparam, typeof(Minmaxinfo));
            var monitor = NativeMethods.MonitorFromWindow(hwnd, MonitorDefaulttonearest);

            if (monitor != IntPtr.Zero)
            {
                var monitorInfo = new MonitorInfo();
                NativeMethods.GetMonitorInfo(monitor, monitorInfo);
                var rcWorkArea    = monitorInfo.rcWork;
                var 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);

                mmi.ptMinTrackSize.X = (int)MinWidth;  //minimum drag X size for the window
                mmi.ptMinTrackSize.Y = (int)MinHeight; //minimum drag Y size for the window

                mmi = AdjustWorkingAreaForAutoHide(monitor, mmi);
                //need to adjust sizing if taskbar is set to autohide
            }
            Marshal.StructureToPtr(mmi, lparam, true);
        }