How do I get the position of the text baseline in a Label and a NumericUpDown?

瘦欲@ 提交于 2019-11-28 13:25:41

// 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);

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!