How can I get IWpfTextView for EnvDte.ActiveDocument?

China☆狼群 提交于 2021-02-07 14:24:31

问题


I am trying to manipulate the Visual Studio Text Editor scrollbar values. The problem is that I have only the dte.ActiveDocument and it is not possible to do it from there.

My extension is loaded only once when VS starts and I capture dte.Events.CommandEvents. At some point I want to change the scrollbar values for the ActiveDocument. To do this I need IWpfTextView or ITextView. Do you have any idea how can I get an instance of that object?

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:


I found a way to do this. In the main class of the extension I get 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);
        }
    }


来源:https://stackoverflow.com/questions/20101167/how-can-i-get-iwpftextview-for-envdte-activedocument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!