前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >WinForm 开发让窗体 Form 居中显示的设置方法(兼容多种尺寸屏幕)

WinForm 开发让窗体 Form 居中显示的设置方法(兼容多种尺寸屏幕)

原创
作者头像
Power
发布2025-04-03 16:06:04
发布2025-04-03 16:06:04
35500
代码可运行
举报
运行总次数:0
代码可运行

在 WinForm 软件开发过程中如果需要窗体在启动时居中显示,可以设置其 StartPosition 属性为 CenterScreen :

  1. 打开您的 WinForm 窗体的设计视图。
  2. 选择窗体(Form)控件。
  3. 在属性窗口中找到 StartPosition 属性。
  4. 将 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) 来获取包含窗体位置的屏幕,然后计算窗体的居中位置。这样,无论窗体位于哪个屏幕上,都会在相应的屏幕上居中显示。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档