admin管理员组文章数量:1033143
WinForm 开发让窗体 Form 居中显示的设置方法(兼容多种尺寸屏幕)
在 WinForm 软件开发过程中如果需要窗体在启动时居中显示,可以设置其 StartPosition
属性为 CenterScreen
:
- 打开您的 WinForm 窗体的设计视图。
- 选择窗体(Form)控件。
- 在属性窗口中找到
StartPosition
属性。 - 将
StartPosition
属性设置为CenterScreen
。
如果想要在窗体显示后将 Form 居中,可以使用以下 C# 代码:
代码语言:javascript代码运行次数:0运行复制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)
来获取包含窗体位置的屏幕,然后计算窗体的居中位置。这样,无论窗体位于哪个屏幕上,都会在相应的屏幕上居中显示。
WinForm 开发让窗体 Form 居中显示的设置方法(兼容多种尺寸屏幕)
在 WinForm 软件开发过程中如果需要窗体在启动时居中显示,可以设置其 StartPosition
属性为 CenterScreen
:
- 打开您的 WinForm 窗体的设计视图。
- 选择窗体(Form)控件。
- 在属性窗口中找到
StartPosition
属性。 - 将
StartPosition
属性设置为CenterScreen
。
如果想要在窗体显示后将 Form 居中,可以使用以下 C# 代码:
代码语言:javascript代码运行次数:0运行复制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)
来获取包含窗体位置的屏幕,然后计算窗体的居中位置。这样,无论窗体位于哪个屏幕上,都会在相应的屏幕上居中显示。
本文标签: WinForm 开发让窗体 Form 居中显示的设置方法(兼容多种尺寸屏幕)
版权声明:本文标题:WinForm 开发让窗体 Form 居中显示的设置方法(兼容多种尺寸屏幕) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/jiaocheng/1748010646a2241100.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论