开发者

How is it possible to centre a form in C#

开发者 https://www.devze.com 2023-03-13 23:24 出处:网络
How can you centre a form in C#, so it appears in the middle of the sc开发者_如何学运维reenIf you\'re talking about where it starts, use Form.StartPosition:

How can you centre a form in C#, so it appears in the middle of the sc开发者_如何学运维reen


If you're talking about where it starts, use Form.StartPosition:

form.StartPosition = FormStartPosition.CenterScreen;

If you want to do it at an arbitrary time, you'll need to work out the size of the screen, the size of the form, and calculate it yourself before setting the Location property accordingly.


Use the StartPosition property of the form, change it to CenterScreen.


In Winforms, there is a property of a Form control called StartPosition; it's an enumeration, which includes the position "CenterScreen". Use this, and your window will appear, when first created, in the middle of the monitor on which the mouse is currently located.


You can set the StartPosition property in the designer


Click your form and press F4 to show the Properties tab, then scroll down to the StartPosition property and change the value to CenterScreen.


If you want it to be centered at startup, set your form's StartPosition ( http://msdn.microsoft.com/en-us/library/system.windows.forms.form.startposition.aspx ) to CenterScreen.

If you want it to be centered some other time, you have to use the SystemInformation.WorkingArea class ( http://msdn.microsoft.com/en-us/library/system.windows.forms.systeminformation.workingarea.aspx ) and perform arithmetic on that like so:

X = (Screen Width - Form Width) / 2

Y = (Screen Height - Form Height) / 2

And then consider what should happen if your form was too big. Setting either or both coordinates to a negative number is poor form, so don't forget that case.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号