简体   繁体   English

在运行时调整WinForms应用程序主窗体的起始大小和位置的最安全方法是什么?

[英]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.

相关问题 获取winforms应用程序名称的正确方法是什么? - what is the right way of getting my winforms application's name? 在应用程序启动时隐藏主窗体的最佳方法是什么 - What is the best way to hide the main form on application's startup 启动不受信任的URL的最安全方法是什么? - What's the safest way to launch an untrusted URL? 确定2个URL是否相同的最安全的方法是什么? - What's the safest way to determine if 2 URLs are the same? WinForms:在其他应用程序的主窗口上显示表单模式 - WinForms: Show form modal over other application's main window 访问主UI线程的最佳/最安全方法是什么? - What is the best/safest way to access the main UI thread? 在一段时间后以编程方式关闭WinForms应用程序的正确方法是什么? - What is the proper way to programmatically close a WinForms application after a certain time? Winforms:每当在我的应用程序中打开表单时,是否有一种通知方法? - Winforms: Is there a way to be informed whenever a form gets opened in my application? 通过屏幕分辨率调整控件的大小/位置 - Adjust Control's Size/Position With Screen Resolution 将应用程序时间与外部服务器时间同步的最佳方法是什么? - What would be the best way to synchronize my application's time with outside server's time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM