Introduction
While working on a project, I don't like to use the mouse very often. But some tasks like opening a project file in Visual Studio need to use the mouse. So I decided to implement an add-in that doesn't need any mouse strokes (but is optional) and assigns a shortcut "Ctrl+Shift+Alt+N". Here is my quick open add-in and step by step instruction on how to write such an add-in for Visual Studio .NET 2005.
Using the Code
First of all, you should create a new Visual Studio .NET 2005 add-in project. File » New » Project. Select Other Project Types » Extensibility »Visual Studio Add-in from New Project dialog. Give a name to the project like QuickOpen and click OK.
On Add-in wizard, select Create an add-in using Visual C# option, Microsoft Visual Studio 2005 as application host, type a name and description and the wizard will generate your add-in project.
After your Add-in project is created, Visual Studio opens the Connect
class to edit. Now we should create a named command so we can create a menuItem to allow users to interact with our add-in.
Command command =
commands.AddNamedCommand2(addInInstance,
"QuickOpen",
"QuickOpen",
"Quick way to open files ",
true,
59,
ref contextGUIDS,
(int)vsCommandStatus.vsCommandStatusSupported+
(int)vsCommandStatus.vsCommandStatusEnabled,
(int)vsCommandStyle.vsCommandStylePictAndText,
vsCommandControlType.vsCommandControlTypeButton);
You have your command and now you can add your command to open menu placed under the Main menu:
CommandBarPopup filePopup = (CommandBarPopup)menuBarCommandBar.Controls["File"];
CommandBarPopup openPopup = (CommandBarPopup)filePopup.Controls["Open"];
command.AddControl(openPopup.CommandBar, 4);
Now we have successfully added our menu item under File » Open with the index 4.
When the user clicks Quick Open menu item, Visual Studio will call the Exec
function that comes with the IDTCommandTarget
interface. Note that, since you may have more than one command in your add-in, Visual Studio will call that method with the CmdName
parameter that's the name of the command being called. We can optionally add keybindings for addin as follows :
command.Bindings = "Global::Ctrl+Shift+Alt+N";
Here, Global
indicates that the shortcut can be used anywhere within Visual Studio.
We've almost finished our add-in. What we have to do is to analyze our solution and display the list of files. You can enumerate DTE2.Solution.Projects
and then ProjectItems
of each project. If you also want to display pretty icons that inform the user about the type of file, you can find each ProjectItem
's type with its Kind
property. Kind
property gives you a GUID
string, so you should know which GUID
belongs to which type (or you may use Constants
class for common types).
FileCodeModel fileCodeModel = applicationObject.ActiveDocument.ProjectItem.FileCodeModel;
foreach (CodeElement codeElement in fileCodeModel.CodeElements) {
AddChilds(index,codeElement.Children);
}
private void AddChilds(int parentId,CodeElements codeElements) {
foreach (CodeElement codeElement in codeElements) {
if (codeElement.Kind != vsCMElement.vsCMElementParameter &&
codeElement.Kind != vsCMElement.vsCMElementOther) {
AddChilds(index,codeElement.Children);
}
}
}

What we have done till now is, we have placed our menu item and analyzed our solution, finally we have listed the solution files. When the user selects one of the listed files, you should check whether it is a physical file (its Kind
should be Constants.vsProjectItemKindPhysicalFile
)
Finally, we can open our file in Visual Studio with the following code:
projectItem.Open(Constants.vsViewKindPrimary).Activate();
I've used icons that ship with Visual Studio .NET 2005, placed under C:\Program Files\Microsoft Visual Studio 8\Common7\VS2005ImageLibrary\VS2005ImageLibrary.zip. I could not find any license terms for them. If any exist, please let me know.
History
- 6th March, 2006: Initial post