GetCharacterRect Method
Collapse the table of content
Expand the table of content

TextPointer.GetCharacterRect Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Returns a bounding box for content that borders the current TextPointer in the specified logical direction.

Namespace:  System.Windows.Documents
Assembly:  System.Windows (in System.Windows.dll)

'Declaration
Public Function GetCharacterRect ( _
	direction As LogicalDirection _
) As Rect

Parameters

direction
Type: System.Windows.Documents.LogicalDirection
One of the LogicalDirection values that specify the logical direction in which to find a content bounding box.

Return Value

Type: System.Windows.Rect
A Rect for content that borders the current TextPointer in the specified direction, or Rect.Empty if current and valid layout information is unavailable.

The return value is always the edge of the next Unicode content, that is, the element edges are invisible. If there is no content in the indicated direction, the return value is the Rect of the content in the opposite direction. If the RichTextBox is empty, the return value is a zero-width Rect with the default line height.

The following code uses the GetCharacterRect method to highlight the first word in the RichTextBox. In this example, a space character is used as the word boundary. This code example is part of a larger example used in the TextPointer class.


    'This method highlights the first word in the RichTextBox
    Public Sub HighlightFirstWord()
        Dim StartofContent As TextPointer = MyRTB1.ContentStart
        Dim currentPointer As TextPointer = StartofContent.GetNextInsertionPosition(LogicalDirection.Forward)
        If (currentPointer Is Nothing) Then
            Return
        End If
        Dim currentChar As String = GetCurrentChar(MyRTB1, currentPointer, LogicalDirection.Forward)

        While ((currentChar <> " ") _
                    AndAlso (currentChar <> ""))
            currentPointer = currentPointer.GetNextInsertionPosition(LogicalDirection.Forward)
            currentChar = GetCurrentChar(MyRTB1, currentPointer, LogicalDirection.Forward)

        End While
        Dim StartRect As Rect = StartofContent.GetCharacterRect(LogicalDirection.Forward)
        Dim EndRect As Rect = currentPointer.GetCharacterRect(LogicalDirection.Forward)
        StartRect.Union(EndRect)
        CreateHighlightRectangle(StartRect)
    End Sub

    Private Function CreateHighlightRectangle(ByVal bounds As Rect) As Rectangle
        Dim r As Rectangle = New Rectangle
        r.Fill = New SolidColorBrush(Color.FromArgb(75, 0, 0, 200))
        r.Stroke = New SolidColorBrush(Color.FromArgb(230, 0, 0, 254))
        r.StrokeThickness = 1
        r.Width = bounds.Width
        r.Height = bounds.Height
        Canvas.SetLeft(r, bounds.Left)
        Canvas.SetTop(r, (bounds.Top + 41))
        LayoutRoot.Children.Add(r)
        Return r
    End Function



Windows Phone OS

Supported in: 8.1, 8.0, 7.1

Windows Phone

Show:
© 2017 Microsoft