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

匆匆过客 提交于 2019-12-02 06:08:11

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;
    }
}
// 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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!