RichTextBox 用鼠标点击后得到完整的单词 [英] RichTextBox get full word after clicking on it with the mouse

查看:38
本文介绍了RichTextBox 用鼠标点击后得到完整的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在点击时获得整个词?我知道类似的问题已经存在并且我看过了,但是出于某种原因,40 行代码有奇怪且非常古老的解决方案.也许有更现代的东西?

How to get the whole word on click? I know that similar questions have already been and I looked, but for some reason there are strange and very old solutions for 40 lines of code. Maybe there is something more modern?

推荐答案

下面的代码演示了如何在 RichTextBox 控件中获取当前插入符号位置的单词.

The code below demonstrates how to get a word on the current caret position in the RichTextBox control.

XAML:

<Window ... >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <RichTextBox Grid.Row="0" x:Name="rtb" AllowDrop="True" VerticalScrollBarVisibility="Auto" Padding="2"
                    PreviewMouseUp="rtb_PreviewMouseUp" >
            <FlowDocument>
                <Paragraph FontSize="18" TextAlignment="Left" >

                   <!-- The RichTextBox control content should defined be here
                        or use the PASTE command to add some content... -->

                </Paragraph>
            </FlowDocument>
        </RichTextBox> 
        <Button Grid.Row="2" Click="Button_Click">Mark Current Word</Button>                   
    </Grid>
</Window>

下面的函数获取一个指向在指定方向解析文本的位置的指针,以检测单词边界.pattern 变量定义了一组可能的单词分隔符.

The function below gets a pointer to the position from which the text is parsed in the specified direction to detect a word boundary. The pattern variable defines set of possible a word delimiters.

public static class TextPointerExt
{
    public static TextPointer GetEdgeTextPointer(this TextPointer position, LogicalDirection direction)
    {
        string pattern = @" ,;.!""?"; // Delimiters 
        int step = direction == LogicalDirection.Forward ? 1 : -1;    
        for (; position != null;)
        {
            var text = position.GetTextInRun(direction);    
            int offset = 0;
            int i = direction == LogicalDirection.Forward ? 0 : text.Length - 1;

            for (; i >= 0 && i < text.Length; offset++, i += step)
            {
                if (pattern.Contains(text[i]))
                {
                    return position.GetPositionAtOffset(offset * step, LogicalDirection.Forward);
                }
            }

            position = position.GetPositionAtOffset(offset * step, LogicalDirection.Forward);
            for (TextPointer latest = position; ;)
            {
                if ((position = position.GetNextContextPosition(direction)) == null)
                    return latest;

                var context = position.GetPointerContext(direction);
                var adjacent = position.GetAdjacentElement(direction);    
                if (context == TextPointerContext.Text)
                {
                    if (position.GetTextInRun(direction).Length > 0)
                        break;
                }
                else if (context == TextPointerContext.ElementStart && adjacent is Paragraph)
                {
                    return latest;
                }
            }
        }
        return position;
    }
}

使用GetEdgeTextPointer()方法判断当前指向的词的例子:

Example of using the GetEdgeTextPointer() method to determine the current pointed word:

public void GetCurrentWord()
{          
    TextPointer current = rtb.CaretPosition;
    var start = current.GetEdgeTextPointer(LogicalDirection.Backward); // Word position before caret
    var end = current.GetEdgeTextPointer(LogicalDirection.Forward); // Word position after caret

    if (start is TextPointer && end is TextPointer && start.CompareTo(end) != 0)
    {
        TextRange textrange = new TextRange(start, end);
        // Print the found word to the debug window.
        System.Diagnostics.Debug.WriteLine(textrange.Text);

        // Select the found word
        rtb.Selection.Select(start, end);
    }
    rtb.Focus();
}

private void rtb_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    GetCurrentWord();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    GetCurrentWord();
}

这篇关于RichTextBox 用鼠标点击后得到完整的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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