方法 : コマンドを追加、処理する

Visual Studio アドインは、Visual Studio 2013 では使用されなくなりました。 アドインを VSPackage 拡張機能にアップグレードしてください。 アップグレードの詳細については、「FAQ: アドインを VSPackage 拡張に変換する」を参照してください。

次に示すオブジェクトを使用すると、Visual Studio のメニューやツール バーでコマンドを作成、処理、および操作できます。

オブジェクト名

説明

IDTCommandTarget

AddNamedCommand2 メソッドを使用して統合開発環境 (IDE) に追加したコマンドのステータスを確認したり、これらのコマンドを実行したりするメソッドを提供します。

Commands2

IDE のすべてのコマンドを表します。

Command

IDE のコマンドを表します。

CommandEvents

アドインに対するコマンド イベントを提供します。

CommandBarEvents

コマンド バーのコントロールがクリックされたときの Click イベントを提供します。

注意

作成したコマンドを特定のコマンド バーに今後表示しない場合、新規のコマンドを追加したり既存のコマンドを変更したりする場合、またはコマンドを再作成しようとする場合は、Visual Studio のすべてのインスタンスを終了し、アドインのソース コードがあるフォルダーで ReCreateCommands.reg ファイルをダブルクリックしてください。

これらのオブジェクトを使用して、次の操作を行うことができます。

  • Visual Studio の IDE にコマンド バーを追加したり、IDE からコマンド バーを削除する (AddCommandBar メソッドおよび RemoveCommandBar メソッド)。

  • 新規の名前付きコマンドをツール バーまたはメニューに追加する (AddNamedCommand2 メソッド)。

  • コマンドまたは名前付きコマンドを起動する (Raise メソッドおよび Exec メソッド)。

  • コマンドのステータスを取得する (CommandInfo メソッドおよび QueryStatus メソッド)。

    注意

    CommandBarEvents イベントは、AddNamedCommand2 を使用して追加された新規コマンド用の CommandBar コントロールに関連付けることはできません。

注意

実際に画面に表示されるダイアログ ボックスとメニュー コマンドは、アクティブな設定またはエディションによっては、ヘルプの説明と異なる場合があります。ここに記載されている手順は、全般的な開発設定が適用されているものとして記述されています。設定を変更するには、[ツール] メニューの [設定のインポートとエクスポート] をクリックします。詳細については、「Visual Studio での開発設定のカスタマイズ」を参照してください。

使用例

以下に示した例の要点を次に示します。

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

参照

処理手順

方法 : アドインを作成する

チュートリアル : ウィザードの作成

概念

プロジェクトとソリューションの制御

オートメーション オブジェクト モデルの階層図

その他の技術情報

環境ウィンドウの作成と制御

アドインおよびウィザードの作成

オートメーションと機能拡張のリファレンス