调整大小后如何保持表格居中于屏幕中间 [英] How to keep form centered to the middle of the screen after resize

查看:153
本文介绍了调整大小后如何保持表格居中于屏幕中间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗体,该窗体根据加载时按fontsize调整大小的屏幕位置居中。调整大小后,位置保持不变,因此表单不再像我想的那样居中。

I have a form that is centered according to the screen position that I resize by fontsize when loading. After resizing the location remains the same as it was before resizing so the form is no longer in the center, like I would like.

让我画一个草图:

Let me draw you a sketch:

我尝试致电

        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.ResumeLayout(false);
        this.PerformLayout();

(我相信,这是代码中将表单居中的部分开始)。没用我还发现了类似的问题:
在窗口调整大小
之后使winform控件居中
,但是它们始终只处理控件居中,而不是窗体本身。

again after resizing (this is, I believe, the part of the code that centers the form in the beginning). It didn't work. I've also found some similar issues like this: "Keeping winform control centered after window resize " but they always only deal with centring a control, not the form itself.

推荐答案

ResizeEnd 事件添加方法。在方法中,当触发 ResizeEnd 时,获取当前屏幕尺寸(在多台显示器上,包含当前表单的屏幕),然后计算表单的位置。看看这个例子

Add method for ResizeEnd event. In method, when ResizeEnd is fired, get current screen size (on multiple monitors, screen that contains current form) and then calculate form's position. Take a look at this example

private void Form1_ResizeEnd(object sender, EventArgs e)
{
    Screen myScreen = Screen.FromControl(this);
    Rectangle area = myScreen.WorkingArea;

    this.Top = (area.Height - this.Height) / 2;
    this.Left = (area.Width - this.Width) / 2;
}

这篇关于调整大小后如何保持表格居中于屏幕中间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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