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)
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.RectA 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 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; }
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
[tfl - 06 04 12] Hi - and thanks for your post. Community content is not the appropriate place for technical support queries. Instead, you should visit the MSDN Forums at http://forums.microsoft.com/MSDN, where such posts are welcomed and where you stand a much better chance of getting your query resolved. Sorry if that's not the answer you wanted to hear.
- 7/6/2011
- BTIdo
- 4/6/2012
- Thomas Lee
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.
but it's not true. The TextPointer.GetCharacterRect method always returns Rect with Width=0.0 regardless of its LogicalDirection.
So, all examples in documentation work only because they demonstrate how to select lines but not single chars.
[MSFT] - it's true that the Rect always has a width of 0 - see http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/97fc1774-cd27-4891-b4fd-e1794509522d . However, that's not the same as Rect.Empty, and it DOES return a Rect with measurements, it just happens to be a Rect with zero width. Documentation could probably clarify that, as it is the intended behavior of the API.
- 10/3/2010
- SergioSF
- 10/29/2010
- Wolf Schmidt - MSFT