Context Menu Template (for Visual Studio 2008)

The goal of this project is to eliminate the overhead and annoyance involved in writing context menus for Windows. Once you have installed this template (the current version works for Visual Studio 2008), you can create a functional menu handler to base your project on right from the Visual Studio 2008 menus in under a minute. Since the point of this is to eliminate overhead, I will get straight to the installation instructions. If you are interested in other details, consult the notes below and the references that follow.

Installation

  1. Download the template source.
  2. Extract the contents and move the entire folder MenuXtn to the right part of the visual studio C++ appwizards directory. This depends on where you installed visual studio, but with a typical installation it will be something like this:
    C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\VCWizards\CodeWiz\ATL
    Make sure it goes into the ATL subdirectory.
  3. Take the files MenuXtn.ico and MenuXtn.vsz (from the root of the .zip archive) and move them to the VCAddClass directory, which will be something like:
    C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\VCAddClass
  4. Finally, (and this is perhaps the most disheartening part of the hack) open the file C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\VCAddClass\VCAddClass.vsdir and then paste into it the following line, on its own line:
    MenuXtn.vsz|0|Context Menu|300|Context Menu Hander Shell Extension|0|0|8224|MyMenu
    I'm pretty sure you can add this line anywhere in the file, but I think you may need to ensure there is a blank line at the end of the file. I've included a copy of my VCAddClass.vsdir file for your reference. Note: on Vista, the program files directory may require administrative rights to write to it, so run your text editor (e.g. notepad, or visual studio) as an administrator, and open the file from there.
Okay! The worst is over. We should now have the template installed and we can get to the fun part.

Usage

  1. Open Visual Studio 2008. Note: if you are using Vista, open Visual Studio as an administrator so that the registration step at the end of the build goes through.
  2. Go to File--->New--->Project from the menus (or hit Ctrl+Shift+N).
  3. The "New Project" dialog should appear. Under Visual C++, select "ATL Project". Give it a name, (maybe "shellexttest" or something) click OK, and then Finish, keeping all the default settings.
  4. Now from the Project Menu, select "Add Class..." and another dialog should appear. On the left panel, select Visual C++, and then on the right panel, somewhere near the bottom you should see an item called "Context Menu", which is our template. Select "Context Menu" and click "Add".
  5. Type an identifier of some sort for the "short name", perhaps something like "test" and the other fields will be populated for you.
  6. At the bottom, you'll see an entry for the file class- this determines the type of files you want the extension to operate on- it defaults to txtfile (text files). Just leave it alone for this test.
  7. Click "Finish", and in a few moments, all the code you need will be generated and the project settings will be updated.
  8. If you have a 64 bit OS, create a build configuration for x64 and select that configuration. Otherwise, proceed to the next step.
  9. Build the solution (F7). Note that there might be a few compiler warnings because I still use the old-school string manipulation functions.
That's about all there is to it. To test it out, open up explorer and browse around for some text files. When you right click, you ought to see a new menu item that looks like this:
The menu items just echo which file(s) were selected along with the sub-menu that was invoked, but it should be pretty easy to modify my code for your purposes. Just browse the .cpp file for the new class (in our example it would have been named testMenu.cpp). The comments I've put in with each member function briefly explain their role in the shell's context menu interface. If you want to learn more, visit the references. Hopefully this saves you some time.

Notes

References