At Your Command

I received the following question in my email this morning:

"I'd like to create a new command that appears on the Project/Add context menu but I don't know where to begin. I'm not clear if I can create an OleMenuCommand in 2003 or if that's just in 2005."

OleMenuCommand/OleMenuCommandService is part of the Managed Package Framework that we're working on for Visual Studio 2005. It allows you to dynamically add commands to Visual Studio at runtime. In VS 2002 & 2003, you had to declare all possible commands that your package offered in a ctc file.

Now to the question….how do you add a command to the Project-Add context menu in VS 2003? As mentioned above, you’ll need to declare the command in your ctc file like so:

NEWGROUPS_BEGIN

    // NewGroup Parent Group Priority

    guidProjAddCommandDemoCmdSet:MyMenuGroup, guidSHLMainMenu:IDM_VS_CSCD_PROJECT_ADD, 0x0400;

NEWGROUPS_END

BUTTONS_BEGIN

    // Command Parent Group Priority Image Type Visibility

    guidProjAddCommandDemoCmdSet:cmdidMyCommand, guidProjAddCommandDemoCmdSet:MyMenuGroup, 0x0100,

guidProjAddCommandDemoCmdSet:bmpPic1, BUTTON, , "My Command";

BUTTONS_END

How did I figure that out you ask? If you look in your VSIP 7.1 folder, you’ll notice a bunch of ‘.h’ and ‘.ctc’ files in the EnvSDK\common\inc folder. These are some of the files taken directly out of Microsoft’s build lab to produce Visual Studio .NET 2003. Of particular interest is the SharedCmdPlace.ctc file. This file defines much of the layout of the menus and context menus in Visual Studio. Take a look at the NEWGROUPS section in this file. If you scroll down a bit you’ll find a block with the comment “Project node context menu groups”. A ha! This looks like what we need.

Looking at the list a little more closely now, we note 3 entries with “IDM_VS_CSCD_PROJECT_ADD” as their parent menu. Translating from GeekAbbreviation to English, that reads “Visual Studio, Cascade, Project, Add”. In other words, these lines define the 3 sub-sections of the Project-Add context menu. We want to add our own section here (with a lower priority). Now take a look again at the NEWGROUPS section that I defined. I am defining a new menu group with Project-Add as the parent menu. Now we’re on a roll. The last step is to define my new command and specify the group I just created as the parent group.

When working with Visual Studio .NET 2002/2003, you’ll need to use the above approach to add your commands to Visual Studio. In VS 2005, you’ll have the option of OleMenuCommandService to add commands dynamically from a package written in C#. However , in most instances CTC files will still be the way to go in VS 2005. Since you are highly encouraged to use delay loading for packages (don’t load until you have to) for performance reasons, you won’t be able to add commands with OleMenuCommandService until your package gets loaded and begins executing.

If you’ve been playing around with the DynamicMenu sample that’s been in the CTP builds of VSIP for the past few months, you’ll note that there is still a CTC file to define the “static” menu commands that must exist to load the package. OleMenuCommandService will be an excellent way to add commands to Visual Studio, but it won’t be a full replacement for CTC files in Visual Studio 2005.

 

A note of clarification: The other big advantage you will get in Visual Studio 2005 from OleMenuCommand/Package is the avoidance of implementing IOleCommandTarget. You can see the result from the package wizard after checking "Menu Command" to see an example.