2

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?

1 Answer 1

2

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
    }
}
2
  • This is great, thank you! I think I can make this work even though I agree it's not ideal -- perhaps another approach would be to override the global Visual Studio settings to hide certain commands like you can do with Tools > Customize? Maybe the "right" question is how to change settings programmatically?
    – jvaneenwyk
    Jan 19, 2016 at 4:31
  • I think it should be void OnAfterExecute(string Guid, int ID, object CustomIn, object CustomOut), without the last argument.
    – valentin
    Aug 8, 2018 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.