Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

TextPointer.GetCharacterRect Method

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)
public Rect GetCharacterRect(
	LogicalDirection direction
)

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.

Run this sample


//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;
}


Silverlight

Supported in: 5, 4

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.