How to get the absolute position of a text cursor in a Visual Studio 2010 extension

I designed a Dialog similar to IntelliSense, which should appear on a particular key stroke. (My project is VS-Package, my dialog will be opened as a team) The problem is that I do not know how to display my dialog at the current cursor position . There are simple ways to process the currently selected text, for example. from

TextSelection objSel = (EnvDTE.TextSelection)(dte.ActiveDocument.Selection); 

but I can’t get any absolute position here.

I searched a lot, but found nothing that could help me. Maybe someone can give me a hint or, even better, code examples to solve my problem. I am very grateful for your help!

+6
source share
1 answer

I am doing the same in the current project, so here is the corresponding copy of the code and the insert. I am generating an activeWpfTextView object elsewhere using the following answer: Find an IVsTextView or IWpfTextView for a given ProjectItem in the VS 2010 RC extension .

  private IVsWindowFrame GetWindow() { // parent is the Microsoft.VisualStudio.Shell.ToolWindowPane // containing this UserControl given in the constructor. var window = (ToolWindowPane)parent.GetIVsWindowPane(); return (IVsWindowFrame)window.Frame; } private void DoShow() { var window = GetWindow(); var textViewOrigin = (activeWpfTextView as UIElement).PointToScreen(new Point(0, 0)); var caretPos = activeWpfTextView.Caret.Position.BufferPosition; var charBounds = activeWpfTextView .GetTextViewLineContainingBufferPosition(caretPos) .GetCharacterBounds(caretPos); double textBottom = charBounds.Bottom; double textX = charBounds.Right; Guid guid = default(Guid); double newLeft = textViewOrigin.X + textX - activeWpfTextView.ViewportLeft; double newTop = textViewOrigin.Y + textBottom - activeWpfTextView.ViewportTop; window.SetFramePos(VSSETFRAMEPOS.SFP_fMove, ref guid, (int)newLeft, (int)newTop, 0, 0); window.Show(); resultsList.Focus(); } 
+6
source

Source: https://habr.com/ru/post/904376/


All Articles