如何:将外接程序公开为工具栏上的按钮
如果您在使用外接程序向导创建外接程序时选择了创建用户界面 (UI) 的选项,则向导为该外接程序创建**“工具”**菜单上的命令。 如果希望在较为突出或易于访问的位置(例如在 Visual Studio 工具栏上,又称“标准”工具栏)上显示外接程序,则您也可以执行这些操作。
备注
显示的对话框和菜单命令可能会与“帮助”中的描述不同,具体取决于您现用的设置或版本。这些过程是在“常规开发设置”处于活动状态时开发的。若要更改设置,请在“工具”菜单上选择“导入和导出设置”。有关更多信息,请参见 Visual Studio 设置。
过程
在标准工具栏上显示外接程序
创建或打开外接程序项目。
用下面的代码替换外接程序的代码。
示例
下面的示例演示如何创建将按钮添加到 Visual Studio“标准”工具栏上的外接程序。 (这是 Visual Studio 中的工具栏的名称。)
使用 AddNamedCommand2 方法创建外接程序的命令。
然后获取对标准工具栏的引用。
最后,使用 AddControl 方法添加一个新按钮。
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
Dim stdCmdBarCtl As CommandBarControl
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
Dim cmd As Command
Dim stdCmdBar As CommandBar
Dim cmdBarBtn As CommandBarButton
Try
_applicationObject = CType(application, DTE2)
_addInInstance = CType(addInInst, AddIn)
Select Case connectMode
Case ext_ConnectMode.ext_cm_AfterStartup, _
ext_ConnectMode.ext_cm_Startup
' Add the command
cmd = _applicationObject.Commands. _
AddNamedCommand(_addInInstance, _
"ANewCommand", "ANewCommand", _
"A new command", True, 59, Nothing, _
vsCommandStatus.vsCommandStatusSupported _
Or vsCommandStatus.vsCommandStatusEnabled)
' Reference the Visual Studio standard toolbar.
stdCmdBar =
CType(_applicationObject.CommandBars.Item _
("Standard"), _
Microsoft.VisualStudio.CommandBars.CommandBar)
' Add a button to the standard toolbar.
stdCmdBarCtl = CType(cmd.AddControl(stdCmdBar, _
stdCmdBar.Controls.Count + 1), _
Microsoft.VisualStudio.CommandBars. _
CommandBarControl)
' Set a caption for the toolbar button.
stdCmdBarCtl.Caption = "A new command bar"
' Set the toolbar's button style to an icon button.
cmdBarBtn = CType(stdCmdBarCtl, CommandBarButton)
cmdBarBtn.Style = MsoButtonStyle.msoButtonIcon
End Select
Catch e As System.Exception
System.Windows.Forms.MessageBox.Show(e.ToString)
End Try
End Sub
Public Sub OnDisconnection(ByVal disconnectMode As _
ext_DisconnectMode, ByRef custom As Array)
' Implements IDTExtensibility2.OnDisconnection()
Try
' When the add-in closes, get rid of the toolbar button.
If Not (stdCmdBarCtl Is Nothing) Then
stdCmdBarCtl.Delete()
End If
Catch e As System.Exception
System.Windows.Forms.MessageBox.Show(e.ToString)
End Try
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 = EnvDTE.vsCommandStatusTextWanted. _
vsCommandStatusTextWantedNone Then
If commandName = "cmdBar2.Connect.ANewCommand" 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 = "cmdBar2.Connect.ANewCommand" Then
handled = True
System.Windows.Forms.MessageBox.Show("Add-in running")
Exit Sub
End If
End If
End Sub
End Class