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

huangapple go评论102阅读模式
英文:

How to get highlighted text's absolute position and length of RichTextBox?

问题

在WPF项目中,我使用RichTextBox来显示高亮文本,如下所示。

> Hello,这是Tom Xue,很高兴见到你。

假设HelloTom都用颜色Brushes.Yellow进行了高亮显示。

我需要获取它们的绝对起始索引和长度,即0,515,3。有了这些数据,我可以重建这个富文本。

有什么建议吗?

英文:

In WPF project, I use RichTextBox to show highlighted text, like below.

> Hello, this is Tom Xue, nice to meet you.

Let's assume Hello and Tom are both highlighted with color Brushes.Yellow.

And I need to get their absolute start index and length, that is 0,5 and 15,3. With these data, I can reconstruct this rich text.

Any advise?

答案1

得分: 1

你可以使用 TextPointer 类与 GetPropertyValue 方法。考虑以下代码中的 MainRichTextBox

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

namespace WpfApp1
{
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();
            // ... 一些文本和高亮的初始化 ...
		}

		private void Button_Click(object sender, RoutedEventArgs e)
		{
			int getRenderedIndex(TextPointer pointer)
			{
				return new TextRange(
					MainRichTextBox.Document.ContentStart,
					pointer
				).Text.Length;
			}

			var highlightedSegments = new List<Tuple<int, int>>();
			int highlightStartIndex = -1;
			TextPointer current = MainRichTextBox.Document.ContentStart;
			while (current != null)
			{
				TextPointer next = current.GetNextContextPosition(LogicalDirection.Forward);
				if (next != null && (new TextRange(current, next).GetPropertyValue(TextElement.BackgroundProperty) as SolidColorBrush)?.Color == Colors.Yellow)
				{
					if (highlightStartIndex < 0)
						highlightStartIndex = getRenderedIndex(current);
				}
				else
				{
					if (highlightStartIndex >= 0)
						highlightedSegments.Add(new Tuple<int, int>(highlightStartIndex, getRenderedIndex(current) - highlightStartIndex));
					highlightStartIndex = -1;
				}

				current = next;
			}

			MessageBox.Show(string.Join("\n", highlightedSegments));
		}
	}
}

在给定的代码中,我们遍历了 RichTextBoxDocument 属性,并尝试定位高亮的部分。关键点在于 getRenderedIndex 内部函数,它根据文本表示返回输入指针的索引。

最后,单击按钮后,高亮的部分将被显示。

英文:

You can use the TextPointer class along with the GetPropertyValue method. Consider the MainRichTextBox in the following code:

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

namespace WpfApp1
{
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();
            // ... some text and highlighting initialization ...
		}

		private void Button_Click(object sender, RoutedEventArgs e)
		{
			int getRenderedIndex(TextPointer pointer)
			{
				return new TextRange(
					MainRichTextBox.Document.ContentStart,
					pointer
				).Text.Length;
			}

			var highlightedSegments = new List&lt;Tuple&lt;int, int&gt;&gt;();
			int highlightStartIndex = -1;
			TextPointer current = MainRichTextBox.Document.ContentStart;
			while (current != null)
			{
				TextPointer next = current.GetNextContextPosition(LogicalDirection.Forward);
				if (next != null &amp;&amp; (new TextRange(current, next).GetPropertyValue(TextElement.BackgroundProperty) as SolidColorBrush)?.Color == Colors.Yellow)
				{
					if (highlightStartIndex &lt; 0)
						highlightStartIndex = getRenderedIndex(current);
				}
				else
				{
					if (highlightStartIndex &gt;= 0)
						highlightedSegments.Add(new Tuple&lt;int, int&gt;(highlightStartIndex, getRenderedIndex(current) - highlightStartIndex));
					highlightStartIndex = -1;
				}

				current = next;
			}

			MessageBox.Show(string.Join(&quot;\n&quot;, highlightedSegments));
		}
	}
}

In the given code, we traversed the Document property of the RichTextBox and attempted to locate highlighted segments. The crucial point lies in the getRenderedIndex inner function, which returns the index of the input pointer based on the textual representation.

Finally, upon clicking the button, the highlighted segments will be displayed:

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

huangapple
  • 本文由 发表于 2023年7月27日 22:56:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定