本文介绍了绘制完全适合矩形的tekst的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于画布的绘图控件.
用户可以通过在左上角按下鼠标按钮并将鼠标拖动到右下角位置来绘制矩形.如果用户放开鼠标按钮,则会在用户执行该操作的确切位置上绘制矩形.
现在,我也需要在此绘制控件上绘制文本,这是相同的过程,其思想是,当用户放开鼠标时,该矩形将被tekst标签"填充,并且该矩形正好填充了周围长方形.如果我将tekst更改为"Lgl",则应重新应用格式,因为"g"很可能具有负底端.我怎样才能做到这一点? (不是重新格式化事件,那是正常的事情,而是周围矩形本身的拟合)
如果我说fontsize为50,则产生的大小很可能更大.
我已经使用以下代码根据在网上发现的内容进行了一些调查:

I have a drawingcontrol based on a canvas.
The user is able to draw a rectangle by pressing the mouse button on the topleft position and dragging the mouse to the bottom right position. If the user lets go of the mouse button, the rectangle is drawn on the exact position that the user performed that action.
Now I need to draw text on this drawing control too, and it is the same procedure, the idea is that when the user lets go of the mouse the rectangle is filled in with the tekst "label", and that it exactly fills the surrounding rectangle. If I change the tekst to say "Lgl" the formatting should be reapplied because most likely the ''g'' has a negative bottom bearing. How can I make this happen ? (Not the reformatting event, that is happening ok, but the fitting in the surrounding rectangle itself)
If I say the fontsize is 50, the resulting size is most likely bigger.
I used following code to do some investigation already based on what I found on the net :

public class HeightFontSizeConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string text = (string)values[0];
            double desiredHeight = (double)values[1];
            FontFamily family = (FontFamily)values[2];
            FontStyle style = (FontStyle)values[3];
            FontWeight weight = (FontWeight)values[4];
            FontStretch stretch = FontStretches.Normal;
            if (desiredHeight == 0) return (double)0;
            Typeface face = new Typeface(family,style,weight,stretch);
            GlyphTypeface gtf;
            if (face.TryGetGlyphTypeface(out gtf))
            {
                for (int i = 0; i < text.Length; ++i)
                {
                    ushort index = gtf.CharacterToGlyphMap[text[i]];
                    double h = gtf.AdvanceHeights[index];
                    double b = gtf.BottomSideBearings[index];
                    double t = gtf.TopSideBearings[index];
                    double baseline = gtf.Baseline;
                    double height = gtf.Height;
                    Debug.WriteLine("in HeightFontSizeConverter {0} baseline={1} height={2} advh={3} bsb={4} tsb={5}",text[i], baseline, height, h, b,t);
                }
                return desiredHeight;
            }
            else return (double)0;
        }


基本上,问题是我似乎没有在GlyphTypeface中找到参数的含义.我对左侧轴承和右侧轴承不感兴趣,我感觉它与顶部轴承和底部轴承有关,但是它们从何处开始.
我不想在tekst上方有任何空间,也不想在tekst下方有任何空间,它应该完全适合矩形.


The problem basically is that I don''t seem to find the meaning of the parameters in GlyphTypeface. I am not interested in left side bearing and right side bearing, I have the feeling it has to do with top side bearing and bottom side bearing but where do they start exactly.
I don''t want any space above the tekst and I do not want any space below the tekst, it should fit the rectangle exactly.

推荐答案


TextBox myText = new TextBox();
Rect textRext = myText.GetRectFromCharacterIndex(myText.Text.Length);



http: //blogs.microsoft.co.il/blogs/tamir/archive/2007/03/12/Text-length-measurement_3F00_-It_2700_s-really-easy-with-WPF.aspx [ ^ ]



http://blogs.microsoft.co.il/blogs/tamir/archive/2007/03/12/Text-length-measurement_3F00_-It_2700_s-really-easy-with-WPF.aspx[^]


这篇关于绘制完全适合矩形的tekst的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:32