Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF richTextBox问题_C#_Wpf_Richtextbox - Fatal编程技术网

C# WPF richTextBox问题

C# WPF richTextBox问题,c#,wpf,richtextbox,C#,Wpf,Richtextbox,如果一行文本被包装到另一行,我如何以编程方式确定字符串中的断点 示例:Input string=“这是对包装文本行的测试” 我需要确定的是被包装的单词行中的偏移量。在上述情况下,“文本”一词 当我从richTextBox中提取Xaml时,我会将原始文本展开 谢谢 Bob Kerlinger我发现的技巧使用TextPointer类及其GetCharacterRec方法 RichTextBox保存一个FlowDocument。流文档中的文本包含在运行对象中(有点简化,但它可以工作)。代码在第一次运行

如果一行文本被包装到另一行,我如何以编程方式确定字符串中的断点

示例:Input string=“这是对包装文本行的测试”

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

当我从richTextBox中提取Xaml时,我会将原始文本展开

谢谢


Bob Kerlinger

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

RichTextBox保存一个FlowDocument。流文档中的文本包含在运行对象中(有点简化,但它可以工作)。代码在第一次运行开始时找到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>
公共部分类窗口1:窗口
{
公共窗口1()
{
初始化组件();
}
专用无效检查(对象发送器、路由目标)
{
TextPointer=FindRun(inBox.Document);
字符串textAfterBreak=FindBreak(指针);
outBox.Text=textAfterBreak;
}
私有字符串FindBreak(文本指针)
{
Rect rectAtStart=pointer.GetCharacterRect(LogicalDirection.Forward);
指针=指针.GetNextInsertionPosition(LogicalDirection.Forward);
Rect currentRect=指针.GetCharacterRect(LogicalDirection.Forward);
while(currentRect.Bottom==rectAtStart.Bottom)
{
指针=指针.GetNextInsertionPosition(LogicalDirection.Forward);
currentRect=pointer.GetCharacterRect(LogicalDirection.Forward);
}
string textBeforeBreak=pointer.getTerimeRun(LogicalDirection.Backward);
字符串textAfterBreak=pointer.getTerimeRun(LogicalDirection.Forward);
休息后返回文本;
}
私有文本指针FindRun(FlowDocument文档)
{
text指针位置=document.ContentStart;
while(位置!=null)
{
如果(运行position.Parent)
打破
位置=位置.GetNextContextPosition(逻辑方向.Forward);
}
返回位置;
}
}
休息


但是
startOfFirstLine.GetLineStartPosition(1)
返回null

您试图使用此偏移量做什么?我需要在显示时提取合成文本,以便可以重新显示它,就像它在另一媒体上合成一样。此外,在某些情况下,我需要创建richTextBox不支持的悬挂缩进。我还需要做一些其他特殊的格式化。我一开始尝试过,但是startOfNexLine一直返回null。它似乎可以找到下一次运行的开始位置,但不会在运行对象中找到断点。我可能错过了什么。我知道我参加聚会有点晚了,但是你有没有想办法绕过StartoNextline返回空值?
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>
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;
}