How to overlay a control onto the Visual Studio code surface
-
06-07-2019 - |
Question
I am writing an addin into Sql Server Management Studio, using the Visual Studio Extensibilty APIs. I have had some success overlaying a control onto the text surface (I'm attempting to emulate the CodeRush/Refactor action list, similar to the intellisense combo), however I can only locate it's coordinate space based the following property:
get
{
var point = TextDocument.Selection.TopPoint;
return new Cursor( point.DisplayColumn, point.Line );
}
This code does allow me to then convert cols/rows into pixels, however I can not find a way to offset the cols/rows when the text editor has been scrolled either vertically or horizontally. This causes the listbox to disappear below the visible screen space.
What I am looking for is a method of getting the screen coordinates from the current col/row pair, so that I can place the listbox next to the cursor, regardless of the scrolled position.
Solution
The TextDocument.Selection property, of type TextSelection, has a TextPane property - see here for more info. It doesn't explicitly say so, but the TextPane is the part of the screen that is visible. Further, the StartPoint property for a TextPane provides the 'offset' of the scrolled text.
I was therefore able to determine the offset cursor position by subtracting the TextPane.StartPoint from the Selection's StartPoint:
get
{
var start = TextDocument.Selection.TextPane.StartPoint;
var top = TextDocument.Selection.TopPoint;
return new Cursor(
top.DisplayColumn - start.DisplayColumn + 1 ,
top.Line - start.Line + 1
);
}