[英]What's the safest way to adjust my WinForms application main form starting size and position at run time?
A WinForms application of mine makes serious use of screen area and would better be given as much as possible. 我的WinForms应用程序会严重占用屏幕区域,因此最好提供尽可能多的屏幕区域。 At the same time I would not like to ignore small screen users.
同时,我不想忽略小屏幕用户。 I also find it a bad manner to make an application to start in maximized mode by default.
我还发现默认情况下使应用程序以最大化模式启动是一种不好的方式。 Moreover, I seek to deliver reasonable behavior without need to store and read configuration in files/registry.
此外,我寻求提供合理的行为,而无需在文件/注册表中存储和读取配置。
So what I seek to do is to set the main form size at the application start time, setting it to something like Width = Screen.PrimaryScreen.WorkingArea.Width * 85 / 100;
因此,我要做的是在应用程序启动时设置主窗体的大小,将其设置为
Width = Screen.PrimaryScreen.WorkingArea.Width * 85 / 100;
(and the same thing with height). (和高度相同)。
And this works but the problem is that the form bottom and right edges usually go beyond the screen in this case even though it is perfectly possible to fit the screen nicely if positioned properly. 这样做是可行的,但问题是这种情况下,即使正确放置屏幕也很可能很好地适合屏幕,但表单的底部和右侧边缘通常会超出屏幕。
What is the way to adjust the main form position effectively? 有效调整主窗体位置的方法是什么?
Maybe you did not set the formposition. 也许您没有设置格式。
First , Set 一,设定
this.StartPosition = FormStartPosition.Manual;
If you didn't set it , the this.locatin command won't ever work. 如果未设置,则this.locatin命令将永远无法工作。
Next 下一个
this.Location = new Point(0, 0);
It should works 应该可行
Set the Location
to center the form based on the size you've chosen: 设置
Location
以根据您选择的大小将表单居中:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.Width = Screen.PrimaryScreen.WorkingArea.Width * 85 / 100;
this.Height = Screen.PrimaryScreen.WorkingArea.Height * 85 / 100;
this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width * 15 / 200, Screen.PrimaryScreen.WorkingArea.Height * 15 / 200);
}
Be sure StartPosition
is set to manual. 确保将
StartPosition
设置为手动。
I usually attempt to load an application with the same position and size it had when it last run, with this information persisted in the registry. 我通常会尝试以与上次运行时相同的位置和大小加载应用程序,并将此信息保留在注册表中。
There are a few complications in trying to do this - the screen resolution may have changed since the app last ran, etc. To do so you might need to: 尝试执行此操作时会遇到一些麻烦-自从应用程序上次运行以来,屏幕分辨率可能已发生更改,等等。为此,您可能需要:
handle SizeChanged and remember the size of the form whenever its WindowState is Normal (no point in remembering it when it's maximized or minimized. 处理SizeChanged,并在窗体的WindowState为“正常”时记住窗体的大小(在最大化或最小化时记住窗体没有意义。
similarly remember its location whenever it's moved. 同样,只要移动它,就记住它的位置。
persist this information when the app is closed 关闭应用程序后保留此信息
on startup, use the persisted information to position and size the form, adjusted to ensure the whole form fits on the screen and has a sensible minimum size. 在启动时,使用保留的信息来定位和调整表格大小,并进行调整以确保整个表格都适合屏幕并具有合理的最小尺寸。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.