Preventing Winform from being maximized?

23,908

Solution 1

There are actually two completely separate issues at work here. Granted, they look basically the same from a functional perspective, but from an implementation-model perspective (as Windows itself would naturally use) they are unrelated.

As others have answered, you can prevent your form from being maximized by setting its MaximizeBox property to false. This removes the WS_MAXIMIZEBOX style on the form's underlying window, which prevents the window manager from allowing the window to be maximized. It disables the maximize box on the window's caption bar (the one between the minimize box and the close box), disables the "Maximize" option on the window/system menu, and any other methods of maximizing a window that I may have forgotten.

However, this has nothing to do with the Win+ keyboard shortcut, which invokes Aero Snap the same as would dragging the window to the the magic position sat the edges of the screen. The window manager, whether as a result of a bug or a feature of Aero Snap, still allows you to effectively "maximize" windows that should not otherwise be maximized. Setting the MaximizeBox property doesn't affect this, so if you truly want to prevent the user from changing the window's size this way, you will need to disable Aero Snap's effect on your window.

Unfortunately, there's no method (at least, not to my knowledge) of programmatically disabling Aero Snap on a per-window or per-process basis (also see this related question). If the user has Aero Snap enabled, you can assume that they want that behavior and applications aren't supposed to tamper with it. The only thing you can do is completely disable resizing your window. In WinForms, you do that by setting the FormBorderStyle property to one of the following: FormBorderStyle.FixedSingle, FormBorderStyle.Fixed3D, or FormBorderStyle.FixedDialog. If you still want your window to be resizable in other ways, you will need to write the code to handle that manually, which is not a particularly easy task.

Thus, I encourage you to very carefully consider whether this is really necessary. All other non-maximizable windows accomplish this simply by setting the MaximizeBox property (or doing the equivalent in their UI framework), which means that they can still be effectively maximized via Aero Snap. If this works for everyone else's windows, including those that are part of Windows itself, it should probably work for you.

Solution 2

The form has a property called MaximizeBox - set this to false.

In regard to your second question, check out this question and it's answers for the best ways to implement keyboard shortcuts in WinForms.

Solution 3

this.FormBorderStyle = FormBorderStyle.FixedSingle;

That line of code will prevent the user from re-sizing the Window.

In addition to that you hide/disable the maximize box and that should do what you asked.

To disable the maximize box use this

this.MaximizeBox = false;

To hide the maximize box use this as well

this.MinimizeBox = false;

If Maximize and Minimize are set to false the buttons disappear.

Solution 4

Setting the MaximumSize equal to the Size (or some size) at least stops the windows from going full-screen. It still snaps to the top left corner but it's still a window at least and looks right - like it's Windows being stupid instead of your program.

Solution 5

You can prevent the windows snapping to the upper left corner by setting:

private void toolbox_Move(object sender, EventArgs e)
{
    this.WindowState = FormWindowState.Normal;
}

in the move event of the form.

Share:
23,908
Benison Sam
Author by

Benison Sam

I'm a International Masters in Data Analytics Student at University of Hildesheim. I like to develop awesome applications in my free time. My ambition was always to become a versatile programmer. But now my attention is also towards Data Science and AI.

Updated on January 08, 2020

Comments

  • Benison Sam
    Benison Sam over 4 years

    I want to prevent my desktop application from being maximized. It should not become maximized by any means - by double clicking on title bar, or by clicking Windows + Up arrow on the keyboard, etc.

    -> I disable both the MaximizeBox and MinimizeBox.
    -> I also set the MaximumSize and MinimumSize for my WinForm

    Still when I press Windows + Up arrow, my win form Shifts to top left of the screen, I mean it gets maximized. So please tell me any way to prevent this thing happening...

  • Benison Sam
    Benison Sam over 11 years
    MaximizeBox = false will not stop the Windows + Up Arrow shortcut to maximize any window... If I'm not wrong, coz m facing the same problem in my application....
  • dsgriffin
    dsgriffin over 11 years
    You could always disable the Windows + Up shortcut as well, and then focus on making every other keyboard shortcut available to use..
  • Benison Sam
    Benison Sam over 11 years
    Buddy this will do but m having a custom form with FormBorderStyle = FormBorderStyle.None . Is there an other way to do this...
  • Benison Sam
    Benison Sam over 11 years
    This thing is not working buddy.. The window is shifting to the top left corner just like when you try to maximize a 16-bit dos based application on your 32-bit Windows OS... It just goes to the top left corner and then denies any movement, like when you maximize any window... I mean the size remains the same but all the other properties are same as when any normal window is maximized.. Is there ne way to resolve this problem...??
  • Andre Fly
    Andre Fly over 11 years
    You could suppress the 'WINKEY + UP' combination to prevent maximizing the window. Besides that I would set the Maximum/Minimum size to a fixed value. This solution seems pretty dirty, but if your requirement is the 'FormBorderStyle.None'-option, that's probably your best shot.
  • Karlovsky120
    Karlovsky120 over 7 years
    I came across this looking for similar answer and setting MaximizeBox to false seems to prevent any path of maximization. I'm guess this was fixed between now and posting of this answer. Or did I miss something?
  • user1703401
    user1703401 over 6 years
    Ought to work, but it doesn't. Hammering it off works, stackoverflow.com/a/47994707/17034
  • DCOPTimDowd
    DCOPTimDowd over 5 years
    You might also have to set the Minimum size as well. For me, if the Min and Max aren't equal, Win+Up can still mess with the size. I made a LockSize() and UnlockSize() for resizing. Lock() sets both Min and Max to the current size, and Unlock() sets the Min to a true minimum, while the Max gets put to an arbitrary large number.