C# 判断表单是否最大化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/940549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Tell If Form Is Maximising
提问by Ozzy
Ok heres my problem. I have a form that when it is not maximised, its maximum size has to be the total height of the components inside the form. To achieve this, i use this:
好的继承人我的问题。我有一个表单,当它没有最大化时,它的最大尺寸必须是表单内组件的总高度。为了实现这一点,我使用这个:
private void resize_form(object sender, EventArgs e)
{
this.MaximumSize = new System.Drawing.Size(1000, this.panel4.Height + this.label2.Height + this.HeightMin);
}
That fires on the Resize event of the form. Because the component size is always changing it made sense to do this on a resize event. How ever if i want to maximise the form, the form just goes to the highest settings defined in this.MaximumSize. So i was wondering is there a way to tell when a form is going to be maximised and set its maximumsize to the screen boundarys before the form maximises.
这会在表单的 Resize 事件上触发。因为组件大小总是在变化,所以在调整大小事件上这样做是有意义的。如果我想最大化表单,表单只会转到 this.MaximumSize 中定义的最高设置。所以我想知道有没有办法告诉表单何时最大化并在表单最大化之前将其最大尺寸设置为屏幕边界。
If there is a better way to change the maximumsize value without resize event, that would also be great :)
如果有更好的方法来在没有调整大小事件的情况下更改最大大小值,那也很棒:)
采纳答案by Ozzy
I found the answer that suited me perfectly. A lil WndProc override :D (i love WndProc now)
我找到了非常适合我的答案。小 WndProc 覆盖 :D(我现在喜欢 WndProc)
protected override void WndProc(ref Message message)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_MAXIMIZE = 0xF030;
switch (message.Msg)
{
case WM_SYSCOMMAND:
int command = message.WParam.ToInt32() & 0xfff0;
if (command == SC_MAXIMIZE)
{
this.maximize = true;
this.MaximumSize = new System.Drawing.Size(0, 0);
}
break;
}
base.WndProc(ref message);
}
private void resize_form(object sender, EventArgs e)
{
if (!maximize)
{
this.MaximumSize = new System.Drawing.Size(1000, this.panel4.Height + this.label2.Height + this.HeightMin);
}
}
Basically it sets this.maximize to true when it receives teh SC_MAXIMIZE message. The resize event will only set a new MaximumSize if this.maximize is set to false. Nifty xD
基本上它在收到 SC_MAXIMIZE 消息时将 this.maximize 设置为 true。如果 this.maximize 设置为 false,则 resize 事件只会设置一个新的 MaximumSize。漂亮xD
回答by ChrisF
You still need to use the resize event, but check the WindowState
:
您仍然需要使用 resize 事件,但请检查WindowState
:
if (this.WindowState == FormWindowState.Maximized)
{
// Do your stuff
}
As yshuditelu points out you can set the minimum size property of your form too - which should, when coupled with judicious use of anchor values, mean that it can never shrink too far and when it does grow the components will move and/or grow as required.
正如 yshuditelu 指出的那样,您也可以设置表单的最小大小属性 - 这应该与明智地使用锚值相结合,意味着它永远不会缩小太多,并且当它确实增长时,组件将移动和/或增长为必需的。
回答by Timothy Carter
Are you sure you don't want to be setting the MinimumSizeproperty? If you set the MinimumSize to the size of all the labels, then the form will never be smaller than that. But it can still grow to whatever size the user wants, so long as it is larger than the minimum.
您确定不想设置MinimumSize属性吗?如果将 MinimumSize 设置为所有标签的大小,则表单永远不会小于该大小。但它仍然可以增长到用户想要的任何大小,只要它大于最小值。
回答by Drew Hoskins
Check out the System.Windows.Forms.Screenclass. Get the screen from a relevant point (to handle the multi-mon case) and then get its resolution.
查看System.Windows.Forms.Screen类。从相关点获取屏幕(以处理多监视器情况),然后获取其分辨率。
This should work in conjunction with the other comment about checking FormWindowState.Maximized.
这应该与有关检查 FormWindowState.Maximized 的其他注释结合使用。
回答by Pedro Arruda
If the user clicks in the upper bar, they can resize the window, so I use this:
如果用户点击上方的栏,他们可以调整窗口大小,所以我使用这个:
private void Form1_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Normal)
{
this.WindowState = FormWindowState.Maximized;
}
}