Visual Studio .Net Add-In
2005-08-04 22:45
381 查看
Source Code: AddInsInTheIDE.zip
Sometimes its desirable to integrate added functionality directly into the environment. Add-Ins are a powerful way to do this. In this example well create an add-in that allows you to insert a copyright statement at the top of your code window.
To create an add-in in Visual Studio .NET , open File->New->Project from the menu and choose the Other Projects tab. Pick the Extensibility Projects Folder, and inside this folder choose the Visual Studio .NET Add-in. Change the name to the desired add-in and click OK.
Sometimes its desirable to integrate added functionality directly into the environment. Add-Ins are a powerful way to do this. In this example well create an add-in that allows you to insert a copyright statement at the top of your code window.
To create an add-in in Visual Studio .NET , open File->New->Project from the menu and choose the Other Projects tab. Pick the Extensibility Projects Folder, and inside this folder choose the Visual Studio .NET Add-in. Change the name to the desired add-in and click OK.
Figure 1 - Choosing an Add-In Project The next step brings up the Add-In wizard. Click Next when the Welcome wizard screen comes up and this brings up the language choice screen shown below: Figure 2 Choosing an Add-In Language in the Add-In Wizard This Wizard screen gives the choice of writing your add-in in either C#, VB, or Visual C++. We choose C# being a mainly a fan of this language. The next wizard screen allows us to run the add-in either in the VS.NET IDE or the Macro IDE or both. Figure 3 Wizard screen for IDE to insert the add-in The next screen allows you to add a name and description of your add-in. This will appear later in the Add-In manager dialog located in the Tools menu. Figure 4 Name and Description Entry screen for the add-in Clicking Next brings us to a screen that allows us to set certain options for our add-in using various checkboxes. The screen is shown below in Figure 5. Figure 5 Miscellaneous options for the add-in The Options screen allows options for the following: 1) Create an additional menu item with a happy face in the Tools Menu for activating the add-in 2) Determine when your add-in loads, for example when the VSIDE starts. 3) Setup the availability of the add-in, whether all users can use it, or only the person who is currently logged on to the computer. The next screen gives the programmer the option of adding an about box and some technical support information of who to contact should the add-in produce strange results. Figure 6 About Box for technical support of the add-in The last screen is the Summary screen and displays a summary of all of the options the programmer has chosen for our custom add-in. Click Finish to generate a Solution containing an add-in code project and an add-in setup project. Figure 7 Add-In Wizard Summary Screen The results of clicking finish are shown below. Only one class is produced in C# called the Connect class. This class is where you can make your initial changes to the addin. To build the setup project (CopyrRightAddInSetup in this case), youll need to go into the Configuration Manager in the Build menu and check the build, because by default it is unchecked. Figure 8 CopyRightAddIn Solution and its corresponding Setup Solution So where do we begin execution of our command? There is a method called Exec created by the wizard. This command is called when the user chooses the Add-In from the Tools menu. This menu item is installed in the Tools menu on start up as we specified in the wizard shown in figure 9 below. You can prove this to yourself right away by running the add-in in debug mode by clicking F5. Figure 9 Shows Add-In installed in the start menu. So by clicking the Happy Face in the Tools menu above, the Exec method is called shown in listing 1 below and the executionOption with command name CopyRightAddIn.Connect.CopyrRightAddIn: Listing 1 - The Exec method in the Connect class of the Add-In
private _DTE applicationObject; The Application object allows us to access all of the hierarchy objects of the extensibility interface. Below is the code that utilizes the application object to create a new code file and inserts the copyright lines into our code Listing 2 - Using the Add-In to create a New File headed with Copyright information
Figure 10 Results of running the add-in A more interesting add-in would be one that opens up every C# file in an existing project and inserts the copyright information at the top. Below is the code that does this. This code steps through each project item in the active solution, opens the file if its a c# file, and sticks the copyright information at the top of the existing file. Then in addition, it saves the file: Listing 3 - Using the Add-In to create copyright info in every file of a project
Listing 4 - The OnConnection Event Handler in the Connect Object
Figure 11 - Sequence Diagram illustrating Add-In behavior drawn using WithClass 2000 Basically, if the user Launches the host and the add-in set up to run on start-up, OnConnect is called. Also OnConnect is called if the user chooses to enable the add-in in the Add-In Manager. When the Add-In function is chosen from the Tools menu, Exec is called which in turn calls the CreateNewFileWithCopyrightInfo (or CreateCopyrightInEveryFileInSolution as in the second example). Conclusion Add-Ins are fairly easy to create using the Add-In Wizard. Debugging is another story. I found that you had to be careful when building the add-in because if the add-in was launched on startup, you cant rebuild if you make changes because the add-in is running. What you have to do is turn off the add-in in the add-in manager, leave IDE and then relaunch the IDE. This problem seemed to disappear when I made sure not to exit the IDE of the host application that was launched to debug the add-in and exit by stopping the debugger. The extensibility interface is fairly powerful and you can literally create myriads of add-ins that will enhance your programming experience and increase your efficiency. Hopefully this article is enough to start you on your way! |
相关文章推荐
- How to debug a Visual Studio .NET 2005 Add-In
- Registering a Visual Studio .Net Add-In without an Installer
- Qt Visual Studio Add-in: Getting Started
- 在Visual Studio.NET中使用自定义插件最大化您的生产力(三)Custom Add-Ins Help You Maximize the Productivity of Visual Studio .NET
- C++问题-Qt Visual Studio Add-in
- VS.NET的调式功能简介(Debugging in Visual Studio.NET)
- How to get Intellisense for Web.config and App.config in Visual Studio .NET?(转载)
- Create an ASP.NET Core web app in Visual Studio Code
- 'A previous installation of Qt5 Visual Studio Add-in was detected'解决方案
- Qt Visual Studio Add-in 导出的 .pri 怎么用?
- Code Template Add-In for Visual Studio.NET
- A previous installation of Qt5 Visual Studio Add-in was detected. Please uninstall it before running
- Qt Visual Studio Add-in:Managing Projects
- 在Visual Studio.NET中使用自定义插件最大化您的生产力(三)Custom Add-Ins Help You Maximize the Productivity of Visual Studio .NET
- Adding custom code to Local Reports in Visual Studio.NET 2005 (Problems & Solutions)
- 在Visual Studio Code中开发Office Add-in
- NET开发人员必备的10个Visual Studio Add-Ins
- Creating a SharePoint BCS .NET Connectivity Assembly to Crawl RSS Data in Visual Studio 2010
- Sams Teach Yourself Visual Studio .NET 2003 in 21 Days
- Qt Visual Studio Add-in:Adding Form Files to the Project