Logo
HOWTO: Create a command without user interface items from a Visual Studio package.

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio 2010
Date: July 2014   Microsoft Visual Studio 2012
      Microsoft Visual Studio 2013
Introduction

The package wizard allows you to create a command with a menu item in the "Tools" menu. However, sometimes you may want to create a command without user interface items (toolbar buttons or menu items), for example an internal command or a command intended to be used only through a keyboard shortcut. This article explains how to do it.

More Information

The following package creates a command with the name "MyPackage.MyCommand" without user interface items using the LocCanonicalName tag in the .vsct file. After compiling the package, you can check that the command exists in the "Tools", "Options" window, "Environment", "Keyboard" section and can be executed in the "View" > "Other Windows" > "Command Window" toolwindow:

Language: XML   Copy Code Copy Code (IE only)
<?xml version="1.0" encoding="utf-8"?>
<CommandTable xmlns="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable" xmlns:xs="http://www.w3.org/2001/XMLSchema">

   <Extern href="stdidcmd.h"/>
   <Extern href="vsshlids.h"/>
   <Extern href="msobtnid.h"/>

   <Commands package="guidCommandOnlyPkg">

      <Buttons>
         
         <Button guid="guidCommandOnlyCmdSet" id="cmdidMyCommand" priority="0x0100" type="Button">
            <Strings>
               <LocCanonicalName>MyPackage.MyCommand</LocCanonicalName>
               <ButtonText>My Command</ButtonText>
            </Strings>
         </Button>

      </Buttons>

   </Commands>

   <Symbols>
      
      <GuidSymbol name="guidCommandOnlyPkg" value="{YOUR_GUID_HERE}" />

      <GuidSymbol name="guidCommandOnlyCmdSet" value="{YOUR_GUID_HERE}">
         <IDSymbol name="cmdidMyCommand" value="0x0100" />
      </GuidSymbol>

   </Symbols>

</CommandTable>
Language: C#   Copy Code Copy Code (IE only)
using System;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.ComponentModel.Design;
using Microsoft.Win32;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;

namespace MyCompany
{
   [PackageRegistration(UseManagedResourcesOnly = true)]
   [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
   [ProvideMenuResource("Menus.ctmenu", 1)]
   [Guid(GuidList.guidCommandOnlyPkgString)]
   public sealed class CommandOnlyPackage : Package
   {
      public CommandOnlyPackage()
      {
      }

      protected override void Initialize()
      {
         OleMenuCommandService oleMenuCommandService;
         CommandID commandID;
         MenuCommand menuCommand;

         base.Initialize();

         oleMenuCommandService = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;

         if (oleMenuCommandService != null)
         {
            commandID = new CommandID(GuidList.guidCommandOnlyCmdSet, (int)PkgCmdIDList.cmdidMyCommand);

            menuCommand = new MenuCommand(MenuCommandCallback, commandID);
            
            oleMenuCommandService.AddCommand(menuCommand);
         }
      }

      private void MenuCommandCallback(object sender, EventArgs e)
      {
         System.Windows.Forms.MessageBox.Show(sender.ToString());
      }
   }
}
Language: VB.NET   Copy Code Copy Code (IE only)
Imports Microsoft.VisualBasic
Imports System
Imports System.Diagnostics
Imports System.Globalization
Imports System.Runtime.InteropServices
Imports System.ComponentModel.Design
Imports Microsoft.Win32
Imports Microsoft.VisualStudio
Imports Microsoft.VisualStudio.Shell.Interop
Imports Microsoft.VisualStudio.OLE.Interop
Imports Microsoft.VisualStudio.Shell

<PackageRegistration(UseManagedResourcesOnly:=True), _
    InstalledProductRegistration("#110", "#112", "1.0", IconResourceID:=400), _
    ProvideMenuResource("Menus.ctmenu", 1), _
    Guid(GuidList.guidCommandOnlyPkgString)> _
Public NotInheritable Class CommandOnlyPackage
   Inherits Package

   Public Sub New()
   End Sub

   Protected Overrides Sub Initialize()

      Dim oleMenuCommandService As OleMenuCommandService
      Dim commandID As CommandID
      Dim menuCommand As MenuCommand

      MyBase.Initialize()

      oleMenuCommandService = TryCast(GetService(GetType(IMenuCommandService)), OleMenuCommandService)

      If oleMenuCommandService IsNot Nothing Then

         commandID = New CommandID(GuidList.guidCommandOnlyCmdSet, CInt(PkgCmdIDList.cmdidMyCommand))

         menuCommand = New MenuCommand(New EventHandler(AddressOf MenuCommandCallback), commandID)

         oleMenuCommandService.AddCommand(menuCommand)

      End If

   End Sub

   Private Sub MenuCommandCallback(ByVal sender As Object, ByVal e As EventArgs)

      System.Windows.Forms.MessageBox.Show(sender.ToString())

   End Sub

End Class


Go to the 'Visual Studio Extensibility (VSX)' web site for more articles like this (Articles section)