Preventing Winform from being maximized?

2020-02-01 08:04发布

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...

9条回答
趁早两清
2楼-- · 2020-02-01 08:11

There is a property of the Form class named "MaximumBox" you have to set False in the properties window of your form... This actually will disable the form from being maximized by any way... Also if you want to control your form sizes you can work with such properties as "MinimumSize, MaximumSize" setting their values at your discretion or creating an event handler for the MaximumSizeChanged and MinimumSizeChanged events...

查看更多
淡お忘
3楼-- · 2020-02-01 08:14

You can try to RegisterHotKey Win+Up if your window or application is activated and unregister this hot key if it is deactivated. You must catch the hotkey and return appropriate value to prevent further processing.

Look at WM_ACTIVATEAPP, WM_ACTIVATE and WM_NCACTIVATE. The first can be used if you want to disable the Win+UP for all your windows.

查看更多
做个烂人
4楼-- · 2020-02-01 08:14

One thing you can do is set the MaximumSize and MinimumSize in the same value but not 0,0.

查看更多
兄弟一词,经得起流年.
5楼-- · 2020-02-01 08:17

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.

查看更多
\"骚年 ilove
6楼-- · 2020-02-01 08:21

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.

查看更多
Explosion°爆炸
7楼-- · 2020-02-01 08:22

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.

查看更多
登录 后发表回答