简体   繁体   English

当窗口最大化时获得非最大化的窗口位置/大小

[英]Obtain non-maximized window position/size when window is maximized

When users resize and adjust the location of my program's window (Winforms), they expect to see the window in that same position, even after closing and reopening the program. 当用户调整大小并调整程序窗口(Winforms)的位置时,即使关闭并重新打开该程序,他们也希望窗口处于相同位置。 What I do is store the form's Width, Height, Location.X and Location.Y properties, and set these back when the program is reopened. 我要做的是存储表单的Width,Height,Location.X和Location.Y属性,并在重新打开程序时将其设置回原位。

The problem is when a window is maximized, Width, Height, X, Y refer not to the non-maximized Width/Height/X/Y, but instead to the Maximized dimensions. 问题是当窗口最大化时,宽度,高度,X,Y不是指未最大化的宽度/高度/ X / Y,而是指最大化的尺寸。

Thus when a user has the window maximized, closes and reopens the program, and the proceeds to un-maximize the window, instead of it returning to the original position/size, it sticks at the full size/position. 因此,当用户使窗口最大化时,关闭并重新打开该程序,然后取消窗口最大化,而不是返回到原始位置/大小,而是停留在完整的大小/位置。

So without using a kludge to store variables after certain events execute, how can I obtain the non-maximized position and size when the window is maximized? 因此,在某些事件执行后,无需使用kudge来存储变量,当窗口最大化时,如何获得非最大化的位置和大小?

The best way to solve this I found is to use the RestoreBounds structure. 我发现解决此问题的最佳方法是使用RestoreBounds结构。 When a window is maximized, RestoreBounds will refer to the old (non-maximized) size and position. 当窗口最大化时,RestoreBounds将引用旧的(未最大化)大小和位置。 Here is the code to find out these values. 这是找出这些值的代码。 Simply save these values upon closing, and then when the program is reopened, you can set the Width, Height, Location.X and Location.Y of the form back to these saved values. 只需在关闭时保存这些值,然后在重新打开程序时,就可以将表单的Width,Height,Location.X和Location.Y设置回这些保存的值。

bool b = WindowState == FormWindowState.Maximized;
int xpos = !b? Location.X : RestoreBounds.X;
int ypos = !b? Location.Y : RestoreBounds.Y;
int width = !b? Width : RestoreBounds.Width;
int height = !b? Height : RestoreBounds.Height;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM