Disabling the Maximize button in WPF

I am writing a WPF application which runs as a tool-style dialog on the desktop, and needs to be able to be minimized but not maximised.  It seems that this is somthing the WPF designers didn’t quite anticipate, despite it having been pretty easy to achieve under Windows Forms.

After searching around the ‘net, I finally found some code that disabled both the minimize & maximize buttons, but took a couple shortcuts that I didn’t like (and besides, it was in VB…).

A bit of searching and a quick cleanup left me with a working result.  Catch the Loaded event of your window, and put the following code into the handler:

// Import some references
using System.Interop;
using System.Runtime.InteropServices;

// Place these inside the class definition...
[DllImport("user32.dll")]
private extern static Int32 SetWindowLong(IntPtr hWnd, Int32 nIndex, Int32 dwNewLong);
[DllImport("user32.dll")]
private extern static Int32 GetWindowLong(IntPtr hWnd, Int32 nIndex);

private const Int32 GWL_STYLE = -16;
private const Int32 WS_MAXIMIZEBOX = 0x10000;
private const Int32 WS_MINIMIZEBOX = 0x20000;

// And finally the event handler...
private void Window_Loaded(object sender, RoutedEventArgs e)
{
    IntPtr hWnd = new WindowInteropHelper(this).Handle;
    Int32 windowLong = GetWindowLong(hWnd, GWL_STYLE);
    windowLong = windowLong & ~WS_MAXIMIZEBOX;
    SetWindowLong(hWnd, GWL_STYLE, windowLong);
}

I have included the value for WS_MINIMIZEBOX in case you need it.

Advertisements

2 responses to “Disabling the Maximize button in WPF

  1. what a great site and informative posts, I will add a backlink and bookmark your site. Keep up the good work!

    I’m Out! 🙂

  2. Why this much

    Use ResizeMode=”CanMinimize” and
    WindowStyle=”SingleBorderWindow”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s