WPF控件 RichTextBox查找定位匹配字符

WPF控件 RichTextBox查找定位匹配字符

复制代码

private void Search_Click(object sender, RoutedEventArgs e)//查询定位文本
{
    List<TextRange> textRanges = FindWordFromPosition(richTextBox1.Document.ContentStart, txtSearch.Text);
    foreach (var range in textRanges)
    {
        range.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Red));
        //range.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
        //range.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.PowderBlue));
    }
}

复制代码

 

复制代码

List<TextRange> FindWordFromPosition(TextPointer position, string word)
{
    List<TextRange> matchingText = new List<TextRange>();
    while (position != null)
    {
        if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
        {
            //带有内容的文本
            string textRun = position.GetTextInRun(LogicalDirection.Forward);

            //查找关键字在这文本中的位置
            int indexInRun = textRun.IndexOf(word);
            int indexHistory = 0;
            while (indexInRun >= 0)
            {
                TextPointer start = position.GetPositionAtOffset(indexInRun + indexHistory);
                TextPointer end = start.GetPositionAtOffset(word.Length);
                matchingText.Add(new TextRange(start, end));

                indexHistory = indexHistory + indexInRun + word.Length;
                textRun = textRun.Substring(indexInRun + word.Length);//去掉已经采集过的内容
                indexInRun = textRun.IndexOf(word);//重新判断新的字符串是否还有关键字
            }
        }

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

复制代码

可以使用正则表达式来查找指定中文字符串,并通过设置字符格式来改变其颜色。以下是一个示例代码: ```csharp private void HighlightChineseText(string searchText, Color highlightColor) { // 创建一个正则表达式,用于匹配中文字符 Regex regex = new Regex(@"[\u4e00-\u9fa5]+"); // 获取RichTextBox中所有文本内容的范围 TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd); // 用于保存匹配到的文本范围和起始位置 List<Tuple<TextRange, int>> matches = new List<Tuple<TextRange, int>>(); // 在文本范围中查找匹配的中文字符串 TextPointer pointer = textRange.Start; while (pointer != null) { if (pointer.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text) { MatchCollection matchCollection = regex.Matches(pointer.GetTextInRun(LogicalDirection.Forward)); foreach (Match match in matchCollection) { TextPointer start = pointer.GetPositionAtOffset(match.Index); TextPointer end = start.GetPositionAtOffset(match.Length); matches.Add(new Tuple<TextRange, int>(new TextRange(start, end), start.GetOffsetToPosition(textRange.Start))); } } pointer = pointer.GetNextContextPosition(LogicalDirection.Forward); } // 为匹配到的文本范围设置字符格式,改变其颜色 foreach (var match in matches) { match.Item1.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(highlightColor)); } } ``` 在调用该方法时,传入要查找字符串和要改变的颜色即可: ```csharp string searchText = "中文字符串"; Color highlightColor = Colors.Red; HighlightChineseText(searchText, highlightColor); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值