Skip to main content

Hiding / Disabling Maximize and/or Minimize Box in WPF



After trying my hands on the WPF, I realized that I could convert my existing Windows Forms applications into rich looking WPF applications. The task of migration was definitely not easy. During the course of migration, I came across many hurdles and seemed it was impossible to achieve in WPF. But thanks to the msdn help, which made a lot of things easier :-)

Since I was converting my existing application, I started looking for the similar features in WPF. The problem I faced was in the initial phase itself where I wanted to disable MaximizeBox button in WPF. This posed a challenge to me since I had no clue as to how to achieve this. After searching the net for a while, I got a solution which I will share with you all to make your life easier.

Disabling Maximize Box Button on the WPF Window:
This can be done as follows: Put this piece of code above the main class.


internal static class WindowExtensions
{
    [DllImport("user32.dll")]
    internal extern static int SetWindowLong(IntPtr hwnd, int index, int value);

    [DllImport("user32.dll")] internal extern static int GetWindowLong(IntPtr hwnd, int index); 
    internal static void DisableMaximizeButton(this Window window)
    {
        const int GWL_STYLE = -16;
        IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(window).Handle;
        long value = GetWindowLong(hwnd, GWL_STYLE);
        SetWindowLong(hwnd, GWL_STYLE, (int)(value & -65537));
    }
}

This will disable the MaximizeBox; call it from the form’s constructor.

this.DisableMaximizeButton();

However, I found that calling
DisableMaximizeButton(); directly from the form’s constructor does not work. So here is the work around I found in one of the forums. Put it in the form’s constructor this way.

this.SourceInitialized += (x, y) =>
{
    this.DisableMaximizeButton();
};

Disabling Minimize Box Button on the WPF Window:
Above piece of code can be used with the slight modification to the below line. Instead of -65537, use -131073.
SetWindowLong(hwnd, GWL_STYLE, (int)(value & -131073));

And if you wish to hide both MaximizeBox and MinimizeBox buttons, you can do it this way:
SetWindowLong(hwnd, GWL_STYLE, (int)(value & -131073 & -65537));

I hope this will help you :-)
Cheers!

Comments

  1. this will work in WPF window.
    But in case of WPF pages it didn't work.

    I want to disable maximize button in WPF pages


    Is there any way, pls help me

    Advance thanks......

    ReplyDelete
  2. Wow that's exactly what i wanted.
    Thank you very much

    ReplyDelete
  3. Thank you very very very very much :-)

    ReplyDelete
  4. Above disable whole menu with icon

    ReplyDelete
  5. can use WindowStyle="none" it will hide the windoes title buttons

    ReplyDelete

Post a Comment

Popular posts from this blog

Resistance Calculator mobile app for 4, 5 and 6 Band

  Resistance Calculator allows you to quickly find out the ohmic values of a 4 Band, 5 Band and 6 Band color coded resistors.  The UI is very intuitive and simple to use. Just tap on the desired resistor and start selecting colors from the bottom color strip, and the app will show you the corresponding ohmic values of the selected color bands.  The app retains the selection of your colors, so that you can have a quick look each time you open the app. Features: - Simple and easy to use user interface. - Quickly find out the ohmic values of resistors without the need for multiple taps. - Supports 3, 4 and 5 band color coded resistors.

Create a Virtual Drive through a Command Prompt

Just a few weeks back, I was working on a very old application where in all the directory paths used in the code were hard coded and pointing to a S:/ drive. Since I did not have a drive "S" on my machine, and I had to make the application up and running, I was left two choices: 1. Rename all the directory paths in the application, OR 2. Create a Virtual Drive Option 1 seemed to be a lot time consuming and hence I chose option 2 which I felt was a lot easier to do. I searched on the net, and found out one command which could be used to create virtual drives. Windows OS has an application called " subst"  which helps in creating virtual drives mapped to the local folder. The syntax of the command is: SUBST [drive1: [drive2:]path] Example: subst S: C:\Dev When you run this command,  a drive will be created with a letter S: assigned to a local folder "Dev" on drive C.  This is an easy way to create Virtual Drives, though the drives