WPF C#获取TextBlock的内联元素的大小和位置

时间:2018-11-28 15:15:45

标签: c# wpf textblock inlines

我使用此示例(https://siderite.blogspot.com/2016/03/how-to-draw-outlined-text-in-wpf-and.html#at2665784896)为TextBlock中的文本添加轮廓。但是,此示例不支持Inlines。

我尝试添加修改OnRender的功能,并迭代Inlines集合为每个Inline块构建一个Geometry。问题是我需要获取TextBlock中内联块的位置和大小,而不是TextBlock中整个文本的位置和大小。

这是我修改过的源代码。

protected override void OnRender(DrawingContext drawingContext)
{
    ensureTextBlock();
    base.OnRender(drawingContext);

    foreach (Inline inline in _textBlock.Inlines)
    {
        var textRange = new TextRange(inline.ContentStart, inline.ContentEnd);
        var formattedText = new FormattedText(
            textRange.Text,
            CultureInfo.CurrentUICulture,
            inline.FlowDirection,
            new Typeface(inline.FontFamily, inline.FontStyle, inline.FontWeight, inline.FontStretch),
            inline.FontSize, System.Windows.Media.Brushes.Black // This brush does not matter since we use the geometry of the text.
        );
        formattedText.SetTextDecorations(inline.TextDecorations);


        //*****************************************************************
        //This part get the size and position of the TextBlock
        // Instead I need to find a way to get the size and position of the Inline block

        //formattedText.TextAlignment = _textBlock.TextAlignment;
        //formattedText.Trimming = _textBlock.TextTrimming;

        formattedText.LineHeight = _textBlock.LineHeight;
        formattedText.MaxTextWidth = _textBlock.ActualWidth - _textBlock.Padding.Left - _textBlock.Padding.Right;
        formattedText.MaxTextHeight = _textBlock.ActualHeight - _textBlock.Padding.Top;// - _textBlock.Padding.Bottom;
        while (formattedText.Extent == double.NegativeInfinity)
        {
            formattedText.MaxTextHeight++;
        }

        // Build the geometry object that represents the text.
        var _textGeometry = formattedText.BuildGeometry(new System.Windows.Point(_textBlock.Padding.Left, _textBlock.Padding.Top));

        //*****************************************************************


        var textPen = new System.Windows.Media.Pen(Stroke, StrokeThickness * 2)
        {
            DashCap = PenLineCap.Round,
            EndLineCap = PenLineCap.Round,
            LineJoin = PenLineJoin.Round,
            StartLineCap = PenLineCap.Round
        };

        var boundsGeo = new RectangleGeometry(new Rect(0, 0, ActualWidth, ActualHeight));

        _clipGeometry = Geometry.Combine(boundsGeo, _textGeometry, GeometryCombineMode.Exclude, null);
        drawingContext.PushClip(_clipGeometry);
        drawingContext.DrawGeometry(System.Windows.Media.Brushes.Transparent, textPen, _textGeometry);
        drawingContext.Pop();
    }
}

我需要更改从TextBlock获取大小和位置的部分,以获取Inline块的大小和位置,但是我看不到任何可以获取该信息的Inline属性。有想法吗?

3 个答案:

答案 0 :(得分:0)

我找到了一种获取职位的方法:

inline.ElementStart.GetCharacterRect(LogicalDirection.Forward).Left
inline.ElementStart.GetCharacterRect(LogicalDirection.Forward).Top

然后,我以这种方式更改了构建几何的行:

double posLeft = inline.ElementStart.GetCharacterRect(LogicalDirection.Forward).Left;
double posTop = inline.ElementStart.GetCharacterRect(LogicalDirection.Forward).Top;

var _textGeometry = formattedText.BuildGeometry(new System.Windows.Point(posLeft, posTop));

我仍然对换行有问题,因为当换行文本时,仅在第一行中构建了几何。

答案 1 :(得分:0)

尝试一下

Rect rect = Rect.Union(inline.ElementStart.GetCharacterRect(LogicalDirection.Forward),                                                
                       inline.ElementEnd.GetCharacterRect(LogicalDirection.Backward));

感谢

答案 2 :(得分:-1)

使用TextBlock的LineHeight属性:

function myFunction() {
// This is your alert message
var r = confirm("Do you really want to leave?");
if (r == true) {
// Where to redirect your users
window.location.href = "anotherwebpage.html";
}
else {
}
document.getElementById("demo").innerHTML = x;
}

因此您可以以编程方式修改LineHeight值。