Back to Devexpress

RichEditControl.GetPositionFromPoint(PointF) Method

wpf-devexpress-dot-xpf-dot-richedit-dot-richeditcontrol-dot-getpositionfrompoint-x28-system-dot-drawing-dot-pointf-x29.md

latest5.0 KB
Original Source

RichEditControl.GetPositionFromPoint(PointF) Method

Gets the position in the document closest to the specified point.

Namespace : DevExpress.Xpf.RichEdit

Assembly : DevExpress.Xpf.RichEdit.v25.2.dll

NuGet Package : DevExpress.Wpf.RichEdit

Declaration

csharp
public DocumentPosition GetPositionFromPoint(
    PointF clientPoint
)
vb
Public Function GetPositionFromPoint(
    clientPoint As PointF
) As DocumentPosition

Parameters

NameTypeDescription
clientPointPointF

A PointF structure specifying the location for which the position is retrieved. The point coordinates are measured in documents units.

|

Returns

TypeDescription
DocumentPosition

A DocumentPosition object representing a position in the document.

|

Remarks

The GetPositionFromPoint method returns the closest document position for a specified point or null reference ( Nothing in Visual Basic). The coordinate system used is a standard one, where the origin is located at the upper left corner of the RichEditControl. Use the Units.PixelsToDocuments method to translate coordinates.

The Point Coordinate Units

The measurement logic of RichEditControl uses the units specified by the RichEditControl.Unit property. If this property is set to a value other than DocumentUnit.Document, convert the point coordinates to the specified units. The Units class method allows you to convert between measurement units.

Example

This code snippet illustrates handling the MouseMove event of the RichEditControl and using the RichEditControl.GetPositionFromPoint method to obtain the corresponding position in the document. If the position under the mouse cursor belongs to a bookmark, this bookmark is selected using the BookmarkCollection.Select method, and its name and text fragment are displayed in the text box below.

csharp
void richEditControl1_MouseMove(object sender, MouseEventArgs e)
{
    Point point = e.GetPosition((UIElement)richEditControl1);
    System.Drawing.PointF pt = new System.Drawing.PointF(Units.PixelsToDocumentsF((float)point.X,richEditControl1.DpiX),
        Units.PixelsToDocumentsF((float)point.Y, richEditControl1.DpiY));
    DocumentPosition pos = richEditControl1.GetPositionFromPoint(pt);

    BookmarkCollection bmCollection = richEditControl1.Document.Bookmarks;
    textBlock1.Text = String.Empty; //Clear a text block displaying bookmark info

    if (pos != null)
    {
        foreach (Bookmark bm in bmCollection)
        {
            if (bm.Range.Contains(pos))
            {
                Bookmark bmHovered = bm;
                richEditControl1.Document.SelectBookmark(bmHovered);
                ShowBookmarkInTextBlock(bmHovered);
                break;
            }
        }
    }
}
vb
Private Sub richEditControl1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
    Dim point As Point = e.GetPosition(CType(richEditControl1, UIElement))
    Dim pt As New System.Drawing.PointF(Units.PixelsToDocumentsF(CSng(point.X),richEditControl1.DpiX), Units.PixelsToDocumentsF(CSng(point.Y), richEditControl1.DpiY))
    Dim pos As DocumentPosition = richEditControl1.GetPositionFromPoint(pt)

    Dim bmCollection As BookmarkCollection = richEditControl1.Document.Bookmarks
    textBlock1.Text = String.Empty 'Clear a text block displaying bookmark info

    If pos IsNot Nothing Then
        For Each bm As Bookmark In bmCollection
            If bm.Range.Contains(pos) Then
                Dim bmHovered As Bookmark = bm
                richEditControl1.Document.SelectBookmark(bmHovered)
                ShowBookmarkInTextBlock(bmHovered)
                Exit For
            End If
        Next bm
    End If
End Sub

See Also

RichEditControl Class

RichEditControl Members

DevExpress.Xpf.RichEdit Namespace