connect.vb

来自「书籍《Writing Add-ins for Visual Studio .NE」· VB 代码 · 共 222 行

VB
222
字号
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 which is 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 UIMenusSetup project 
' by right clicking the project in the Solution Explorer, then choosing install.
#End Region

<GuidAttribute("A1DD20DF-FC66-4E5D-B0C3-1831FF589E42"), _
ProgIdAttribute("UIMenus.Connect")> _
Public Class Connect

   Implements Extensibility.IDTExtensibility2
   Implements IDTCommandTarget

   Dim oVB As EnvDTE.DTE
   Dim addInInstance As EnvDTE.AddIn
   Dim CommandObj As Command
   Dim CommandObj2 As Command
   Dim CommandObj3 As Command

   ' moved to module level to make visible to
   ' AddAddinCmdBar
   Dim objAddin As AddIn

   ' Command Bar commands and object
   Dim cmdObj As Command
   Dim cmdObj2 As Command
   Dim cmdbarobj As Microsoft.Office.Core.CommandBar

   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
         CommandObj.Delete()
         CommandObj2.Delete()
         CommandObj3.Delete()
         ' new
         cmdObj.Delete()
         cmdObj2.Delete()

         ' cmdbarobj.Delete() does not work in RC so
         ' just hide the command bar
         cmdbarobj.Visible = False

      Catch e As System.Exception
         MsgBox("Error deleting menus: " & 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
      Dim iBitmap As Integer

      oVB = CType(application, EnvDTE.DTE)
      addInInstance = CType(addInInst, EnvDTE.AddIn)

      ' test for any either type startup, first or subsequent
      If connectMode = Extensibility.ext_ConnectMode.ext_cm_UISetup Or _
         connectMode = Extensibility.ext_ConnectMode.ext_cm_Startup Or _
         connectMode = Extensibility.ext_ConnectMode.ext_cm_AfterStartup _
         Then
         'Dim objAddIn As AddIn = CType(addInInst, AddIn)
         objAddin = 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 which is 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 
         ' UIMenusSetup 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, _
               "UIMenus", _
               "UIMenus One", _
               "Executes the command for UIMenus", _
               True, 59, Nothing, 1 + 2) _
               '1+2 == vsCommandStatusSupported+vsCommandStatusEnabled
            CommandObj.AddControl(oVB.CommandBars.Item("Tools"))

            CommandObj2 = oVB.Commands.AddNamedCommand(objAddin, _
              "UIMenus2", _
              "UIMenus Two", _
              "Executes the command for UIMenus", _
              True, 65, Nothing, 1 + 2) _
              '1+2 == vsCommandStatusSupported+vsCommandStatusEnabled
            CommandObj2.AddControl(oVB.CommandBars.Item("Tools"))

            CommandObj3 = oVB.Commands.AddNamedCommand(objAddin, _
               "UIMenus3", _
               "UIMenus Three", _
               "Executes the command for UIMenus", _
               True, 73, Nothing, 1 + 2) _
               '1+2 == vsCommandStatusSupported+vsCommandStatusEnabled
            CommandObj3.AddControl(oVB.CommandBars.Item("Tools"))

            ' call method to add my toolbar
            AddAddinCmdBar()

         Catch e As System.Exception
            MsgBox("Error Adding Menus: " & 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 = False
      If (executeOption = vsCommandExecOption.vsCommandExecOptionDoDefault) _
         Then
         If cmdName = "UIMenus.Connect.UIMenus" Then
            handled = True
            MsgBox("Menu one selected.")
         ElseIf cmdName = "UIMenus.Connect.UIMenus2" Then
            handled = True
            MsgBox("Menu two selected.")
         ElseIf cmdName = "UIMenus.Connect.UIMenus3" Then
            handled = True
            MsgBox("Menu three selected.")
         ElseIf cmdName = "UIMenus.Connect.MyCommand" Then
            handled = True
            MsgBox("Command bar button1 clicked.")
            Exit Sub
         ElseIf cmdName = "UIMenus.Connect.MyCommand2" Then
            handled = True
            MsgBox("Command bar button2 clicked.")
         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 = "UIMenus.Connect.UIMenus" Or _
            cmdName = "UIMenus.Connect.UIMenus2" Or _
            cmdName = "UIMenus.Connect.UIMenus3" Or _
            cmdName = "UIMenus.Connect.MyCommand" Or _
            cmdName = "UIMenus.Connect.MyCommand2" _
            Then
            statusOption = CType(vsCommandStatus.vsCommandStatusEnabled + _
            vsCommandStatus.vsCommandStatusSupported, vsCommandStatus)
         Else
            statusOption = vsCommandStatus.vsCommandStatusUnsupported
         End If
      End If
   End Sub
   Sub AddAddinCmdBar()
      'Add a reference to the Office type library to gain access to the 
      'CommandBar object.
      Dim cmds As Commands
      Try
         cmds = oVB.Commands

         ' if we have already added the toolbar, set a ptr
         ' to it, otherwise create the commandbar
         Try
            cmdbarobj = oVB.CommandBars("MycmdBar")
         Catch
            cmdbarobj = cmds.AddCommandBar("Mycmdbar", _
                vsCommandBarType.vsCommandBarTypeToolbar)
         End Try
         cmdbarobj.Visible = True

         cmdObj = oVB.Commands.AddNamedCommand(objAddin, _
            "MyCommand", _
            "Button1", _
            "Click for Button1", _
            True, 67, Nothing, 1 + 2)
         cmdObj.AddControl(cmdbarobj)

         cmdObj2 = oVB.Commands.AddNamedCommand(objAddin, _
            "MyCommand2", _
            "Button2", _
            "Click for Button2", _
            True, 68, Nothing, 1 + 2)
         cmdObj2.AddControl(cmdbarobj)
      Catch e As System.Exception
         MsgBox(e.Message)
      End Try
   End Sub
End Class

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?