如何在 C# 和 C# 中使窗口在屏幕上居中WinForms?
我需要一种方法使当前窗口在屏幕上居中。
例如,如果用户按下按钮,我希望窗口在屏幕上居中。
我知道您可以使用 startposition 属性,但除了应用程序首次启动时之外,我无法找到使用该属性的方法。
那么如何使表单在屏幕上居中呢?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
使用 Form.CenterToScreen() 方法。
Use Form.CenterToScreen() method.
使用属性窗口
选择表格→进入属性窗口→选择“起始位置”→选择你想要的位置。
以编程方式
表单 form1 = new Form();
form1.StartPosition = FormStartPosition.CenterScreen;
form1.ShowDialog();
注意:不要直接从代码中调用 Form.CenterToScreen()。请阅读此处。
Using the Property window
Select form → go to property window → select "start position" → select whatever the place you want.
Programmatically
Form form1 = new Form();
form1.StartPosition = FormStartPosition.CenterScreen;
form1.ShowDialog();
Note: Do not directly call Form.CenterToScreen() from your code. Read here.
单行:
A single line:
在 Windows 窗体中:
在 WPF 中:
这就是您所要做的...
In Windows Forms:
In WPF:
That's all you have to do...
如果您想在运行时将窗口居中,请使用下面的代码,将其复制到您的应用程序中:
最后调用上面的方法使其工作:
If you want to center your windows during runtime use the code below, copy it into your application:
And finally call the method above to get it working:
在运行时居中表单
1.设置Form的以下属性:
->开始位置:屏幕中心
-> WindowState: Normal
这将使窗体在运行时居中,但如果窗体大小大于预期,请执行第二步。
2.在InitializeComponent()之后添加自定义Size;
Centering a form in runtime
1.Set following property of Form:
-> StartPosition : CenterScreen
-> WindowState: Normal
This will center the form at runtime but if form size is bigger then expected, do second step.
2. Add Custom Size after InitializeComponent();
使用这个:
Use this:
将您可以控制的任何窗口居中
Centers any window you can get the handle of
可能与问题不完全相关。但也许可以帮助某人。
中心屏幕以上对我来说都不起作用。原因是我正在向表单动态添加控件。从技术上讲,当它居中时,它是正确的,基于添加控件之前的表单。
这是我的解决方案。 (应该适用于这两种情况)
所以您会注意到我使用“PreferredSize”而不是仅使用高度/宽度。
添加控件后,首选大小将保留表单的值。高度/宽度不会的地方。
希望这对某人有帮助。
干杯
Might not be completely relevant to the question. But maybe can help someone.
Center Screen non of the above work for me. Reason was I was adding controls dynamically to the form. Technically when it centered it was correct , based on the form before adding the controls.
So here was my solution. ( Should work with both scenarios )
So you will notice that I am using "PreferredSize" instead of just using Height / Width.
The preferred size will hold the value of the form after adding the controls. Where Height / Width won't.
Hope this helps someone .
Cheers
工作样本
Working sample
使用表单的 Location 属性。将其设置为所需的左上角点
所需的 x = (desktop_width - form_witdh)/2
所需的 y = (desktop_height - from_height)/2
Use Location property of the form. Set it to the desired top left point
desired x = (desktop_width - form_witdh)/2
desired y = (desktop_height - from_height)/2
您可以使用 Screen.PrimaryScreen.Bounds 来检索主显示器的大小(或检查 Screen 对象来检索所有显示器)。使用
MyForms.Bounds
来确定放置表单的位置。You can use the
Screen.PrimaryScreen.Bounds
to retrieve the size of the primary monitor (or inspect theScreen
object to retrieve all monitors). Use those withMyForms.Bounds
to figure out where to place your form.在多显示器的情况下,如果您喜欢在正确的显示器/屏幕上居中,那么您可能想尝试以下几行:
In case of multi monitor and If you prefer to center on correct monitor/screen then you might like to try these lines: