6

如果一行文本被换行到另一行,我如何以编程方式确定字符串中被破坏的点。

示例:输入字符串 =“这是对换行文本的测试”。

      Based on the width of the richTextBox it could display:


            This is a test of a wrapped line of
            text.

我需要确定的是被包装的单词行中的偏移量。在上述情况下,单词“文本”。

当我从richTextBox 中提取Xaml 时,我得到了解包的原始文本。

谢谢,

鲍勃·克林格

4

3 回答 3

2

我发现的技巧使用 TextPointer 类及其 GetCharacterRec 方法。

RichTextBox 包含一个 FlowDocument。流文档中的文本包含在 Run 对象中(有点简化,但它有效)。代码在第一次运行开始时找到 TextPointer。然后它获取第一个字符的边界矩形。接下来,代码一次前进一个字符,获取一个新的边界矩形,并检查新矩形的底部是否与原始矩形不同。如果底部不同,那么我们就在新的一行。然后 TextPointer 可以在中断之前或之后获取文本。

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    private void inspect(object sender, RoutedEventArgs e)
    {
        TextPointer pointer = FindRun(inBox.Document);

        string textAfterBreak = FindBreak(pointer);

        outBox.Text = textAfterBreak;
    }

    private string FindBreak(TextPointer pointer)
    {
        Rect rectAtStart = pointer.GetCharacterRect(LogicalDirection.Forward);

        pointer = pointer.GetNextInsertionPosition(LogicalDirection.Forward);
        Rect currentRect = pointer.GetCharacterRect(LogicalDirection.Forward);

        while (currentRect.Bottom == rectAtStart.Bottom)
        {
            pointer = pointer.GetNextInsertionPosition(LogicalDirection.Forward);
            currentRect = pointer.GetCharacterRect(LogicalDirection.Forward);
        }

        string textBeforeBreak = pointer.GetTextInRun(LogicalDirection.Backward);
        string textAfterBreak = pointer.GetTextInRun(LogicalDirection.Forward);

        return textAfterBreak;
    }

    private TextPointer FindRun(FlowDocument document)
    {
        TextPointer position = document.ContentStart;

        while (position != null)
        {
            if (position.Parent is Run)
                break;

            position = position.GetNextContextPosition(LogicalDirection.Forward);
        }

        return position;
    }
}

<Window x:Class="LineBreaker.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <RichTextBox Grid.Row="0" Name="inBox">
        </RichTextBox>
        <Button Grid.Row="1" Click="inspect">Find Break</Button>
        <TextBox Name="outBox" Grid.Row="2"/>
    </Grid>
</Window>
于 2008-11-24T00:29:24.893 回答
1

http://msdn.microsoft.com/en-us/library/system.windows.documents.textpointer.getlinestartposition.aspx

TextPointer startOfFirstLine = richTextBox.Document.ContentStart;
TextPointer startOfNextLine = startOfFirstLine.GetLineStartPosition(1);
if(startOfNextLine != null)
{
     // At this point what you do with the TextPointer depends on what you define as the position of text.
     // If you want to find out how many characters are on the first line ...
    int firstLineCharacterCount = new TextRange(startOfFirstLine, startOfNextLine).Text.Length;
}
于 2008-11-24T02:50:32.613 回答
1

startOfFirstLine.GetLineStartPosition(1)返回null

于 2009-01-18T05:28:12.660 回答