Gewusst wie: Hinzufügen und Ändern von Befehlen

Add-Ins für Visual Studio sind in Visual Studio 2013 veraltet. Sie müssen für Ihre Add-Ins ein Upgrade auf VSPackage-Erweiterungen durchführen. Weitere Informationen über das Durchführen eines Upgrades finden Sie unter FAQ: Konvertieren von Add-Ins in VSPackage-Erweiterungen.

Mithilfe der folgenden Objekte können Sie Befehle in Menüs und Symbolleisten von Visual Studio erstellen, verwenden und ändern.

Objektname

Beschreibungen

IDTCommandTarget

Stellt Methoden zur Statusbestimmung oder Ausführung eines Befehls bereit, der mithilfe der AddNamedCommand2-Methode zur integrierten Entwicklungsumgebung (IDE) hinzugefügt wurde.

Commands2

Stellt alle Befehle in der IDE dar.

Command

Stellt einen Befehl in der IDE dar.

CommandEvents

Stellt Befehlsereignisse für Add-Ins bereit.

CommandBarEvents

Stellt ein Click-Ereignis bereit, das ausgelöst wird, wenn auf ein Steuerelement einer Befehlsleiste geklickt wird.

Hinweis

Wenn ein Befehl nicht mehr auf der entsprechenden Befehlsleiste angezeigt wird, oder wenn Sie einen neuen Befehl hinzufügen, einen vorhandenen Befehl ändern oder einen Befehl erneut erstellen möchten, schließen Sie alle Instanzen von Visual Studio, und doppelklicken Sie auf die Datei ReCreateCommands.reg. Diese befindet sich in dem Ordner, der den Quellcode des Add-Ins enthält.

Mithilfe dieser Objekte können Sie folgende Aufgaben durchführen:

  • Hinzufügen oder Entfernen einer Befehlsleiste in der Visual Studio-IDE (AddCommandBar-Methode und RemoveCommandBar-Methode)

  • Hinzufügen eines neu benannten Befehls zu einer Symbolleiste oder zu einem Menü (AddNamedCommand2-Methode)

  • Aufrufen eines Befehls oder benannten Befehls (Raise-Methode und Exec-Methode)

  • Statusermittlung eines Befehls (CommandInfo-Methode und QueryStatus-Methode)

    Hinweis

    Sie können keine Verbindung mit einem CommandBarEvents-Ereignis für CommandBar-Steuerelemente herstellen, die für einen neuen Befehl erstellt wurden, der durch AddNamedCommand2 hinzugefügt wurde.

Hinweis

Je nach den aktiven Einstellungen oder der Version unterscheiden sich die Dialogfelder und Menübefehle auf Ihrem Bildschirm möglicherweise von den in der Hilfe beschriebenen.Bei der Entwicklung dieser Verfahren war die Option Allgemeine Entwicklungseinstellungen aktiviert.Wählen Sie im Menü Extras die Option Einstellungen importieren und exportieren aus, um die Einstellungen zu ändern.Weitere Informationen finden Sie unter Anpassen der Entwicklungseinstellungen in Visual Studio.

Beispiel

Im folgenden Beispiel werden folgende Elemente verwendet:

Durch das Verfahren wird veranschaulicht, wie Sie ein Add-In als Befehl im Menü Extras von Visual Studio anzeigen. Fügen Sie der OnConnection-Methode des Add-Ins, das Sie erstellen, den ersten Codeabschnitt hinzu. Stellen Sie in der Exec-Methode und der QueryStatus-Methode sicher, dass der Name des Add-Ins in der Zeile If cmdName = "MyAddin1.Connect.MyAddin1" Then angezeigt wird.

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;
        }
    }
}

Siehe auch

Aufgaben

Gewusst wie: Erstellen von Add-Ins

Exemplarische Vorgehensweise: Erstellen eines Assistenten

Konzepte

Steuern von Projekten und Projektmappen

Diagramm "Automationsobjektmodell"

Weitere Ressourcen

Erstellen und Steuern von Umgebungsfenstern

Erstellen von Add-Ins und Assistenten

Referenz zur Automatisierung und Erweiterbarkeit