Highlighting/Coloring 个 wpf richtextbox 中的字符
Highlighting/Coloring charactars in a wpf richtextbox
public static void HighlightText(RichTextBox richTextBox,int startPoint,int endPoint, Color color)
{
//Trying to highlight charactars here
}
Startpoint 是应该突出显示的第一个字符,endPoint 是最后一个字符。
我已经在网上安静地搜索了一段时间,但我仍然没有想出如何解决我的问题。
我希望你们中的任何人都知道如何解决这个问题。
大致思路如下:
public static void HighlightText(RichTextBox richTextBox, int startPoint, int endPoint, Color color)
{
//Trying to highlight charactars here
TextPointer pointer = richTextBox.Document.ContentStart;
TextRange range = new TextRange(pointer.GetPositionAtOffset(startPoint), pointer.GetPositionAtOffset(endPoint));
range.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(color));
}
但是您需要遍历文档以获得正确的文本位置,因为偏移索引号与字符数不匹配。一些字符可能代表多个偏移位置。
这是执行此操作的方法。我不知道有什么方法可以在不循环遍历整个文档的情况下执行此操作。
public static void HighlightText(RichTextBox richTextBox, int startPoint, int endPoint, Color color)
{
//Trying to highlight charactars here
TextPointer pointer = richTextBox.Document.ContentStart;
TextPointer start = null, end = null;
int count = 0;
while (pointer != null)
{
if (pointer.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
{
if (count == startPoint) start = pointer.GetInsertionPosition(LogicalDirection.Forward);
if (count == endPoint) end = pointer.GetInsertionPosition(LogicalDirection.Forward);
count++;
}
pointer = pointer.GetNextInsertionPosition(LogicalDirection.Forward);
}
if (start == null) start = richTextBox.Document.ContentEnd;
if (end == null) end = richTextBox.Document.ContentEnd;
TextRange range = new TextRange(start, end);
range.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(color));
}
public static void HighlightText(RichTextBox richTextBox,int startPoint,int endPoint, Color color)
{
//Trying to highlight charactars here
}
Startpoint 是应该突出显示的第一个字符,endPoint 是最后一个字符。
我已经在网上安静地搜索了一段时间,但我仍然没有想出如何解决我的问题。 我希望你们中的任何人都知道如何解决这个问题。
大致思路如下:
public static void HighlightText(RichTextBox richTextBox, int startPoint, int endPoint, Color color)
{
//Trying to highlight charactars here
TextPointer pointer = richTextBox.Document.ContentStart;
TextRange range = new TextRange(pointer.GetPositionAtOffset(startPoint), pointer.GetPositionAtOffset(endPoint));
range.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(color));
}
但是您需要遍历文档以获得正确的文本位置,因为偏移索引号与字符数不匹配。一些字符可能代表多个偏移位置。
这是执行此操作的方法。我不知道有什么方法可以在不循环遍历整个文档的情况下执行此操作。
public static void HighlightText(RichTextBox richTextBox, int startPoint, int endPoint, Color color)
{
//Trying to highlight charactars here
TextPointer pointer = richTextBox.Document.ContentStart;
TextPointer start = null, end = null;
int count = 0;
while (pointer != null)
{
if (pointer.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
{
if (count == startPoint) start = pointer.GetInsertionPosition(LogicalDirection.Forward);
if (count == endPoint) end = pointer.GetInsertionPosition(LogicalDirection.Forward);
count++;
}
pointer = pointer.GetNextInsertionPosition(LogicalDirection.Forward);
}
if (start == null) start = richTextBox.Document.ContentEnd;
if (end == null) end = richTextBox.Document.ContentEnd;
TextRange range = new TextRange(start, end);
range.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(color));
}