如何获取文本基线和标签水平边框之间的距离?(How to get the distance between the baseline of text and the horizontal border

编程入门 行业动态 更新时间:2024-10-27 00:24:04
如何获取文本基线和标签水平边框之间的距离?(How to get the distance between the baseline of text and the horizontal border of a label?)

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

这是我的尝试:

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

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

I want to get the exact distance between the baseline of the text and the bottom border of a label in C#. I want this because I want to draw a line under the text (don't want to use underlined font, because it's so tight/close to the text).

Here is my try:

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

But it's not exact, the dy returns about 3 and the line drawn to the label is too far from the baseline of the text.

最满意答案

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

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;

信用就在这里 。

To get text baseline for a label, assuming you are inside the custom label class, in the drawing handler.

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;

Credit goes here.

更多推荐

文本,text,dy,label,want,电脑培训,计算机培训,IT培训"/> <meta name="descrip

本文发布于:2023-08-07 20:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465641.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:基线   边框   文本   距离   水平

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!