Window does not resize properly when moved to larger display

前端 未结 2 2100
抹茶落季
抹茶落季 2021-02-05 14:23

My WPF application is exhibiting strange behavior on my two monitor laptop development system. The second monitor has a resolution of 1920 x 1080; the laptop\'s resolution is 1

2条回答
  •  南笙
    南笙 (楼主)
    2021-02-05 15:07

    Turns out its not a complicated fix. The MinTrackSize point (bounds) needs to be set to the working area dimensions of the secondary monitor.

    private static void WmGetMinMaxInfo(System.IntPtr hwnd, System.IntPtr lParam)
    {
        MINMAXINFO mmi = (MINMAXINFO)System.Runtime.InteropServices.Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));
    
        /*  0x0001        // center rect to monitor 
            0x0000        // clip rect to monitor 
            0x0002        // use monitor work area 
            0x0000        // use monitor entire area */
    
        int MONITOR_DEFAULTTONEAREST = 0x00000002;
        System.IntPtr monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
    
        if (monitor != System.IntPtr.Zero)
        {
            MONITORINFO monitorInfo = new MONITORINFO();
            GetMonitorInfo(monitor, monitorInfo);
            RECT rcWorkArea = monitorInfo.rcWork;
            RECT rcMonitorArea = monitorInfo.rcMonitor;
    
            // set the maximize size of the application
            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);
    
            // reset the bounds of the application to the monitor working dimensions
            mmi.ptMaxTrackSize.x = mmi.ptMaxSize.x;
            mmi.ptMaxTrackSize.y = mmi.ptMaxSize.y;
        }
    
        System.Runtime.InteropServices.Marshal.StructureToPtr(mmi, lParam, true);
    }
    
    [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
    public struct MINMAXINFO
    {
        public POINT ptReserved;
        public POINT ptMaxSize;
        public POINT ptMaxPosition;
        public POINT ptMinTrackSize;
        public POINT ptMaxTrackSize;
    };
    

提交回复
热议问题