Extending Visual Studio 2005
I have discussed at a high level the Visual Studio 2005 extensibility model and I've provided an overview of the Managed Package Framework plus the VS SDK. It has always been my opinion that the best way to really understand software development is to develop some software.
To begin, download the VS SDK from http://msdn.microsoft.com/vstudio/extend/. You will need to go through a painless registration process. In order to work with the "Experimental" version of Visual Studio 2005 (what you're downloading), you will need to have a non—express edition of Visual Studio 2005 installed on your workstation.
In addition with the new install, you will see a new start item called "Visual Studio 2005 SDK." Below that, you will see the build (I'm using the 3/2006 build), and under that you will see a number of new items. Of note, you will see "Start Visual Studio 2005 under experimental hive" which will launch VS 2005 running in the sandbox and you will also see an item that says "Reset the Visual Studio 2005 experimental hive." You guessed it, this is basically the reset button which restores your sandbox to its original versions after you dork it up when writing code. I assure you will use this at least once while working with the SDK!
Adding a Menu Item
Launch the experimental version of Visual Studio from the start menu. From the file menu select "New—>Project" and from the "Other Project Types" menu select "Extensibility" and choose "Visual Studio Integration Package" (see Figure 2). For this first example you will be adding a new button to the top—level tool bar of the Visual Studio 2005 IDE.
Click here for larger image
After your new package opens, the wizard window will pop up. The Visual Studio Integration Package Wizard does a lot of the heavy lifting for you by creating the plumbing code needed to build a package.
The first step in the wizard is the language selector; for this example you will be using C# and you should let the wizard generate the cryptographic key for you (see Figure 3). Step 2 requires you to enter some information such as Company Name, Package Name, Minimum VS Version, etc., all pretty self—explanatory.
In step 3 the wizard will ask you if you want to create a "Menu Command", "Tool Window", or "Custom Editor". You can select any combination of those, but for this example select "Menu Command". After selecting "Menu Command" in Step 3, Step 4 will display "Command Name" and "Command ID" text boxes, which will be "ErrorLog" (more on this to come) and "cmdidErrorLog" in this example. After you have typed in the information in step 4 you are done with the wizard and can click "Finish."
What Are All Of These Files?
You are probably noticing a whole bunch of files in your solution. Let's take a look at some of the files that the wizard generated. The first file is VsPkg.cs, which is the core of the project, and the VS Package. This file contains the class, "MyFirstPackage", which inherits from the Microsoft.Visual.Studio.Shell.Package class. The base class "Package" is one of the many classes in the Managed Package Framework (MPF). The "Package" class provides the implementation for IVsPackage which is stored in Microsoft.VisualStudio.Shell.Interop. Along with the implementation code you will also see a number of attributes at the top of the "MyFirstPackage" class. Without going into too much detail, these attributes are used as registration instructions. For example:
The "DefaultRegistryRoot" attribute tells the registration system (Regpkg.exe) where the default registry hive is located.
Along with VsPkg.cs, the following files have also been generated and added to the solution. One thing to note is that the files stored in the CTCComponents directory are Command Table Configuration (CTC) files. For the most part these files are C++ files which contain the meta—data used by the CTC compiler to define the layout and type of the actual commands. The CTC compiler takes all of the CTC specific files and translates them into binary that is used by Visual Studio. Don't worry if you're not a C++ person because the work that you will be doing in these files is minimal.
- CTCComponents Directory
- Guids.h CTC file containing the list of command GUID's
- PkgCmdID.h CTC file containing Command ID's
- Resource.h CTC resource ID's
- MyFirstPackage.ctc CTC file that contains layout specific directives
- Guids.cs contains all of the command GUID's, C# equivalent of Guids.h
- Key.snk The key file used to sign the assembly
- PkgCmdID contains the list of Command ID's, C# equivalent of PkgCmdID.h
- ResourcesId.cs — Contains a list of Resource IDs, C# equivalent of Resource.h
- VsPkg.cs The actual package
Make It Go!
As it stands right now, you have a simple package with a new menu command under the tools menu (see Figure 4). What if you want the command button positioned as a top—level menu item like: "File, Edit, or View" and what if you want to change that stupid "1" icon that the wizard automatically added? As previously stated, the CTC files control the layout of the commands in the IDE. So, to move the button, you just have to make a few edits to the MyFirstPackage.ctc file.
Locate the NEWGROUPS_BEGINNEWGROUPS_END section in the ctc file. This section defines menu groups that are containers for menus and or buttons (i.e., commands). In the NEWGROUPS section you will see the following code:
guidMyFirstPackageCmdSet:MyMenuGroup, guidSHLMainMenu:IDM_VS_MENU_TOOL, 0x0600;
In this code, the "MyMenuGroup" is being defined under the "Tools" menu that is denoted by the IDM_VS_MENU_TOOL ID. In order to move your menu group to the top—level toolbar, simply change the IDM_VS_MENU_TOOL to IDM_VS_TOOL_MAINMENU like this:
guidMyFirstPackageCmdSet:MyMenuGroup, guidSHLMainMenu:IDM_VS_TOOL_MAINMENU, 0x0600;
The next step is to tell CTC to change the bitmap file associated with your command. Note that all of the top—level toolbar items (i.e. "File", "Edit" etc.) are bitmaps so you must have a bitmap associated with your group. If you choose to leave the group under the Tools menu, then you can just removed the icon.
There are two sections you should examine to change the bitmap associated with the menu group. The first section, the BITMAPS_BEGINBITMAPS_END section, contains the bitmap's associated with the command.
guidMyFirstPackageCmdSet:IDB_MENU_IMAGES, bmpPic1, bmpPic2, bmpPicSmile, bmpPicX, bmpPicArrows;
The second section BUTTONS_BEGINBUTTONS_END defines the buttonmenu groups visibility, priority, etc.
guidMyFirstPackageCmdSet:cmdidErrorLog, guidMyFirstPackageCmdSet:MyMenuGroup, 0x0100, guidMyFirstPackageCmdSet:bmpPic1, BUTTON, , "ErrorLog";
As you may have already figured out, to change the icon, simply change guidMyFirstPackageCmdSet:bmpPic1 to guidMyFirstPackageCmdSet:bmpPicX. At this point you are probably asking: Can I customize the bitmaps? The answer is yes, however I am not going to cover it in this article. If you are really curious, check the "Creating Custom Bitmap Icons" in the help documentation. Additionally, by default you can use the bitmaps that ship with Windows, Office, or Visual Studio.
In Figure 5 you see that the menu has moved from the tools menu to the top—level toolbar, and the icon has changed to an "X."
Page 2 of 3