为 visual studio 扩展找到正确的 GUID-CommandId 组合

Find the correct GUID-CommandId combination for visual studio extension

我正在尝试编写一个简单的 VS 扩展,它将显示菜单是否附加了快捷方式。

因此,当用户从菜单中单击“编辑”->“撤消”时,我想显示一条消息 'Ctrl + Z',以便用户可以开始学习该快捷方式。我遇到的问题是识别 GUID 和相应的命令 ID。下面看起来是正确的,但是当我点击文件->关闭时,回调没有被调用。

CommandID menuCommandId = new CommandID(VSConstants.GUID_VSStandardCommandSet97, (int)VSConstants.VSStd97CmdID.FileClose);

受保护的覆盖 void Initialize() {

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

    // Add our command handlers for menu (commands must exist in the .vsct file)
    var mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
    if ( null != mcs )
    {
        // Create the command for the menu item.

        // https://msdn.microsoft.com/en-us/library/cc826118%28v=vs.120%29.aspx
        CommandID menuCommandId = new CommandID(GuidList.guidLearnShortcutsCmdSet, (int)PkgCmdIDList.cmdIdLearnShortcuts);

        //workbench.files.action.closeFile
        //CommandID menuCommandId = new CommandID(VSConstants.GUID_VSStandardCommandSet97, (int)VSConstants.VSStd97CmdID.FileClose);
        MenuCommand menuItem = new MenuCommand(MenuItemCallback, menuCommandId );
        mcs.AddCommand( menuItem );
    }
}

    private void MenuItemCallback(object sender, EventArgs e)
        {
            IVsStatusbar statusBar = (IVsStatusbar)GetService(typeof(SVsStatusbar));

            MenuCommand btn = (MenuCommand)sender;

            // Make sure the status bar is not frozen
            int frozen;

            statusBar.IsFrozen(out frozen);

            if (frozen != 0)
            {
                statusBar.FreezeOutput(0);
            }

            // Set the status bar text and make its display static.
            statusBar.SetText("Update here.");

            // Freeze the status bar.
            statusBar.FreezeOutput(1);

            // Get the status bar text. 
            string text;
            statusBar.GetText(out text);
            System.Windows.Forms.MessageBox.Show(text);

            // Clear the status bar text.
            statusBar.FreezeOutput(0);

        }

在注册表中启用 VSIPLogging,版本号应该是您拥有的或您使用的最高版本。

HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\[version num]\General\EnableVSIPLogging = 1

然后在按住 CTRL+SHIFT 的同时单击要识别的工具栏或菜单。这将显示一个带有命令栏属性的对话框。记下 Guid 和 CmdID

旧文章,但仍然有效 https://blogs.msdn.microsoft.com/dr._ex/2007/04/17/using-enablevsiplogging-to-identify-menus-and-commands-with-vs-2005-sp1/