如何获取文本基线和标签水平边框之间的距离?

时间:2013-04-18 13:09:17

标签: c# winforms

我想获得文本基线与C#中标签底边之间的确切距离。我想要这个,因为我想在文本下画一条线(不想使用带下划线的字体,因为它非常紧密/靠近文本)。

这是我的尝试:

//This is placed in the custom label class
int dy = (int)((ClientRectangle.Height - Font.GetHeight())/2);

但这并不准确,dy返回约3,绘制到标签的线距文本基线太远。

1 个答案:

答案 0 :(得分:3)

获取标签的文本基线,假设您在自定义标签类中,在绘图处理程序中。

Font myFont = this.Font;
FontFamily ff = myFont.FontFamily;

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

积分here