如何在Visual Studio 2010扩展中获取文本光标的绝对位置

Chr*_*der 6 c# visual-studio-2010 envdte vsix visual-studio-extensions

我开发了类似智能感知的对话框,它应该出现在特定的按键上.(我的项目是VS-Package,我的对话框将作为命令打开)问题是,我不知道如何在当前光标位置显示我的对话框.有简单的方法来处理当前选定的文本,例如

 TextSelection objSel = (EnvDTE.TextSelection)(dte.ActiveDocument.Selection);
Run Code Online (Sandbox Code Playgroud)

但我不能在这里获得任何绝对的立场.

我搜索了很多,但没有发现任何东西,这可以帮助我.也许有人可以给我一个提示或 - 甚至更好 - 代码示例来解决我的问题.我真的很感谢你的帮助!

per*_*man 6

我在当前项目中做的完全相同,所以这里是相关的代码复制和粘贴.我activeWpfTextView使用以下答案在其他地方生成对象:在VS 2010 RC扩展中为给定的ProjectItem查找IVsTextView或IWpfTextView.

    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();
    }
Run Code Online (Sandbox Code Playgroud)