最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

网站源码admin10浏览0评论

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

在 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) 来获取包含窗体位置的屏幕,然后计算窗体的居中位置。这样,无论窗体位于哪个屏幕上,都会在相应的屏幕上居中显示。

发布评论

评论列表(0)

  1. 暂无评论