Reputation: 342
I am coding a scroll bar with mark (like Visual Studio) and I want to be able to put a mark when i do a find (Ctrl+F) for a richtextbox. My user control is a scrollViewer with a contentPresenter, then in my window I put my content in the contentPresenter.
For now, I am able to find all the pattern i want with this code. But now, I want to add a mark on my scrollbar.
My algorithm to put mark is base on the position of the element I want to mark. So I would like to get the position from the top of the richtextbox or his parent (in my case it's a contentPresnter).
At beginning, I tought about getting the row of my string, but I can't cause it's a richtextbox and one line can have font size 48 and the next 12.
SOLUTION
I end up with one of the idea of Furkle. I use the RichTextBox.CaretPosition.GetCharacterRect(...) to get the Y and the Height property of the Rect.
Upvotes: 2
Views: 1023
Reputation: 5059
I think there's two basic ways you can do this:
Create a custom control that extends the functionality of the RichTextBox. There is documentation about extending WPF controls on MSDN here: http://msdn.microsoft.com/en-us/library/cc295235.aspx
Use a second, separate control, approximately the width of the scrollbar and the height of the whole RichTextBox. Give this control a transparent background and place it in a higher z-order than the RichTextBox, and you'll be able to draw overtop the RichTextBox. This is, in my imagination, basically the same thing way you'd be extending the RichTextBox in #1. Bear in mind two things, though - you'll have to make sure the rectangles, and the control in which they're being held, are not HitTestVisible, or you'll make it impossible to use the scrollbar.
It's also possible that this is a built-in function I'm totally unaware of, but I've never encountered it before.
In order to get the pixel position, one function you could use is CaretPosition.GetCharacterRect(): https://social.msdn.microsoft.com/Forums/vstudio/en-US/af780cd9-a4d5-4ab0-850d-c1e98a0d0046/how-to-get-x-y-corordicates-of-caret-in-richtextbox-when-caret-is-at-the-end-of-the-wrapped-line?forum=wpf. In moving through the RichTextBox to find the matching phrases, you could set the caret at that position, saving all the positions you find and storing that to later be used in drawing your rectangles.
Upvotes: 1