【问题标题】:C#, Windows Forms: Cant start center the formC#,Windows 窗体:无法开始居中窗体
【发布时间】:2015-03-13 18:13:31
【问题描述】:
  static void Main()
    {
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
          Application.Run(new MainScreen());
    }

这是我创建和运行主屏幕的主屏幕,我尝试过使用:

  • Form.StartPosition = CenterScreen/CenterParent
  • 在构造函数中手动更改表单位置。
  • 我什至尝试过单独创建表单,然后更改其 StartPosition,然后在 Application.Run() 中调用它。

这些都不起作用,它只是一直显示在屏幕的左上角。 关于如何使表格居中的任何想法?

【问题讨论】:

  • 使用Form.CenterToScreen方法
  • @AmeyaDeshpande CenterToScreen 方法 " 不打算直接从您的代码中使用。"
  • 您确定没有将 StartPosition 设置为 Manual 的代码行吗?你的 Location 属性是 0,0?
  • @Steve 在查看表单属性后,我注意到 windowState 被设置为最大化,这可能是导致它的原因。现在已修复,感谢大家的回答。
  • 我建议在您的发现中发布答案。虽然这是一个简单的错误,但它可能对未来的读者有用

标签: c# winforms centering


【解决方案1】:

你试过这个吗?解决方法

this.Location = new Point((Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2,
                          (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2);

【讨论】:

  • 我将其标记为答案,我已将其放入构造函数中,并且在注意到 WindowState 属性设置为导致问题的最大化后起作用。将其更改为正常,现在可以使用了,谢谢。
  • 我还建议使用Screen.FromControl(this) 而不是Screen.PrimaryScreen
猜你喜欢
  • 1970-01-01
  • 2011-09-20
  • 1970-01-01
  • 1970-01-01
  • 2017-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多