Visual Studio Hacks [Electronic resources]

Andrew Lockhart

نسخه متنی -صفحه : 172/ 133
نمايش فراداده

Hack 90. Find the Name of That Command Bar

Use a freebie from Microsoft to discover the names of the different command bars in Visual Studio.

One of the difficult tasks when working with Visual Studio is finding out what the names of the various command bars are. Command bars are used by Visual Studio to store commands. The Tools menu is a command bar, as is the right-click menu. To create a new command and assign it to a command bar, you will use code that looks like the following:

Command command = commands.AddNamedCommand(addInInstance, 
"Add-IN", "Command", "Executes the command for this Add-in", 
true, 59, ref contextGUIDS,   
(int)vsCommandStatus.vsCommandStatusSupported + 
(int)vsCommandStatus.vsCommandStatusEnabled);
CommandBar commandBar = (CommandBar)commandBars["Tools"];
CommandBarControl commandBarControl = 
command.AddControl(commandBar, 1);

The preceding code would add a new command to the Tools menu. As you can see, there is a collection named commandBars that you use to specify which CommandBar you want to add your command to. The problem is that there is no easy way to find out what these names are in Visual Studio. Thankfully, there is a way to get a list of all these CommandBars using a free download from Microsoft.

12.6.1. Install the Command Browser

The add-in you can use to view the names of the different command bars is actually part of the automation samples. To download this add-in, go to:

http://www.microsoft.com/downloads/details.aspx?familyid=3ff9c915-30e5-430e-95b3-621dccd25150&displaylang=en#filelist

Choose the Command Browser Add-in from the list of files (CmdBrowser.exe). Download this file and then extract its contents to a directory on your hard drive. This will put the code for this add-in on your machine. Next, you need to follow these steps to get the command browser installed:

Open the CmdBrowser.sln file in Visual Studio.

Build the solution in Visual Studio.

Close Visual Studio.

Run the AddinRef.reg file in the CmdBrowser directory.

This will install the Command Browser on your machine.

12.6.2. Run the Command Browser

When you start Visual Studio, you should see a new command on the Tools menu for the Command Browser. (If you don't, you may need to enable the add-in by using the Add-in Manager or run devenv /setup [Hack #92] .)

When you run the add-in, you will see the Command Browser, which is shown in Figure 12-13.

Figure 12-13. Command Browser

The node titled Commands includes a list of all the commands in Visual Studio. If you expand the node, as shown in Figure 12-14, you can then select a node and get more information about that node in the Command Information dialog, shown in Figure 12-15.

Figure 12-14. Command Browser commands

Figure 12-15. Command Information dialog

This dialog shows you the name of the command, the GUID and ID of the command, as well as the current keystroke bindings.

More importantly, you can also use this tool to view all the command bars under the Tool Bars node, which is shown in Figure 12-16.

Figure 12-16. Command BrowserTool Bars node

Under this node, you can see the names of all the command bars in Visual Studio. If you have a hard time finding a command bar, the best way to find it is to look at one of the items on that command bar in Visual Studio and then search for that command in the Search box. Figure 12-17 shows how search can be used to quickly find a command bar.

Figure 12-17. Command Browser Search

After you have found the command bar, you now have the key used with the commandBars collection. As another example, here is the code to get a reference to the Debug command bar:

CommandBar commandBar = (CommandBar)commandBars["Debug"];

The Command Browser makes it easy to find the elusive names of command bars so you can add commands from your add-ins to those command bars.