如何获取 Label 和 NumericUpDown 中文本基线的位置?

发布于 2024-07-24 10:34:00 字数 101 浏览 10 评论 0原文

我正在尝试按文本基线对齐 LabelNumericUpDown 。 我是用代码而不是设计师来完成的。 如何获取文本基线的位置?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸印温柔 2024-07-31 10:34:00

// 以坐标 (pt.X, pt.Y) 处的基线渲染文本:

Font myFont = Label1.Font;
FontFamily ff = myFont.FontFamily;

float lineSpace = ff.GetLineSpacing(myFont.Style);
float ascent = ff.GetCellAscent(myFont.Style);
float baseline =  myFont.GetHeight(ev.Graphics) * ascent / lineSpace;

PointF renderPt = new PointF(pt.X, pt.Y  - baseline));
ev.Graphics.DrawString("Render this string", myFont, textBrush, renderPt);

// to render text with baseline at coordinates (pt.X, pt.Y) :

Font myFont = Label1.Font;
FontFamily ff = myFont.FontFamily;

float lineSpace = ff.GetLineSpacing(myFont.Style);
float ascent = ff.GetCellAscent(myFont.Style);
float baseline =  myFont.GetHeight(ev.Graphics) * ascent / lineSpace;

PointF renderPt = new PointF(pt.X, pt.Y  - baseline));
ev.Graphics.DrawString("Render this string", myFont, textBrush, renderPt);
ゃ懵逼小萝莉 2024-07-31 10:34:00

对于Label控件,可以这样获取文本底部的位置:

找到Label控件中文本的底部:

dim btmOfText  as single
btmOfText = Label1.Font.GetHeight + Label1.Top

假设.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:

dim btmOfText  as single
btmOfText = Label1.Font.GetHeight + Label1.Top

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文