I'm currently writing a plugin/extension for Visual Studio and I'd like to be able to find an existing menu command and hide it. Specifically, I want to find the Rebuild commands in the context menus and hide or disable them. It looks like I have access to the command IDs: https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.vsconstants.vsstd2kcmdid(v=vs.140).aspx
And using OleMenuCommandService there is a FindCommand but it returns NULL. Here is what I'm currently trying:
OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if ( null != mcs )
{
MenuCommand rebuildCommand = mcs.FindCommand(new CommandID(
Microsoft.VisualStudio.VSConstants.UICONTEXT_SolutionHasSingleProject,
(int)Microsoft.VisualStudio.VSConstants.VSStd97CmdID.RebuildCtx) );
// rebuildCommand is NULL
}
Any ideas on the way to do this?
Unfortunately, it appears to be impossible to get an existing Visual Studio command using FindCommand method. You can see more detailed information on the MSDN forum.
Also, I think it's not a good idea to make some existing Visual Studio features unavailable in an extension. Instead, you can subscribe to the CommandEvents.BeforeExecute
and CommandEvents.AfterExecute
events. Here's how to do it:
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Shell;
...
DTE2 ide = ServiceProvider.GlobalProvider.GetService(typeof(DTE)) as DTE2);
CommandEvents commandEvents = ide.Events.CommandEvents;
commandEvents.BeforeExecute += OnBeforeExecute;
commandEvents.AfterExecute += OnAfterExecute;
...
private void OnBeforeExecute(string Guid, int ID, object CustomIn, object CustomOut, ref bool CancelDefault)
{
if (ID == (int)VSConstants.VSStd97CmdID.RebuildCtx)
{
// Do something
}
}
private void OnAfterExecute(string Guid, int ID, object CustomIn, object CustomOut, ref bool CancelDefault)
{
if (ID == (int)VSConstants.VSStd97CmdID.RebuildCtx)
{
// Do something
}
}