방법: 도구 메뉴에 추가 기능 노출(Visual Basic)

업데이트: 2007년 11월

추가 기능 마법사를 사용하여 추가 기능을 만들 때 이 기능을 명령으로 표시하도록 옵션을 선택하면 해당 명령이 기본적으로 도구 메뉴에 나타납니다. 그러나 추가 기능을 만들 때 이 옵션을 선택하지 않은 경우 추가 기능 마법사를 다시 실행하고 해당 옵션을 선택한 다음 기존의 코드를 새 추가 기능에 복사하기만 하면 됩니다.

이 방법을 사용할 수 없는 경우에는 다음 절차를 통해 동일한 결과를 얻을 수 있습니다.

참고:

표시되는 대화 상자와 메뉴 명령은 실제 설정이나 버전에 따라 도움말에서 설명하는 것과 다를 수 있습니다. 이러한 절차는 일반 개발 설정을 사용하여 개발되었습니다. 설정을 변경하려면 도구 메뉴에서 설정 가져오기 및 내보내기를 선택합니다. 자세한 내용은 Visual Studio 설정을 참조하십시오.

기존의 추가 기능에 메뉴 명령을 추가하려면

  1. 추가 기능의 Connect 클래스에 Implements IDTCommandTarget을 추가합니다.

    이렇게 하면 명령을 만드는 데 필요한 IDTCommandTarget 명령 인터페이스에 액세스할 수 있습니다.

  2. OnConnection 프로시저에 다음 코드를 추가합니다.

    Imports System
    Imports Microsoft.VisualStudio.CommandBars
    Imports Extensibility
    Imports EnvDTE
    Imports EnvDTE80
    
    _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
        End Try
    End If
    

    이 코드는 추가 기능을 Visual Studio에 로드("연결")할 때 실행됩니다. 이 코드에서는 ext_cm_UISetupext_ConnectMode 값을 사용하여 추가 기능이 로드되었는지 확인합니다. 즉, 추가 기능을 설치한 이후 이를 처음으로 시작했는지 확인합니다. 추가 기능을 설치 후 처음으로 시작하는 경우이면 AddNamedCommand 메서드를 사용하여 도구 메뉴에 해당 기능의 명령이 작성됩니다. 자세한 내용은 방법: 명령 추가 및 처리를 참조하십시오.

  3. Connect 클래스에 다음 프로시저를 추가합니다.

    QueryStatus 메서드는 명령의 가용성을 업데이트할 때 호출됩니다. Exec 메서드는 명령을 호출할 때 호출됩니다.

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

    IDTCommandTarget을 구현할 때마다 이 두 가지 프로시저를 추가해야 합니다. 편집기의 왼쪽 위 모퉁이에 있는 클래스 이름 드롭다운 상자에서 IDTCommandTarget을 선택하여 이 작업을 간편하게 수행할 수도 있습니다. 오른쪽 위 모퉁이에 있는 메서드 이름 드롭다운 상자에서 각 프로시저를 차례로 선택합니다. 이렇게 하면 필요한 빈 프로시저가 올바른 매개 변수와 함께 작성됩니다. 나중에 이 프로시저에 코드를 추가할 수 있습니다.

    Exec 프로시저는 사용자가 메뉴 명령을 클릭할 때 호출되므로 이때 실행하려는 코드를 여기에 삽입합니다.

참고 항목

작업

방법: 도구 메뉴에 추가 기능 노출(Visual C#)

방법: 추가 기능 관리자를 사용하여 추가 기능 제어

방법: 추가 기능 만들기

개념

자동화 개체 모델 차트