Disable maximizing WPF window on double click on the caption

double-click, maximize, window, wndproc, wpf

Solution

WPF does not have a native way to disable maximizing windows (unlike WinForms). Hence consider the following key points:

1. Hide the Maximize button

Using WinAPI is a way to go, but only for hiding the Maximize button. Use the following:

[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

private const int GWL_STYLE = -16;
private const int WS_MAXIMIZEBOX = 0x10000;

private void Window_SourceInitialized(object sender, EventArgs e)
{
    var hwnd = new WindowInteropHelper((Window)sender).Handle;
    var value = GetWindowLong(hwnd, GWL_STYLE);
    SetWindowLong(hwnd, GWL_STYLE, (int)(value & ~WS_MAXIMIZEBOX));
}

2. Handling maximizing manually

That code above still allows maximizing (e.g. by double click on the window's title).

WPF has no control on the title bar behavior. if you want to change the double click behavior you will need to remove the title bar and create you own. Take a look how it's been done in MahApps.Metro - link to sample. After that handle double click event.

Problem

How to disable maximizing WPF window on double click on the caption and leave resizing available? I know that ResizeMode disables maximizing, but it also prevents resizing the form ``` ResizeMode="CanMinimize" ``` I know how to remove maximize and minimize buttons, but it's still possible to maximize by double click on the caption. In WinForms it can be achieved easily. Just set FormBorderStyle from None to FixedSingle or Fixed3D. But it's not an option in WPF any more. P.S. I'm trying some tricks with handling WM_GETMINMAXINFO, WM_SYSCOMMAND, etc. But seems it's not working...

Original source

Related problems