当窗口最大化时获得非最大化的窗口位置/大小 [英] Obtain non-maximized window position/size when window is maximized

查看:150
本文介绍了当窗口最大化时获得非最大化的窗口位置/大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户调整我的程序窗口(Winforms)的大小并调整其位置时,即使关闭并重新打开该程序,他们也希望该窗口处于相同的位置.我要做的是存储表单的Width,Height,Location.X和Location.Y属性,并在重新打开程序时将其设置回原来的位置.

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

问题是当窗口最大化时,宽度",高度","X","Y"不是指非最大化的宽度/高度/X/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.

因此,当用户最大化窗口时,关闭并重新打开程序,然后取消最大化窗口的收益,而不是返回到原始位置/大小,而是停留在完整的大小/位置.

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.

因此,在某些事件执行之后,无需使用kudge来存储变量,当窗口最大化时,如何获得未最大化的位置和大小?

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?

推荐答案

解决这个问题的最佳方法是使用RestoreBounds结构.当窗口最大化时,RestoreBounds将引用旧的(未最大化)大小和位置.这是找出这些值的代码.只需在关闭时保存这些值,然后在重新打开程序时,就可以将表单的Width,Height,Location.X和Location.Y设置回这些保存的值.

The best way to solve this I found is to use the RestoreBounds structure. When a window is maximized, RestoreBounds will refer to the old (non-maximized) size and position. 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.

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;

这篇关于当窗口最大化时获得非最大化的窗口位置/大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆