C# 怎么设置子窗体在主窗体中居中显示

现在有两个窗体Form1主窗体,Form2子窗体
而且我相信大部分人都会这样写
在子窗体的Load事件中
这样写
this.StartPosition = FormStartPosition.CenterParent;
其实这样写是不对的,正确的写法应该是
this.StartPosition = FormStartPosition.CenterScreen;
为什么是CenterScreen而不是CenterParent呢?
那是因为我们调用的方法的问题,如果你在调用子窗体时是这样写的话
            Form2 f2 = new Form2();
            f2.MdiParent  = this;
            f2.Show();
那就得使用CenterScreen而不是CenterParent了,因为在Show的时候窗体是Owner页不是Parent
只要使用ShowDialog()方法时使用CenterParent才有效
代码不应该写在Load事件中,而是在Show方法之前写。
正确的写法应该是这样的
            Form2 f2 = new Form2();
            f2.MdiParent  = this;
            f2.StartPosition = FormStartPosition.CenterScreen;
            f2.Show();

子窗体不需要任何操作。

猜你喜欢

转载自www.cnblogs.com/susiesnai-sun/p/12361261.html