The Actipro Blog

All the latest UI control development news from Actipro

SyntaxEditor for WPF adds robust hit testing features

The lastest build of SyntaxEditor for WPF added a very robust hit testing feature.  This is something that has been in the WinForms version for SyntaxEditor for a while and has been highly requested by our WPF customers.

The feature allows you to provide a Point (generally a mouse location) and SyntaxEditor returns detailed information about what is under the Point.  You can get offsets, text positions (line/col), text, tokens, etc. 

How does it work?

A hit test can be performed by first obtaining a Point relative to the SyntaxEditor's coordinates and then calling the SyntaxEditor.HitTest method.  This method returns an object of type IHitTestResult.

   1: IHitTestResult result = editor.HitTest(Mouse.GetPosition(editor));

The hit test result

The IHitTestResult object contains information about exactly what is at the location being hit tested.  It has a Type property that provides a high-level categorization about the result, such as does the result specify the location was over a character in the text area, over a margin, over a splitter or scrollbar, etc.

A VisualElement property provides a reference to the FrameworkElement that was used as the result.  So if the location was over a ScrollBar, that is what would be returned in this property.

When the location is over an IEditorView, that view is returned in the View property.  If the location is over a margin in the view, the related IEditorViewMargin is returned in the Margin property.

If the location is over a character in a view’s text area, the Offset and Position properties are filled in with the exact position of the character within the view’s Snapshot (also made available in the result).  If the location is not directly over a character (such as when it is over a margin), the Offset and Position of the closest character is returned.  The Type property can be used to determine if the location was over a character or not.

There also is a helpful GetReader method that returns an ITextSnapshotReader initialized to the Offset.  This allows you to instantly get the character at the offset, or get the token there.  You can navigate around the Snapshot’s text/tokens using the reader.

The new hit testing QuickStart

We’ve added a QuickStart to demo the new hit testing feature.  As you move the mouse over anything in the SyntaxEditor instance (splitters, scollbars, margins, text area, etc.), the TextBox below it displays details about the hit test result.


In the screenshot above, the mouse cursor is over the r in the word QuickStart on line #7.  The hit test information is telling us the snapshot version at the time of the hit test, which view it’s over, the offset and text position (both zero-based), the character, and information about the token that contains the offset.


As you can see, we’ve built this hit testing mechanism so that the result gives you everything you need to know about what is at a particular location.  It is extremely useful for reacting to mouse move or hover events.

Filed under: Actipro, New features, WPF

Pingbacks and trackbacks (1)+

Comments are closed