Skip to main content

How to: Determine the Document Element under the Mouse Pointer

  • 3 minutes to read

The following code example shows how to use the HitTestManager.HitTest method to get information about the element located under the mouse pointer. The method returns the RichEditHitTestResult instance, containing the information about a specific document element (RichEditHitTestResult.LayoutElement) located 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 ToolTipController is used to show the element information. Handle the ToolTipController.GetActiveObjectInfo event to make the ToolTip display the information about the hovered element, as shown below:

View Example

if (!e.SelectedControl.Equals(richEditControl1))

// Obtain the mouse cursor's layout position on the page and the current page index:
PageLayoutPosition pageLayoutPosition = richEditControl1.ActiveView.GetDocumentLayoutPosition(e.ControlMousePosition);
if (pageLayoutPosition == null)

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);
        // 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:
    e.Info = new ToolTipControlInfo(element.Bounds, text, title, ToolTipIconType.Information);

See Also