【问题标题】:Find the correct GUID-CommandId combination for visual studio extension为 Visual Studio 扩展找到正确的 GUID-CommandId 组合
【发布时间】:2016-06-09 00:49:56
【问题描述】:

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

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

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

        }

【问题讨论】:

  • 您能否在文件 -> 关闭或其他菜单项上显示挂钩点击事件的代码?
  • @@Colin Zabransky - 如果您需要项目的其他部分,请告诉我。
  • @Arun,“识别 GUID 和 id”是什么意思。您想捕获已执行命令的 id 及其组 GUID,对吧?
  • @NValchev,没错。
  • 我认为你的问题在于这个 VSConstants.GUID_VSStandardCommandSet97 我不认为这会起作用,之后你的命令是否为空?我认为您将需要找到所有单独的命令 guid 并分别附加它们,我认为您不能一次性完成。

标签: c# visual-studio-extensions vsix


【解决方案1】:

在注册表中启用 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/

【讨论】:

  • 感谢@Paul 的回答。
猜你喜欢
  • 1970-01-01
  • 2021-09-13
  • 1970-01-01
  • 1970-01-01
  • 2019-02-09
  • 2015-06-04
  • 2017-10-11
  • 1970-01-01
  • 2019-02-09
相关资源
最近更新 更多