방법: 명령 추가 및 처리

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에서 개발 설정 사용자 지정을 참조하십시오.

예제

아래 예제에서는 다음을 사용합니다.

이 프로시저는 Visual Studio의 도구 메뉴에 추가 기능을 명령으로 나타나게 하는 방법을 보여 줍니다. 사용자가 만든 추가 기능의 OnConnection 메서드에 첫 번째 코드 섹션을 추가합니다. ExecQueryStatus 메서드의 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;
        }
    }
}

참고 항목

작업

방법: 추가 기능 만들기

연습: 마법사 만들기

개념

프로젝트 및 솔루션 제어

자동화 개체 모델 차트

기타 리소스

환경 창 만들기 및 제어

추가 기능 및 마법사 만들기

자동화 및 확장성 참조