Search code examples
c#wpfpinvoke

Is it possible to specify the MaximizedBounds of a WPF window via PInvoke?


I'm having trouble with my borderless WPF window. Specifically, it's maximizing over top the windows taskbar, and also maximizes with a -7 margin, making the window maximize beyond the screen by 7 pixels in each direction.

This would be easily solved in winforms by setting the MaximizedBounds property of the window, but WPF does not have this property.

I tried solving this intercepting the maximize message via WndProc and setting the size/position manually, but this had the issue of overwriting the RestoreRegion to the maximized size/position, removing the ability to restore the window.

So, I thought that I might be able to set the MaximizedBounds of the window via PInvoke.

This page implies that it can be done with Win32 SetWindowPlacement... It takes a structure called a WINDOWPLACEMENT:

    [Serializable]
    [StructLayout(LayoutKind.Sequential)]
    public struct WINDOWPLACEMENT
    {
        public int length;
        public int flags;
        public int showCmd;
        public POINT minPosition;
        public POINT maxPosition;
        public RECT normalPosition;
    } 

I think that I just need to set the Rectanle and a flag to specify that it's for the MaximizedBound property, but I can't find any example of this online, and none of the flags I've found look like it would accomplish this. Am I barking up the wrong tree? If so, is there any other way to specify the MaximizedBound (or similar) that I've overlooked?


Solution

  • I'd try something like this before going down the P/Invoke route - there is no WM_SETMINMAXINFO message that I'm aware of, so you'd probably need to override your WinProc to handle the WM_GETMINMAXINFO and return bogus data....but anyways, try this first, see if it works for you:

    public class ConstrainedWindow : Window
    {
        public ConstrainedWindow()
        {
            this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
            this.MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;
        }
    
    }