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

时间:2016-06-06 09:38:24

标签: c# visual-studio-extensions vsix

我试图编写一个简单的VS扩展程序,它会显示菜单是否附加了快捷方式。

因此,当用户点击菜单中的编辑 - >撤消时,我想要显示一条消息' Ctrl + Z'所以用户可以开始学习那个快捷方式。我遇到的问题是识别GUID和相应的命令ID。下面看起来是正确的,但当我点击File-> Close时,不会调用回调。

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

protected override 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);

        }

1 个答案:

答案 0 :(得分:0)

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

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/