windowsforms-118988-controls-and-libraries-rich-text-editor-examples-layout-how-to-determine-the-document-element-under-the-mouse-pointer.md
This code example uses the HitTestManager.HitTest method to find information about an element under the mouse pointer. The method returns a RichEditHitTestResult instance. This instance contains details about a specific document element (the RichEditHitTestResult.LayoutElement object) located under the test point. It lets you identify the element’s type and the area it occupies in the document.
In this example, the ToolTipController displays the element information. Handle the ToolTipController.GetActiveObjectInfo event. This makes the ToolTip show details about the hovered element. Here is an example:
void ToolTipController_GetActiveObjectInfo(object sender, ToolTipControllerGetActiveObjectInfoEventArgs e)
{
if (!e.SelectedControl.Equals(richEditControl1))
return;
// 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)
return;
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:
e.Info = new ToolTipControlInfo(element.Bounds, text, title, ToolTipIconType.Information);
}
}
string GetBounds(LayoutElement element)
{
return String.Format("\r\nBounds : {0}", element.Bounds);
}
string GetLocation(LayoutElement element)
{
while (element != null)
{
switch (element.Type)
{
case LayoutType.CommentsArea:
return "Comments Area Location";
case LayoutType.Header:
return "Header Location";
case LayoutType.Footer:
return "Footer Location";
}
element = element.Parent;
}
return "Page Location";
}
Private Sub ToolTipController_GetActiveObjectInfo(ByVal sender As Object, ByVal e As ToolTipControllerGetActiveObjectInfoEventArgs)
If Not e.SelectedControl.Equals(richEditControl1) Then
Return
End If
' Obtain the mouse cursor's layout position on the page and the current page index:
Dim pageLayoutPosition As PageLayoutPosition =
richEditControl1.ActiveView.GetDocumentLayoutPosition(e.ControlMousePosition)
If pageLayoutPosition Is Nothing Then
Return
End If
Dim point As 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:
e.Info = New ToolTipControlInfo(element.Bounds, text, title, ToolTipIconType.Information)
End If
End Sub
Private Function GetBounds(ByVal element As LayoutElement) As String
Return String.Format(ControlChars.CrLf & "Bounds : {0}", element.Bounds)
End Function
Private Function GetLocation(ByVal element As LayoutElement) As String
Do While element IsNot Nothing
Select Case element.Type
Case LayoutType.CommentsArea
Return "Comments Area Location"
Case LayoutType.Header
Return "Header Location"
Case LayoutType.Footer
Return "Footer Location"
End Select
element = element.Parent
Loop
Return "Page Location"
End Function
See Also