| < Free Open Study > |
|
To create the new add-in, open Visual Studio .NET. Click the New Project button in the Start window. When the New Project dialog box opens, click the Other Projects folder and then click the Extensibility Projects folder to open it. Type WinFormsAutomation as the Name of the project. Set the path of the new project, either by typing it into the Location box or by using the Browse button to select the destination path for your new add-in project. Highlight the Visual Studio .NET Add-in icon and click the OK button to start the Add-in Wizard.
In Step 1 of the Add-in Wizard, select the "Create an Add-in using Visual Basic" option and click the Next button. In Step 2, check only the Microsoft Visual Studio .NET check box and click the Next button. In Step 3, enter WinFormsAutomation as the name of your add-in. Also, enter a description such as Demo of the WinForms Automation Object Model. Click the Next button to move to Step 4.
Step 4 is where you choose options for running the add-in. Check the first box, which will cause the wizard to create a menu item on the Tools menu. Do not check the second box, which would otherwise indicate that your add-in would never put up a modal dialog box. Although this add-in will not display a UI form, it will display multiple message boxes to describe the process that the add-in is performing. Therefore, the add-in cannot be considered "command line safe". Check the third box, which indicates that you want the add-in to be loaded when the host application is started. Check the fourth box to allow anyone to use the add-in. Click the Next button to move to the next step.
Because the Add-in Wizard in the current version of Visual Studio will not create an About box, simply skip Step 5 by clicking the Next button. Step 6 is just asummary of the options you have selected. Click the Finish button to cause the wizard to create the initial code for the new add-in.
The code generated by the wizard will be almost identical to the code generated by your first use of the wizard in Chapter 2. Only the name, description, and menu command caption will be different because you have selected a different name, description, and so forth. The code in Listing 6-1 includes the standard template generated by the Add-in Wizard, with a few minor changes. The changes that you have made thus far are the same ones that you made back in the Chapter 2 wizard-generated code. These include such things as moving the CommandObj variable up to the module level, shortening the reference variable from applicationObject to oVB, deleting the add-in's menu from the IDE on shutdown of the add-in, and so forth. In Listing 6-1, the changes made to the wizard-generated code appear in bold.
Listing 6-1: Add-in Generated Code
Imports Microsoft.Office.Core Imports Extensibility Imports System.Runtime.InteropServices Imports EnvDTE #Region " Read me for Add-in installation and setup information. " ' When run, the Add-in wizard prepared the registry for the Add-in. ' At a later time, if the Add-in becomes unavailable for reasons such as: ' 1) You moved this project to a computer other than the one it was originally ' created on. ' 2) You chose 'Yes' when presented with a message asking if you wish to ' remove the Add-in. ' 3) Registry corruption. ' you will need to re-register the Add-in by building the ' WinFormsAutomationSetup project ' by right-clicking the project in the Solution Explorer, then choosing install. #End Region <GuidAttribute("B404B902-65A3-4335-8BA3-24CE23DA3E24"), ProgIdAttribute("WinFormsAutomation.Connect")> _ Public Class Connect Implements Extensibility.IDTExtensibility2 Implements IDTCommandTarget Dim oVB As EnvDTE.DTE Dim addInInstance As EnvDTE.AddIn Dim CommandObj As Command Public Sub OnBeginShutdown(ByRef custom As System.Array) Implements _ Extensibility.IDTExtensibility2.OnBeginShutdown End Sub Public Sub OnAddInsUpdate(ByRef custom As System.Array) Implements _ Extensibility.IDTExtensibility2.OnAddInsUpdate End Sub Public Sub OnStartupComplete(ByRef custom As System.Array) Implements _ Extensibility.IDTExtensibility2.OnStartupComplete End Sub Public Sub OnDisconnection(ByVal RemoveMode As _ Extensibility.ext_DisconnectMode, _ ByRef custom As System.Array) _ Implements Extensibility.IDTExtensibility2.OnDisconnection Try ' remove the addins UI menu CommandObj.Delete() Catch e As System.Exception MsgBox("OnDisconnection: " & e.Message) End Try End Sub Public Sub OnConnection(ByVal application As Object, ByVal connectMode As _ Extensibility.ext_ConnectMode, ByVal addInInst As Object, _ ByRef custom As _ System.Array) Implements Extensibility.IDTExtensibility2.OnConnection oVB = CType(application, EnvDTE.DTE) addInInstance = CType(addInInst, EnvDTE.AddIn) If connectMode = Extensibility.ext_ConnectMode.ext_cm_AfterStartup Or _ connectMode = Extensibility.ext_ConnectMode.ext_cm_Startup _ Then Dim objAddIn As AddIn = CType(addInInst, AddIn) ' When run, the Add-in wizard prepared the registry for the Add-in. ' At a later time, the Add-in or its commands may become unavailable ' for reasons such as: ' 1) You moved this project to a computer other than the one it was ' originally created on._ ' 2) You chose 'Yes' when presented with a message asking if you ' wish to remove the Add-in. ' 3) You add new commands or modify commands already defined. ' You will need to re-register the Add-in by building the ' WinFormsAutomationSetup project,_ ' right-clicking the project in the Solution Explorer, and then ' choosing install. ' Alternatively, you could execute the ReCreateCommands.reg file the ' Add-in Wizard generated in _ ' the project directory, or run 'devenv /setup' from a command prompt. Try CommandObj = oVB.Commands.AddNamedCommand(objAddIn, _ "WinFormsAutomation", "WinFormsAutomation", _ "Executes the command for WinFormsAutomation", True, 59, Nothing, 1 + 2) '1+2 == _ vsCommandStatusSupported+vsCommandStatusEnabled CommandObj.AddControl(oVB.CommandBars.Item("Tools")) Catch e As System.Exception MsgBox("OnConnection, Can't Add Command: " & e.Message) End Try End If End Sub Public Sub Exec(ByVal cmdName As String, _ ByVal executeOption As vsCommandExecOption, _ ByRef varIn As Object, _ ByRef varOut As Object, _ ByRef handled As Boolean) _ Implements IDTCommandTarget.Exec handled = True If (executeOption = vsCommandExecOption.vsCommandExecOptionDoDefault) Then If cmdName = "WinFormsAutomation.Connect.WinFormsAutomation" Then handled = True Exit Sub End If End If End Sub Public Sub QueryStatus(ByVal cmdName As String, ByVal neededText As _ vsCommandStatusTextWanted, ByRef statusOption As vsCommandStatus, _ ByRef commandText As Object) Implements IDTCommandTarget.QueryStatus If neededText = _ EnvDTE.vsCommandStatusTextWanted.vsCommandStatusTextWantedNone _ Then If cmdName = "WinFormsAutomation.Connect.WinFormsAutomation" Then statusOption = CType(vsCommandStatus.vsCommandStatusEnabled + _ vsCommandStatus.vsCommandStatusSupported, vsCommandStatus) Else statusOption = vsCommandStatus.vsCommandStatusUnsupported End If End If End Sub
| < Free Open Study > |
|