在 .NET (Winforms) 中的窗体中居中控件? [英] Centering controls within a form in .NET (Winforms)?

查看:33
本文介绍了在 .NET (Winforms) 中的窗体中居中控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个固定大小的控件居中放置在表单中.

I'm trying to center a fixed size control within a form.

出于兴趣,有没有一种非愚蠢的方法来做到这一点?我真正想要的是类似于 text-align css 属性的东西.

Out of interest, is there a non-idiotic way of doing this? What I really want is something analogous to the text-align css property.

目前,我将周围表单的 padding 属性设置为合适的大小,并将控件的 Dock 属性设置为填充.

At the moment, I'm setting the padding property of the surrounding form to a suitable size and setting the Dock property of the control to fill.

推荐答案

您可以使用锚点来实现这一点.或者更准确地说是不使用它们.

You could achieve this with the use of anchors. Or more precisely the non use of them.

控件默认固定在表单的左上角,这意味着当表单大小改变时,它们与表单左上角的距离将保持不变.如果将控件锚点更改为左下角,则在调整窗体大小时,控件将与窗体底部和左侧保持相同的距离.

Controls are anchored by default to the top left of the form which means when the form size will be changed, their distance from the top left side of the form will remain constant. If you change the control anchor to bottom left, then the control will keep the same distance from the bottom and left sides of the form when the form if resized.

关闭某个方向的锚点将在调整大小时保持控件居中,如果它已经居中.通常,未锚定的控件保持其与对话框的比例位置.例如.如果您将控件放置在对话框宽度的 X=75% 处并关闭左/右锚点,则控件将在对话框宽度的 X=75% 处保持其中心.

Turning off the anchor in a direction will keep a control centered when resizing, if it is already centered. In general, a control not anchored maintains its proportional position to the dialog. E.g. If you put a control at X=75% of the dialog width and turn off left/right anchors, the control will maintain its center at X=75% of the dialog width.

注意:在 VS2015 中通过属性窗口关闭锚定可能需要输入 None(而不是默认的 Top,Left)

NOTE: Turning off anchoring via the properties window in VS2015 may require entering None (instead of default Top,Left)

这篇关于在 .NET (Winforms) 中的窗体中居中控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆