how to prevent none state form from being maximized in c#
Asked Answered
M

2

2

i have created a form and set its FormBorderStyle property to none. when i press Windows + UP form will be Maximized. how can i prevent form from maximize? i tried

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

but its not that i want. with above code when i press Windows + Up form will maximize and then it restores to normal state. but i want to prevent it basically.

Mucilage answered 27/12, 2017 at 12:39 Comment(2)
Does #3026423 help?Tshirt
@Hans You might consider taking a closer look at it (for bug or something) because nothing seem to prevent that behavior when FormBorderStyle.NoneGoda
A
5

Setting the form's MaximizeBox to False should be enough to stop this Aero Snap feature. But Form.CreateParams calculates the wrong style flags for some mysterious reason. I can't single-step it right now due to the 4.7.1 update and don't see the mistake in the source code. It might have something to do with disabling it in the system menu but not the style flags, just a guess.

Anyhoo, hammering the native style flag off by force does solve the problem. Copy-paste this code into your form class:

protected override CreateParams CreateParams {
    get {
        const int WS_MAXIMIZEBOX = 0x00010000;
        var cp = base.CreateParams;
        cp.Style &= ~WS_MAXIMIZEBOX;
        return cp;
    }
}
Arrest answered 27/12, 2017 at 15:29 Comment(0)
J
-1
// Define the border style of the form to a dialog box.
form1.FormBorderStyle = FormBorderStyle.FixedDialog;

// Set the MaximizeBox to false to remove the maximize box.
form1.MaximizeBox = false;

// Set the MinimizeBox to false to remove the minimize box.
form1.MinimizeBox = false;

Credit to How do I disable form resizing for users?

Joslyn answered 27/12, 2017 at 12:50 Comment(2)
i want a form with formBorderStyle.noneMucilage
Im sure there are multiple ways to do it but that is what i have shown. FixedDialog will stop manual resizing. MaximiseBox = False stops you from maximising the window and MinimiseBox = False should do the same for minimising.Joslyn

© 2022 - 2024 — McMap. All rights reserved.