Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF RichTextBox GetCharacterRect

I am trying to get the character rect for all characters typed inside a richtextbox. Starting from the MyRichTextBox.Document.ContentStart textpointer, I advance every character using the following code

currentPointer = currentPointer.GetNextInsertionPosition(LogicalDirection.Forward)

And retrieve the "character" rect using the following code

currentPointer.GetCharacterRect(LogicalDirection.Forward)

But it looks like, for any textpointer returned from the piece of code above, the width of the rect is always 0. Now I am able to calculate the character rect based on the area between previous character rect and current character rect, but this causes a problem when a word wrap occurs.

Is there any way I can get a non-zero width from the GetCharacterRect() function of the rich text box ?

EDIT

The content from Microsoft here shows the same. Is anybody aware of an alternative to measure the character rect ?

like image 380
Manoj Avatar asked Nov 04 '22 18:11

Manoj


1 Answers

GetCharacterRect just returns a rectangle representing the leading edge of the character in question (i.e., a zero-width, but accurate height and position) - not a "wrapper rectangle"...although if that's what you are after, you could grab the leading edge of two consecutive characters and do the math.

like image 188
JerKimball Avatar answered Nov 15 '22 09:11

JerKimball