HOW TO:新增和處理命令
下列物件允許您建立、處理及操作 Visual Studio 各功能表和工具列上的命令。
物件名稱 |
描述 |
---|---|
使用 AddNamedCommand2 方法來提供方法,以決定加入至整合式開發環境 (IDE) 之命令的狀態或執行該命令。 |
|
表示 IDE 中的所有命令。 |
|
表示 IDE 中的某項命令。 |
|
提供增益集 (Add-In) 的命令事件。 |
|
提供按一下命令列上的控制項時的 Click 事件。 |
注意事項 |
---|
如果您的命令不再出現在適當的命令列上,或如果您加入新命令或修改現有的命令,或者想要重新建立命令,請關閉 Visual Studio 的所有執行個體,並在包含增益集原始程式碼之資料夾中的 ReCreateCommands.reg 檔案上按兩下。 |
使用這些物件,您可以:
加入或移除在 Visual Studio IDE 中的命令列 (AddCommandBar 和 RemoveCommandBar 方法)。
在工具列或功能表上加入新的具名命令 (AddNamedCommand2 方法)。
取得命令的狀態 (CommandInfo 和 QueryStatus 方法)。
注意事項 對於使用 AddNamedCommand2 加入的新命令所建立的 CommandBar 控制項,您不能為它連接 CommandBarEvents 事件。
注意事項 |
---|
根據您目前使用的設定或版本,您所看到的對話方塊與功能表指令可能會與 [說明] 中描述的不同。使用 [一般開發設定] 開發了這些程序。若要變更設定,請從 [工具] 功能表中選擇 [匯入和匯出設定]。如需詳細資訊,請參閱 Visual Studio 設定。 |
範例
下列範例會使用:
Command 物件。
AddNamedCommand 方法。
AddControl 方法。
IDTCommandTarget 介面。
程序會示範如何使增益集顯示為 Visual Studio 中 [工具] 功能表上的命令。 將第一個程式碼區段加入至您所建立之增益集的 OnConnection 方法。 在 Exec 和 QueryStatus 方法中,請確定程式碼行 If cmdName = "MyAddin1.Connect.MyAddin1" Then 可反映您的增益集名稱。
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
Dim resourceManager As System.Resources. _
ResourceManager = New System.Resources. _
ResourceManager("MyAddin1.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
toolsMenuName = "Tools"
End Try
Dim commandBars As CommandBars = CType(_applicationObject _
.CommandBars, CommandBars)
Dim menuBarCommandBar As CommandBar = commandBars. _
Item("MenuBar")
Dim toolsControl As CommandBarControl = _
menuBarCommandBar.Controls.Item(toolsMenuName)
Dim toolsPopup As CommandBarPopup = CType(toolsControl, _
CommandBarPopup)
Try
Dim command As Command = commands.AddNamedCommand2 _
(_addInInstance, "MyAddin1", "MyAddin1", _
"Executes the command for MyAddin1", True, 59, _
Nothing, CType(vsCommandStatus. _
vsCommandStatusSupported, Integer) + CType _
(vsCommandStatus.vsCommandStatusEnabled, Integer), _
vsCommandStyle.vsCommandStylePictAndText, _
vsCommandControlType.vsCommandControlTypeButton)
command.AddControl(toolsPopup.CommandBar, 1)
Catch argumentException As System.ArgumentException
MsgBox(argumentException.ToString)
End Try
End If
End Sub
'Code for the QueryStatus method.
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 = "MyAddin1.Connect.MyAddin1" Then
status = CType(vsCommandStatus. _
vsCommandStatusEnabled + vsCommandStatus. _
vsCommandStatusSupported, vsCommandStatus)
Else
status = vsCommandStatus.vsCommandStatusUnsupported
End If
End If
End Sub
' Code for the Exec method.
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 = "MyAddin1.Connect.MyAddin1" Then
handled = True
Exit Sub
End If
End If
End Sub
public class Connect : Object, IDTExtensibility2, IDTCommandTarget
{
public Connect()
{
}
public void OnConnection(object application, ext_ConnectMode
connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
if(connectMode == ext_ConnectMode.ext_cm_UISetup)
{
object []contextGUIDS = new object[] { };
Commands2 commands = (Commands2)_applicationObject.Commands;
string toolsMenuName;
try
{
ResourceManager resourceManager = new
ResourceManager("MyAddin4.CommandBar",
Assembly.GetExecutingAssembly());
CultureInfo cultureInfo = new
System.Globalization.CultureInfo
(_applicationObject.LocaleID);
string resourceName =
String.Concat(cultureInfo.TwoLetterISOLanguageName,
"Tools");
toolsMenuName = resourceManager.GetString(resourceName);
}
catch
{
toolsMenuName = "Tools";
}
CommandBar menuBarCommandBar =
((CommandBars)_applicationObject.CommandBars)["MenuBar"];
CommandBarControl toolsControl =
menuBarCommandBar.Controls[toolsMenuName];
CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;
try
{
Command command = commands.AddNamedCommand2(_addInInstance,
"MyAddin4", "MyAddin4", "Executes the command for
MyAddin4", true, 59, ref contextGUIDS,
(int)vsCommandStatus.vsCommandStatusSupported+
(int)vsCommandStatus.vsCommandStatusEnabled,
(int)vsCommandStyle.vsCommandStylePictAndText,
vsCommandControlType.vsCommandControlTypeButton);
if((command != null) && (toolsPopup != null))
{
command.AddControl(toolsPopup.CommandBar, 1);
}
}
catch(System.ArgumentException)
{
}
}
}
public void QueryStatus(string commandName, vsCommandStatusTextWanted
neededText, ref vsCommandStatus status, ref object commandText)
{
if(neededText ==
vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
{
if(commandName == "MyAddin4.Connect.MyAddin4")
{
status = (vsCommandStatus)vsCommandStatus.
vsCommandStatusSupported|vsCommandStatus.
vsCommandStatusEnabled;
return;
}
}
}
public void Exec(string commandName, vsCommandExecOption executeOption,
ref object varIn, ref object varOut, ref bool handled)
{
handled = false;
if(executeOption ==
vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if(commandName == "MyAddin4.Connect.MyAddin4")
{
handled = true;
return;
}
}
}