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

업데이트: 2007년 11월

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

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

참고:

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

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

  1. 추가 기능의 Connect 클래스와 OnConnection() 프로시저 코드를 다음과 같이 대체하거나 변경합니다.

    using Microsoft.VisualStudio.CommandBars;
    using System.Resources;
    using System.Reflection;
    using System.Globalization;
    
    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("MyAddin1.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, "MyAddin1", "MyAddin1", "Executes  
                    the command for MyAddin1", 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)
            {
            }
        }
    }
    
  2. 두 개의 필수 프로시저인 QueryStatusExec를 추가합니다.

    public void QueryStatus(string commandName, 
      vsCommandStatusTextWanted neededText, ref vsCommandStatus status, 
      ref object commandText)
    {
        if(neededText == 
          vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
        {
            if(commandName == "MyAddin1.Connect.MyAddin1")
            {
                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 == "MyAddin1.Connect.MyAddin1")
            {
                handled = true;
                    System.Windows.Forms.MessageBox.
                      Show("add-in running.");
                return;
            }
        }
    }
    

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

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

참고 항목

작업

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

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

방법: 추가 기능 만들기

개념

자동화 개체 모델 차트