Back to Devexpress

How to: Determine the Document Element under the Mouse Pointer

wpf-118987-controls-and-libraries-rich-text-editor-examples-layout-how-to-determine-the-document-element-under-the-mouse-pointer.md

latest5.1 KB
Original Source

How to: Determine the Document Element under the Mouse Pointer

  • Dec 18, 2025
  • 3 minutes to read

The following code example shows how to use the HitTestManager.HitTest method to get information about the element under the mouse pointer. The method returns the RichEditHitTestResult instance containing an information about the layout element (RichEditHitTestResult.LayoutElement) under the test point. It allows you to get the element’s type (LayoutElement.Type) and the area it occupies in the document (LayoutElement.Bounds).

In this example, the ToolTip instance is used to display the retrieved information. Perform hit testing and pass the information to the tooltip content in the RichEditControl.MouseMove event handler, as shown below:

xaml
<dxre:RichEditControl x:Name="richEditControl1"                      
                      MouseMove="RichEditControl_MouseMove" 
                      CommandBarStyle="Ribbon"/>
csharp
//Obtain the mouse cursor's layout position on the page and the current page index:
Point wPoint = e.GetPosition(richEditControl1);

PageLayoutPosition pageLayoutPosition = richEditControl1.ActiveView.GetDocumentLayoutPosition
    (new System.Drawing.Point((int)wPoint.X, (int)wPoint.Y));
if (pageLayoutPosition == null)
    return;

System.Drawing.Point point = pageLayoutPosition.Position;
int pageIndex = pageLayoutPosition.PageIndex;
LayoutPage layoutPage = richEditControl1.DocumentLayout.GetPage(pageIndex);

//Create a HitTestManager instance: 
HitTestManager hitTest = new HitTestManager(richEditControl1.DocumentLayout);

//Perform the hit test and pass the result to the RichEditHitTestResult object:
RichEditHitTestResult result = hitTest.HitTest(layoutPage, point);
if (result != null)
{
    //Retrieve the current layout element type:
    LayoutElement element = result.LayoutElement;
    string text = element.Type.ToString();

    //Obtain the the text character and its bounds under the mouse position              
    if (element.Type == LayoutType.CharacterBox)
    {
        text += String.Format(" : \"{0}\"", (element as CharacterBox).Text);
        text += GetBounds(element);
        if (element.Parent.Type == LayoutType.PlainTextBox)
        {
            text += String.Format("\r\nPlainTextBox : \"{0}\"", (element.Parent as PlainTextBox).Text);
            text += GetBounds(element.Parent);
        }
    }
    else
    {
        //Get the hovered element's bounds:
        text += GetBounds(element);
    }

    //Get the element's location:
    string title = GetLocation(element);

    //Display all retrieved information in the tooltip:
    toolTip.IsOpen = true;
    toolTip.Content = text+"\r\n" + title;

}
vb
'Obtain the mouse cursor's layout position on the page and the current page index:
Dim wPoint As Point = e.GetPosition(richEditControl1)

Dim pageLayoutPosition As PageLayoutPosition = richEditControl1.ActiveView.GetDocumentLayoutPosition(New System.Drawing.Point(CInt((wPoint.X)), CInt((wPoint.Y))))
If pageLayoutPosition Is Nothing Then
    Return
End If

Dim point As System.Drawing.Point = pageLayoutPosition.Position
Dim pageIndex As Integer = pageLayoutPosition.PageIndex
Dim layoutPage As LayoutPage = richEditControl1.DocumentLayout.GetPage(pageIndex)

'Create a HitTestManager instance: 
Dim hitTest As New HitTestManager(richEditControl1.DocumentLayout)

'Perform the hit test and pass the result to the RichEditHitTestResult object:
Dim result As RichEditHitTestResult = hitTest.HitTest(layoutPage, point)
If result IsNot Nothing Then
    'Retrieve the current layout element type:
    Dim element As LayoutElement = result.LayoutElement
    Dim text As String = element.Type.ToString()

    'Obtain the the text character and its bounds under the mouse position              
    If element.Type = LayoutType.CharacterBox Then
        text &= String.Format(" : ""{0}""", (TryCast(element, CharacterBox)).Text)
        text &= GetBounds(element)
        If element.Parent.Type = LayoutType.PlainTextBox Then
            text &= String.Format(ControlChars.CrLf & "PlainTextBox : ""{0}""", (TryCast(element.Parent, PlainTextBox)).Text)
            text &= GetBounds(element.Parent)
        End If
    Else
        'Get the hovered element's bounds:
        text &= GetBounds(element)
    End If

    'Get the element's location:
    Dim title As String = GetLocation(element)

    'Display all retrieved information in the tooltip:
    toolTip.IsOpen = True
    toolTip.Content = text &ControlChars.CrLf & title

End If