英文:
How to get highlighted text's absolute position and length of RichTextBox?
问题
在WPF项目中,我使用RichTextBox
来显示高亮文本,如下所示。
> Hello,这是Tom Xue,很高兴见到你。
假设Hello和Tom都用颜色Brushes.Yellow
进行了高亮显示。
我需要获取它们的绝对起始索引和长度,即0,5
和15,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));
}
}
}
在给定的代码中,我们遍历了 RichTextBox
的 Document
属性,并尝试定位高亮的部分。关键点在于 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<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));
}
}
}
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论