在WPF C中的移动中突出显示richtextbox中的单词#

时间:2014-09-15 10:05:44

标签: c# wpf richtextbox

我想在richtextbox中突出显示单词。当用户将鼠标悬停在richtextbox中的单词上时,该单词应突出显示。

以下是我目前的代码。错误:最后一个高亮点是好的,但是起点不准确,当我放置一个新的paragrah或新行时,然后开始点离开预期结果。

 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;
    }
没有换行时

结果。 enter image description here

当有3个新换行符时,

会产生结果。 enter image description here

更新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());

0 个答案:

没有答案