如何获得文本基线和标签垂直边框之间的距离? [英] How to get the distance between the baseline of text and the vertical border of a label?

查看:99
本文介绍了如何获得文本基线和标签垂直边框之间的距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用Google搜索了,但要找到解决方案并不容易。



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



这是我的尝试:



  //  < span class =code-comment>这是放在自定义标签类中 
int dy =( int )((ClientRectangle.Height - Font.GetHeight())/ 2);





但这并不准确,dy返回约3并且标签上的线太远了从文本的基线开始。



我们非常感谢您的帮助。



谢谢。

解决方案

我认为正确答案应该是:你永远不能依赖在此值。相反,你应该以独立于此参数的方式检查你的设计。



你明白了吗?控制布局的这个细节没有记录,几乎没有记录。为什么?因为微软以及任何其他方都希望给它一些设计自由。库 System.Windows.Forms 依赖于当前原始Windows API中可用的Windows控件。这些控制的布局细节是一个变化的主题。如果Window演示文稿的样式再次改变怎么办?它可能需要改变基线的位置。你的设计就像打牌纸一样破碎。



怎么办?你有相反的方法。



首先,让你的设计能够容忍精细的布局变化。仅使用布局上的基本假设。让你的设计流畅。



相反的方法是:如果你关心基线的确切位置,你应该有理由。这意味着您希望将此坐标与其他坐标匹配。到底是什么?这个几何匹配元素不应该在另一个控件中,因为它不可靠,如上所述。因此,您应该创建自己的自定义控件,将所有协调的图形元素放在一起,并以自定义方式呈现所有这些元素。这样,你将介绍一个完全独立于Windows样式的设计元素。



祝你好运,

-SA

I''ve Googled but it''s not easy to find a solution.

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.

Your help would be highly appreciated.

Thanks.

解决方案

I think the correct answer should be: you never can rely on this value. Instead, you should review your design the way independent on this parameter.

Do you see the point? This detail of the control layout is not documented and will hardly be ever documented. Why? Because Microsoft, as well as any other party, want to give it some design freedom. The library System.Windows.Forms relies on the Windows controls available in the present-day raw Windows API. The fine detail of layout of these control is a subject of change. What if the style of Window presentation changes once again? It may require the change in the location of the base line. You design would break like a playing card house.

What to do? You have too opposite approaches.

First, make your design tolerant to fine layout changes. Use only the basic assumptions on the layout. Make your design fluid.

The opposite approach is: if you care about exact position of the base line, you should have a reason for that. It means you want to match this coordinate with something else. What, exactly? This geometrically matching element should not be in another control, as it would not be reliable, as I explained above. Hence, you should create your own, custom control, put all coordinated graphical elements together and render it all in a custom way. This way, you will introduce a design element fully independent of Windows styles.

Good luck,
—SA


这篇关于如何获得文本基线和标签垂直边框之间的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆