在 WinForm 软件开发过程中如果需要窗体在启动时居中显示,可以设置其 StartPosition
属性为 CenterScreen
:
StartPosition
属性。StartPosition
属性设置为 CenterScreen
。如果想要在窗体显示后将 Form 居中,可以使用以下 C# 代码:
public void CenterFormOnScreen()
{
Screen screen = Screen.FromPoint(this.Location);
int screenWidth = screen.WorkingArea.Width;
int screenHeight = screen.WorkingArea.Height;
int formWidth = this.Width;
int formHeight = this.Height;
int x = screen.Bounds.X + (screenWidth - formWidth) / 2;
int y = screen.Bounds.Y + (screenHeight - formHeight) / 2;
this.Location = new Point(x, y);
}
这里,我们使用 Screen.FromPoint(this.Location)
来获取包含窗体位置的屏幕,然后计算窗体的居中位置。这样,无论窗体位于哪个屏幕上,都会在相应的屏幕上居中显示。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。