Freigeben über


TextPointer.GetCharacterRect Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

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

Parameters

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.

Remarks

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.

Examples

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.

Run this sample

    '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

//This method highlights the first word in the RichTextBox
public void HighlightFirstWord()
{
    TextPointer StartofContent = MyRTB1.ContentStart;
    TextPointer currentPointer = StartofContent.GetNextInsertionPosition(LogicalDirection.Forward);

    if (currentPointer == null)
        return;

    string currentChar = GetCurrentChar(MyRTB1, currentPointer, LogicalDirection.Forward);

    while (currentChar != " " && currentChar != "")
    {
        currentPointer = currentPointer.GetNextInsertionPosition(LogicalDirection.Forward);
        currentChar = GetCurrentChar(MyRTB1, currentPointer, LogicalDirection.Forward);
    }

    Rect StartRect = StartofContent.GetCharacterRect(LogicalDirection.Forward);
    Rect EndRect = currentPointer.GetCharacterRect(LogicalDirection.Forward);
    StartRect.Union(EndRect);
    CreateHighlightRectangle(StartRect);

}

private Rectangle CreateHighlightRectangle(Rect bounds)
{
    Rectangle r = 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;
}

Version Information

Silverlight

Supported in: 5, 4

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.