如何在VSPackage插件中捕获Visual Studio命令? [英] How to capture Visual Studio commands in a VSPackage Plugin?

查看:107
本文介绍了如何在VSPackage插件中捕获Visual Studio命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个插件,以捕获Visual Studio的edit.copy命令,如果未选择任何内容,则将其跳过.

I'm trying to make a plugin that captures the edit.copy command of visual studio, and skips it if nothing is selected.

我已经安装了Visual Studio SDK,并创建了VSPackage项目.我的initialize方法当前看起来像这样:

I have installed the Visual Studio SDK and created a VSPackage project. My initialize method curently looks like the following:

private EnvDTE.DTE m_objDTE = null;
protected override void Initialize()
{
    Debug.WriteLine (string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString()));
    base.Initialize();

    m_objDTE = (EnvDTE.DTE)GetService(typeof(EnvDTE.DTE));

    m_objDTE.Events.CommandEvents.BeforeExecute += (
      string Guid,
      int ID,
      Object CustomIn,
      Object CustomOut,
      ref bool CancelDefault
      ) =>
    {
      EnvDTE.Command objCommand;
      string commandName = "";

      objCommand = m_objDTE.Commands.Item(Guid, ID);
      if (objCommand != null)
      {
        commandName = objCommand.Name;

        if (string.IsNullOrEmpty(commandName))
        {
          commandName = "<unnamed>";
        }
      }

      Debug.WriteLine("Before executing command with Guid=" + Guid + " and ID=" + ID + " named " + commandName);


    };

}

打开解决方案时将调用Initialize()方法,我将[ProvideAutoLoad(UIContextGuids80.SolutionExists)]标记添加到了程序包中.但是当我执行操作时,例如按ctrl + c这是一条命令时,不会调用BeforeExecute.使用此代码,我希望所有命令都将在调试控制台中打印出来,为什么不发生这种情况?

The Initialize() method is called when a solution is opened, I have the [ProvideAutoLoad(UIContextGuids80.SolutionExists)] tag added to my package. But the BeforeExecute is not called when I do things, like pressing ctrl + c which is a command, With this code I expect all commands to be printed in the debug console, why isn't this happening?

推荐答案

这是我使用的有效代码:

Here is the code I used that works:

using System;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.ComponentModel.Design;
using Microsoft.Win32;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
using EnvDTE;

namespace Dinto.NoCopy
{
    [PackageRegistration(UseManagedResourcesOnly = true)]
    [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
    [Guid(GuidList.guidNoCopyPkgString)]
    [ProvideAutoLoad(UIContextGuids80.SolutionExists)]
    public sealed class NoCopyPackage : Package
    {
        public NoCopyPackage()
        {
            Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering constructor for: {0}", this.ToString()));
        }

        #region Package Members


        private EnvDTE.DTE m_objDTE = null;
        private CommandEvents _pasteEvent;

        protected override void Initialize()
        {
            Debug.WriteLine (string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString()));
            base.Initialize();

            m_objDTE = (DTE)GetService(typeof(DTE));

            var pasteGuid = typeof(VSConstants.VSStd97CmdID).GUID.ToString("B");
            var pasteID = (int)VSConstants.VSStd97CmdID.Copy;

            _pasteEvent = m_objDTE.Events.CommandEvents[pasteGuid, pasteID];
            _pasteEvent.BeforeExecute += CopyRead;

        }
        #endregion

        private void CopyRead (
          string Guid,
          int ID,
          Object CustomIn,
          Object CustomOut,
          ref bool CancelDefault
          )
        {
          EnvDTE.Command objCommand;
          string commandName = "";


          objCommand = m_objDTE.Commands.Item(Guid, ID);
          if (objCommand != null)
          {
            commandName = objCommand.Name;
          }

          if (commandName.Equals("Edit.Copy"))
          {
            TextSelection textSelection = (TextSelection)m_objDTE.ActiveDocument.Selection;

            if (textSelection.IsEmpty)
            {
              CancelDefault = true;
            }
          }
        }

    }
}

这篇关于如何在VSPackage插件中捕获Visual Studio命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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