如何获取EnvDte.ActiveDocument的IWpfTextView?

时间:2013-11-20 16:17:48

标签: c# visual-studio envdte

我正在尝试操作Visual Studio文本编辑器滚动条值。问题是我只有dte.ActiveDocument而且不可能从那里开始。

我的扩展只在VS启动时加载一次,并且我捕获了dte.Events.CommandEvents。在某些时候,我想更改ActiveDocument的滚动条值。要做到这一点,我需要IWpfTextView或ITextView。你知道如何获得该对象的实例吗?

internal class MyExtension
    {
        private CommandEvents commandEvents;

        private DTE dte;

        public MyExtension(DTE dte)
        {
            this.dte = dte;
            commandEvents = dte.Events.CommandEvents;
            commandEvents.BeforeExecute += commandEvents_BeforeExecute;
        }

        void commandEvents_BeforeExecute(string Guid, int ID, object CustomIn, object CustomOut, ref bool CancelDefault)
        {
            var doc = dte.ActiveDocument
            // CHANGE SCROLLBAR VALUES HERE
        }
    }

1 个答案:

答案 0 :(得分:3)

我找到了一种方法来做到这一点。在扩展的主类中,我得到了SVsTextManager

public sealed class MyExtensionPackage : Package
{
    protected override void Initialize()
    {
        DTE dte = (DTE)base.GetService(typeof(DTE));
        var txtMgr = (IVsTextManager)base.GetService(typeof(SVsTextManager));
        plugin = new MyExtension(dte, txtMgr);
        base.Initialize();
    }
}

internal class MyExtension
    {
        private CommandEvents commandEvents;

        private DTE dte;
        private IVsTextManager txtMngr;

        public MyExtension(DTE dte, IVsTextManager txtMngr)
        {
            this.txtMngr = txtMngr;
            this.dte = dte;
            commandEvents = dte.Events.CommandEvents;
            commandEvents.BeforeExecute += commandEvents_BeforeExecute;
        }

        void commandEvents_BeforeExecute(string Guid, int ID, object CustomIn, object CustomOut, ref bool CancelDefault)
        {
            var doc = dte.ActiveDocument

            IVsTextView textViewCurrent;
            txtMngr.GetActiveView(1, null, out textViewCurrent);
            int a, b, c, verticalScrollPosition;

            var scrollInfo = textViewCurrent.GetScrollInfo(1, out a, out b, out c, out verticalScrollPosition);
            textViewCurrent.SetScrollPosition(1, verticalScrollPosition);
        }
    }