C# 在WPF C中的moveover上突出显示richtextbox中的单词#
我试图突出显示richtextbox中的单词。当用户将鼠标悬停在richtextbox中的单词上时,该单词应高亮显示 下面是我到目前为止的代码。错误:最后一个高光点是好的,但起点不准确,当我放置一个新的段落或新行时,起点就会偏离预期的结果C# 在WPF C中的moveover上突出显示richtextbox中的单词#,c#,wpf,richtextbox,C#,Wpf,Richtextbox,我试图突出显示richtextbox中的单词。当用户将鼠标悬停在richtextbox中的单词上时,该单词应高亮显示 下面是我到目前为止的代码。错误:最后一个高光点是好的,但起点不准确,当我放置一个新的段落或新行时,起点就会偏离预期的结果 private void richTextBox1_MouseMove(object sender, MouseEventArgs e) { richTextBox1.Focus(); selectWordOnMou
private void richTextBox1_MouseMove(object sender, MouseEventArgs e)
{
richTextBox1.Focus();
selectWordOnMouseOver();
}
public void selectWordOnMouseOver()
{
if (richTextBox1 == null)
return;
TextPointer cursurPosition = richTextBox1.GetPositionFromPoint(Mouse.GetPosition(richTextBox1), false);
if (cursurPosition == null)
return;
int offset = richTextBox1.Document.ContentStart.GetOffsetToPosition(cursurPosition);
// offset = offset;
//MessageBox.Show("Offset = " + offset.ToString());
int spaceAfter = FindSpaceAfterWordFromPosition(cursurPosition, " ");
int spaceBefore = FindSpaceBeforeWordFromPosition(cursurPosition, " ");
TextPointer wholeText = richTextBox1.Document.ContentStart;
TextPointer start = wholeText.GetPositionAtOffset(spaceBefore);
TextPointer end = wholeText.GetPositionAtOffset(spaceAfter);
if (start == null || end == null )
return;
richTextBox1.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Black);
richTextBox1.Selection.Select(start, end);
// MessageBox.Show("Mouse Over On = " + offset.ToString() + ": Word Start = " + (spaceBefore).ToString() + ": Word End = " + (spaceAfter).ToString() + " : Word is = " + richTextBox1.Selection.Text);
}
int FindSpaceBeforeWordFromPosition(TextPointer position, string word)
{
while (position != null)
{
if (position.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.Text)
{
string textRun = position.GetTextInRun(LogicalDirection.Backward);
// Find the starting index of any substring that matches "word".
int spaceIndexBeforeMouseOver = textRun.LastIndexOf(word);
return spaceIndexBeforeMouseOver;
}
position = position.GetNextContextPosition(LogicalDirection.Backward);
}
// position will be null if "word" is not found.
return 0;
}
int FindSpaceAfterWordFromPosition(TextPointer position, string word)
{
while (position != null)
{
if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
{
string textRun = position.GetTextInRun(LogicalDirection.Forward);
// Find the starting index of any substring that matches "word".
int spaceIndexAfterMouseOver = textRun.IndexOf(word);
int lastIndexAfterMouseOverIndex = textRun.Count();
int mouseOverIndex = richTextBox1.Document.ContentStart.GetOffsetToPosition(position);
if (spaceIndexAfterMouseOver >= 0)
{
return spaceIndexAfterMouseOver + mouseOverIndex;
}
else//if space index not found the select to to the last word of text box
return mouseOverIndex + lastIndexAfterMouseOverIndex;
}
position = position.GetNextContextPosition(LogicalDirection.Forward);
}
// position will be null if "word" is not found.
return 0;
}
没有新行时的结果。
结果当有3个新行馈送时。
更新1:
我最近在测试后发现一件事,我得到了错误的补偿
Point nMousePositionCoordinate = Mouse.GetPosition(richTextBox1);
// System.Diagnostics.Debug.WriteLine(nMousePositionCoordinate.ToString());
TextPointer cursurPosition = richTextBox1.GetPositionFromPoint(nMousePositionCoordinate, false);
if (cursurPosition == null)
return;
int offset = richTextBox1.Document.ContentStart.GetOffsetToPosition(cursurPosition);
System.Diagnostics.Debug.WriteLine(offset.ToString());
调试提示:使用System.Diagnostics.Debug.WriteLine()而不是MessageBox.Show();您可以在输出窗口(调试->窗口->输出)中查看消息;弹出一个关于MouseMove事件的对话框是痛苦的。谢谢你的好提示。。。。