如何获取 Label 和 NumericUpDown 中文本基线的位置?
我正在尝试按文本基线对齐 Label
和 NumericUpDown
。 我是用代码而不是设计师来完成的。 如何获取文本基线的位置?
I'm trying to align a Label
and a NumericUpDown
by their text baselines. I'm doing it in code, rather than the designer. How do I get the position of the text baseline?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
// 以坐标 (pt.X, pt.Y) 处的基线渲染文本:
// to render text with baseline at coordinates (pt.X, pt.Y) :
对于Label控件,可以这样获取文本底部的位置:
找到Label控件中文本的底部:
假设.TextAlign设置为TopLeft或TopCenter或TopRight,则可以通过以下方法 GetHeight 方法返回标签当前使用的字体的高度(以像素为单位)。
如果.TextAlign是Middle或者Bottom,那么你需要做稍微复杂一些的计算。
同样的方法也适用于 NumericUpDown 控件。
For the Label control, you can get the position of the bottom of the text this way:
Assuming the .TextAlign is set to TopLeft or TopCenter or TopRight, the bottom of the text in the Label control can be found by this method:
The .GetHeight method returns the height, in pixels of the current font used by the Label.
If the .TextAlign is Middle or Bottom, then you need to do a slightly more complex calculation.
This same method will also work with the NumericUpDown control.