7

我开发了一个类似 IntelliSense 的对话框,它应该出现在特定的击键上。(我的项目是一个 VS 包,我的对话框将作为命令打开)问题是,我不知道如何在当前光标位置显示我的对话框。有一些简单的方法可以处理当前选定的文本,例如

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

但我在这里无法获得任何绝对位置。

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

4

1 回答 1

7

我在当前项目中做同样的事情,所以这里是相关的代码复制和粘贴。我activeWpfTextView使用以下答案在其他地方生成对象:Find an IVsTextView or IWpfTextView for a given ProjectItem, in 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();
    }
于 2012-02-08T00:11:51.477 回答