You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取WPF中RichTextBox高亮文本的绝对位置与长度?

WPF RichTextBox 获取高亮文本的起始索引与长度方案

核心思路

遍历RichTextBox的FlowDocument结构,识别出使用Brushes.Yellow高亮的文本片段,通过累加前面所有文本的长度计算每个高亮片段的绝对起始索引,最终收集起始索引和长度信息。

实现代码

using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

// 存储高亮文本的位置信息
public class HighlightedTextInfo
{
    public int StartIndex { get; set; }
    public int Length { get; set; }
}

public static class RichTextBoxHelper
{
    public static List<HighlightedTextInfo> GetYellowHighlightedPositions(RichTextBox richTextBox)
    {
        var highlightedInfos = new List<HighlightedTextInfo>();
        int currentOffset = 0;

        // 遍历所有段落,支持多段落场景
        foreach (Block block in richTextBox.Document.Blocks)
        {
            if (block is Paragraph paragraph)
            {
                ProcessInlines(paragraph.Inlines, ref currentOffset, highlightedInfos);
                // 段落间默认累加换行符长度,可按需调整
                currentOffset += 1;
            }
        }

        return highlightedInfos;
    }

    private static void ProcessInlines(InlineCollection inlines, ref int currentOffset, List<HighlightedTextInfo> highlightedInfos)
    {
        foreach (Inline inline in inlines)
        {
            if (inline is Run run)
            {
                // 判断是否为黄色高亮,若引用不一致可改用颜色值比较
                if (Equals(run.Foreground, Brushes.Yellow))
                {
                    highlightedInfos.Add(new HighlightedTextInfo
                    {
                        StartIndex = currentOffset,
                        Length = run.Text.Length
                    });
                }
                currentOffset += run.Text.Length;
            }
            // 处理嵌套容器(如Span、Bold、Italic等)
            else if (inline is Span span)
            {
                ProcessInlines(span.Inlines, ref currentOffset, highlightedInfos);
            }
            else if (inline is Bold bold)
            {
                ProcessInlines(bold.Inlines, ref currentOffset, highlightedInfos);
            }
            else if (inline is Italic italic)
            {
                ProcessInlines(italic.Inlines, ref currentOffset, highlightedInfos);
            }
            // 可扩展支持其他Inline类型
        }
    }
}

使用示例

// 假设你的RichTextBox实例名为rtbContent
var highlightedList = RichTextBoxHelper.GetYellowHighlightedPositions(rtbContent);
foreach (var info in highlightedList)
{
    // 输出结果示例:起始索引: 0, 长度: 5;起始索引: 15, 长度: 3
    Debug.WriteLine($"起始索引: {info.StartIndex}, 长度: {info.Length}");
}

注意事项

  1. 多段落处理:代码已支持多段落场景,段落间的换行符长度默认累加1,可根据实际显示需求调整。
  2. 颜色匹配问题:若高亮颜色是通过XAML资源或动态创建的SolidColorBrush,可能出现引用不相等的情况,此时可改用颜色值比较:
    if (run.Foreground is SolidColorBrush brush && brush.Color == Colors.Yellow)
    
  3. 复杂结构支持:代码已处理Span、Bold、Italic等嵌套容器,若有其他自定义容器类型,可扩展ProcessInlines方法的判断逻辑。

内容的提问来源于stack exchange,提问作者Tom Xue

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 20:50:22