Imports System
Imports Microsoft.VisualStudio.CommandBars
Imports Extensibility
Imports EnvDTE
Imports EnvDTE80
Public Class Connect
Implements IDTExtensibility2
Implements IDTCommandTarget
Dim _applicationObject As DTE2
Dim _addInInstance As AddIn
Public Sub New()
End Sub
Public Sub OnConnection(ByVal application As Object, _
ByVal connectMode As ext_ConnectMode, ByVal addInInst _
As Object, ByRef custom As Array) Implements _
IDTExtensibility2.OnConnection
_applicationObject = CType(application, DTE2)
_addInInstance = CType(addInInst, AddIn)
If connectMode = ext_ConnectMode.ext_cm_UISetup Then
Dim commands As Commands2 = CType(_applicationObject. _
Commands, Commands2)
Dim toolsMenuName As String
Try
' To move the command to a different
' menu, change the word, Tools, to the English version
' of the menu. This code will take the culture, append
' the name of the menu, and then add the command to
' that menu. A list of all the top-level menus is
' in the file, CommandBar.resx.
Dim resourceManager As System.Resources. _
ResourceManager = New System.Resources. _
ResourceManager("MyAddin3.CommandBar", _
System.Reflection.Assembly.GetExecutingAssembly())
Dim cultureInfo As System.Globalization.CultureInfo _
= New System.Globalization. _
CultureInfo(_applicationObject.LocaleID)
toolsMenuName = resourceManager.GetString(String. _
Concat (cultureInfo.TwoLetterISOLanguageName, _
"Tools"))
Catch e As Exception
' We tried to find a localized version of the word,
' Tools, but one was not found. Default to the en-US
' word, which may work for the current culture.
toolsMenuName = "Tools"
End Try
' Place the command on the Tools menu.
' Find the MenuBar command bar, which is the top-level
' command bar holding all the main menu items:
Dim commandBars As CommandBars = _
CType(_applicationObject.CommandBars, _
CommandBars)
Dim menuBarCommandBar As CommandBar = _
commandBars.Item("MenuBar")
' Find the Tools command bar on the MenuBar command bar.
Dim toolsControl As CommandBarControl = _
menuBarCommandBar.Controls.Item(toolsMenuName)
Dim toolsPopup As CommandBarPopup = CType(toolsControl, _
CommandBarPopup)
Try
' Add a command to the Commands collection.
Dim command As Command = _
commands.AddNamedCommand2(_addInInstance, _
"MyAddin3", "MyAddin3", "Executes the command for _
MyAddin3", False, 1, Nothing, _
CType(vsCommandStatus.vsCommandStatusSupported, _
Integer) + CType(vsCommandStatus. _
vsCommandStatusEnabled, Integer), _
vsCommandStyle.vsCommandStylePictAndText, _
vsCommandControlType.vsCommandControlTypeButton)
' Find the appropriate command bar on the MenuBar
' command bar.
command.AddControl(toolsPopup.CommandBar, 1)
Catch argumentException As System.ArgumentException
' If we are here, then the exception is probably
' because a command with that name already exists. If
' so there is no need to recreate
' the command and we can safely ignore the exception.
End Try
End If
End Sub
Public Sub OnDisconnection(ByVal disconnectMode As _
ext_DisconnectMode, ByRef custom As Array) Implements _
IDTExtensibility2.OnDisconnection
End Sub
Public Sub OnAddInsUpdate(ByRef custom As Array) Implements _
IDTExtensibility2.OnAddInsUpdate
End Sub
Public Sub OnStartupComplete(ByRef custom As Array) Implements _
IDTExtensibility2.OnStartupComplete
End Sub
Public Sub OnBeginShutdown(ByRef custom As Array) Implements _
IDTExtensibility2.OnBeginShutdown
End Sub
Public Sub QueryStatus(ByVal commandName As String, ByVal _
neededText As vsCommandStatusTextWanted, ByRef status As _
vsCommandStatus, ByRef commandText As Object) Implements _
IDTCommandTarget.QueryStatus
If neededText = vsCommandStatusTextWanted. _
vsCommandStatusTextWantedNone Then
If commandName = "MyAddin3.Connect.MyAddin3" Then
status = CType(vsCommandStatus.vsCommandStatusEnabled _
+ vsCommandStatus.vsCommandStatusSupported, _
vsCommandStatus)
Else
status = vsCommandStatus.vsCommandStatusUnsupported
End If
End If
End Sub
Public Sub Exec(ByVal commandName 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 commandName = "MyAddin3.Connect.MyAddin3" Then
handled = True
MsgBox("Add-in is running.")
Exit Sub
End If
End If
End Sub
End Class