Welcome!

.NET Authors: Liz McMillan, Mark O'Neill, Peter Silva, Yakov Werde, Matthew Pollicove

Related Topics: .NET

.NET: Article

Extending Visual Studio 2005

Build and integrate new features into Visual Studio 2005

The Visual Studio Command model is rich and powerful enough that you can create a set of commands and then add then to virtually any exposed Menu structure, whether that happens to be in the main Menu Bar, a toolbar in the IDE, or a toolbar located on a specific tool window. We use Command Table Compiler (CTC) files to control this interaction.

To get started, open PkgCmd.ctc. Look for the section marked "CMDS_SECTION." This is where we'll add new Commands to Visual Studio for use in our Package. For our purposes, there are three pieces of data that we'll have to specify right now: a new Menu, a new Menu Group, and a new Button. The relevant portions of PkgCmd.ctc are reproduced in Listing 2.

CTC Menu Section

  • Menu uniquely identifies our new menu. This is the same data we'll feed to our Tool Window's Toolbar property later on.
  • Relative to Group specifies what group this menu will be placed on. For example, if you wanted to create a submenu on an existing menu, you'd place the existing menu's identifier here. To see this in concrete terms look at how the Tasklist Sample command is added to the View menu.
  • Priority is quite important. You should only use the top two numbers when defining the priority for a command, group, or menu. This is required by the shell to deal with the merging of items. This parameter determines where the item is to be placed in the group specified in parameter 2, 0x0000 being the highest, and 0xFF00 being the lowest.
  • Menu Type specifies what type of menu you're creating. This is how you specify whether you're creating context menus, tool bars, and so forth.
  • Text is the textual name of the menu if it were to appear as an item somewhere, either in the Tools.Customize dialog or on the menu itself.
CTC Menu Group Section
  • New Group defines the group you want to create. This allows you to place logically related commands together. For example: Cut, Copy, and Paste commands would all be placed in a group together.
  • Parent Group defines the parent menu the group will be placed on.
  • Priority defines the priority of the group in the specified parent menu.
CTC Button Section
  • Command uniquely identifies the new command you're creating.
  • Parent Group species the Primary group that this command belongs to. Every command MUST belong to a primary group. This is necessary for both Keyboard and Toolbar Customization modes. The command will show up in its primary location when the end user looks in either one of these dialogs.
  • Priority defines the priority of the command in the specified group.
  • Image defines the picture associated with this command. If you don't want an image associated with this button, pass in the value guidOfficeIcon:msotcidNoIcon.
  • Button Type determines the type of button we're defining here. This could be BUTTON or SPLITDROPDOWN, for example.
  • Visibility specifies how the command is shown or hidden. Leaving this blank means the command will always be visible and enabled.
  • Text is the text that will show up as the button text itself, or as the button's tool tip.
Defining Menus, Groups, and Commands
If you try compiling right now, you'll receive a number of pithy error messages from the CTC compiler. We need to define the new Menu, Group, and Button that we have created.

We'll start by adding these to the Native project. Open PkgCmd-ID.h and add the following #defines:

#define ToolbarMenu 0x1000
#define ToolbarMenuGroup 0x1100
#define cmdidAddTask 0x110

Next, we need to add these values to the Managed project. Open PkgCmdID.cs and add the following values:

public const int cmdidAddTask = 0x110;
public const int ToolbarMenu = 0x1000;
public const int ToolbarMenuGroup = 0x1100;

Last, but certainly not least, we need to tell our tool window about the new Toolbar we have created. Open TasklistToolwindow.cs and add the following code to the constructor:

this.ToolBar = new System.ComponentModel.Design.CommandID(
GuidList.guidTasklist_SampleCmdSet,
PkgCmdIDList.ToolbarMenu);

Detailed Task Entry UI
Now that we have a functional Toolbar with a button in our tool window, let's make it do something useful. What we're going to do is add a Windows Form to our Package that will be displayed upon clicking the Add Task button in our tool bar.

First, add a new Windows Form named AddTaskDialog.cs to your C# project (see Figure 10).

Add a label with the Text property "&Description:" and make sure you get the ampersand added. The ampersand will give the label a mnemonic, which allows your users to select the next control in the tab order by pressing Alt+D, in this case. Now, add a multiline TextBox named txtDescription.

Drag another label onto the Form and name it "&Priority:" which results in a mnemonic invokable through the key combination Alt+P. Next, add a combo box named cmbPriority with the following Items:

  • High
  • Medium
  • Low
Drag two buttons onto the Form and rename them OK and Cancel. Use the Windows Forms Designer Snaplines feature to position them at the bottom-right corner of the dialog. Cancel goes on the far right and OK sits at its left. Set their name properties to be btnOK and btnCancel, respectively.

More Stories By Aaron Brethorst

Aaron Brethorst is a program manager on the Visual Studio Core team, where he is responsible for Accessibility and User Experience across the Visual Studio IDE. Aaron joined Microsoft in 2003 after graduating from the University of Minnesota with a degree in Computer Science. When he's not developing software, you can usually find him in a Seattle coffee shop with a good book.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.