之后,我的问题添加一个命令过滤器的TextView [英] Issues after I add a command filter to textview

查看:119
本文介绍了之后,我的问题添加一个命令过滤器的TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要拦截与我搜索帮助许多文章的关键事件,并 >启发了我。我所做的是:




  1. 创建一个新的类,并实行IVsTextManagerEvents界面注册每个TextView的



     公共无效OnRegisterView(IVsTextView PVIEW)
    {
    CommandFilter过滤器=新CommandFilter();
    IOleCommandTarget nextCommandTarget;
    pView.AddCommandFilter(过滤器,出nextCommandTarget);
    filter.NextCommandTarget = nextCommandTarget;
    }


  2. 添加新阶层CommandFilter,它实现IOleCommandTarget,我们在其中可以从VS



    拦截olecommand

     公共类CommandFilter:IOleCommandTarget 
    {

    公共IOleCommandTarget NextCommandTarget
    {
    获得;
    组;
    }

    公众诠释QueryStatus(REF的Guid pguidCmdGroup,UINT cCmds,OLECMD [] prgCmds,IntPtr的pCmdText)
    {
    NextCommandTarget.QueryStatus(REF pguidCmdGroup,cCmds, prgCmds,pCmdText);
    返回VSConstants.S_OK;
    }

    公众诠释Exec的(REF的Guid pguidCmdGroup,UINT nCmdID,UINT nCmdexecopt,IntPtr的pvaIn,IntPtr的pvaOut)
    {
    如果(pguidCmdGroup == typeof运算(VSConstants .VSStd2KCmdID).GUID)
    {
    开关(nCmdID)
    {
    的情况下(UINT)VSConstants.VSStd2KCmdID.RETURN:
    MessageBox.Show(输入) ;
    中断;
    }
    }

    NextCommandTarget.Exec(pguidCmdGroup,nCmdID,nCmdexecopt,pvaIn,pvaOut);

    返回VSConstants.S_OK;
    }
    }


  3. 我们需要初始化<通知IVsTextManagerEvents / p>

     保护覆盖无效初始化()
    {
    base.Initialize();

    的IConnectionPointContainer textManager =(的IConnectionPointContainer)GetService的(typeof运算(SVsTextManager));
    的Guid = interfaceGuid typeof运算(IVsTextManagerEvents).GUID;
    textManager.FindConnectionPoint(REF interfaceGuid,出tmConnectionPoint);
    tmConnectionPoint.Advise(新TextManagerEventSink(),出tmConnectionCookie);
    }




在上面的准备,我们现在可以截获键盘事件。您可以在您行程键输入看到一个消息框。



我的问题是,我在上面

完成后


  1. 我无法保存文件,这意味着,当我抚摸CTRL + S,什么都没有发生。

  2. 您可以看到明显的延迟,当我钥匙在口头上。看来我的包裹需要很长的时间来处理一些东西,但你可以在上面看到的,我根本没有。


!解决方案

看来我已经找到了答案



  NextCommandTarget.Exec(pguidCmdGroup,nCmdID,nCmdexecopt,pvaIn,pvaOut); 

返回VSConstants.S_OK;



不过:

 返回NextCommandTarget.Exec(pguidCmdGroup,nCmdID,nCmdexecopt,pvaIn,pvaOut); 


I want to intercept key events in vs. I searched many articles for help, and this article inspired me. What I have done is:

  1. create a new class and implement "IVsTextManagerEvents" interface to register every textview.

    public void OnRegisterView(IVsTextView pView)
    {
        CommandFilter filter = new CommandFilter();
        IOleCommandTarget nextCommandTarget;
        pView.AddCommandFilter(filter, out nextCommandTarget);
        filter.NextCommandTarget = nextCommandTarget;
    }
    

  2. add new class "CommandFilter" which implement IOleCommandTarget , in which we can intercept olecommand from vs

    public class CommandFilter : IOleCommandTarget
    {    
    
        public IOleCommandTarget NextCommandTarget
        {
            get; 
            set;
        }
    
        public int QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, IntPtr pCmdText)
        {
            NextCommandTarget.QueryStatus(ref pguidCmdGroup, cCmds, prgCmds, pCmdText);
            return VSConstants.S_OK;
        }
    
        public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            if (pguidCmdGroup == typeof(VSConstants.VSStd2KCmdID).GUID)
            {
                switch (nCmdID)
                {
                    case (uint)VSConstants.VSStd2KCmdID.RETURN:
                        MessageBox.Show("enter");
                        break;
                }
            }
    
            NextCommandTarget.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
    
            return VSConstants.S_OK;
        }
    }
    

  3. we need to advise IVsTextManagerEvents in Initialize

    protected override void Initialize()
    {
        base.Initialize();
    
        IConnectionPointContainer textManager = (IConnectionPointContainer)GetService(typeof(SVsTextManager));
        Guid interfaceGuid = typeof(IVsTextManagerEvents).GUID;
        textManager.FindConnectionPoint(ref interfaceGuid, out tmConnectionPoint);
        tmConnectionPoint.Advise(new TextManagerEventSink(), out tmConnectionCookie);
    }
    

after above prepare, we can now intercept key events. you can see a message box after you stroke key "enter".

My question is, after I have done above

  1. I can't save the document, that means when I stroked ctrl+S, nothing happened.
  2. You can see obvious delay, when I key in words. It seems my package take a long time to handle something, but as you can see above, I didn't at all.

解决方案

It seems I have found the answer!

Not:

NextCommandTarget.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);

return VSConstants.S_OK;

But:

return NextCommandTarget.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);

这篇关于之后,我的问题添加一个命令过滤器的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆