如何:公開增益集當做工具列上的按鈕
.Visual Studio 增益集在 Visual Studio 2013 中已不適用。 您應該升級您的增益集至 VSPackage 擴充套件。 如需升級的詳細資訊,請參閱 常見問題集:將增益集轉換成 VSPackage 擴充功能。
如果您在使用 [增益集精靈] 建立增益集時,選擇要建立使用者介面 (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