Winform窗体显示在父窗体的中间位置几种代码 (转)

对于winform窗体显示在父窗体中,有两种情况,第一是模式显示,既showDialog().这种显示只需要设置StartPosition=CenterPostion.用代码如下:

From f2
=new Form();

f2.StartPosition
= FormStartPosition.CenterParent;

f2.ShowDialog()

第二种是非模式显示:这样只能用手动去控制他的位置了。代码如下:

From f2
=new Form();

f2.Left
= this.Left + (this.Width - f2.Width) / 2;
f2.Top
= this.Top + (this.Top - f2.Top) / 2;
f2.Show();

再在属性中设置StartPosition 为Manual.

特别提示:这里f2.show()一定要写在f2.left,f2.top后面,否则效果会达不到的

你可能感兴趣的:(WinForm)