10

我正在尝试通过它们的文本基线对齐 aLabel和 a 。NumericUpDown我是在代码中做的,而不是设计师。如何获取文本基线的位置?

4

2 回答 2

16

// 在坐标 (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);
于 2011-11-28T01:38:03.117 回答
2

对于 Label 控件,您可以通过以下方式获取文本底部的位置:

假设 .TextAlign 设置为 TopLeft 或 TopCenter 或 TopRight,则可以通过此方法找到 Label 控件中文本的底部:

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

.GetHeight 方法返回标签使用的当前字体的高度,以像素为单位。
如果 .TextAlign 是 Middle 或 Bottom,那么您需要进行稍微复杂的计算。

同样的方法也适用于 NumericUpDown 控件。

于 2009-06-17T14:56:01.513 回答