Visual Studio扩展程序带有停靠窗口

5
新手Visual Studio扩展问题:我想创建一个扩展,监视键盘输入并将统计数据报告给一个可停靠窗口(“可停靠”类似于解决方案资源管理器或属性窗口)。我在寻找好的教程时遇到了困难,这些教程不仅仅提供编辑器中的语法高亮。
这一切都可以作为扩展完成吗?还是我需要创建一个插件?为了完成这些高级任务,我最感兴趣的类是哪些?
1个回答

2

通过Visual Studio插件,您可以轻松实现自己的想法。如果您想创建VS样式的ToolWindow,请按照以下步骤操作。在本例中,VS ToolWindow托管TreeView,但您可以使用任何控件。我更喜欢C++ CLI,但通常下面的步骤也适用于C#或VB.Net。

  1. Create a new Add-In through File->New->Project->OtherProjectTypes->Extensibility->VS Add-In
  2. Implement the following into your Add-In class

    public ref class Connect : public IDTExtensibility2, public IDTCommandTarget
    {
    public:
    ...
    private:
       literal String^ VSToolWinGuid = "{6CCD0EE9-20DB-4636-9149-665A958D8A9A}";
    
       DTE2^ appObject;    // object which lets us access the IDE
       AddIn^ addInInstance;   // the AddIn object itself
       Window^ MainWindow;     // VS style tool Window
       TreeView^ FormTreeView; 
    
       // Instance of the Add-In button located in the VS IDE Toolbar
       CommandBarButton^ StdCommandBarButton;  
    
       void InitializeToolBarButton(); // Creates a button on the Standard Toolbar
       void ShowAddInWindow();     // Creates and displays the Tool Window
    ...
    };
    
    
    
    void Connect::OnConnection(...)
    {
       appObject = dynamic_cast<DTE2^>(Application);
       addInInstance = dynamic_cast<AddIn^>(AddInInst);
    
       // Initialize the add-in when VS is setting up it's user interface
       if (ext_ConnectMode::ext_cm_UISetup==ConnectMode)
          InitializeToolBarButton();
    }
    
    
    void Connect::Exec(...)
    {
       handled = false;
       if (vsCommandExecOption::vsCommandExecOptionDoDefault == ExecuteOption)
       {
          // when the ToolBar Button is clicked through the UI
          if (!CmdName->CompareTo("FormBrowserAddIn.Connect.FormBrowser"))
          {             
             ShowAddInWindow();
             handled = true;
             return;
          }
       }
    }
    
    
    void Connect::InitializeToolBarButton()
    {
       try
       {    
          Command^ command = nullptr;           
          try
          {  // obtain the command if it is already created         
             command = appObject->Commands->Item(addInInstance->ProgID + "." + AddInName, -1);
          }
          catch(Exception^){}
    
          // create the command if does not exists
          if (nullptr == command)
          {
             // it is better to use the newest Commands2, because it allows to create
             // the ToolBar button with the style definition at once
             EnvDTE80::Commands2^ commands2 = safe_cast<EnvDTE80::Commands2^>(appObject->Commands);
    
             // optional, determines which environment contexts (debug mode, design mode, ...) show the command
             Array^ contextGUIDs = Array::CreateInstance(Object::typeid, 0);                
    
             // create the ToolBar button for our Add-In
             command = commands2->AddNamedCommand2(addInInstance, 
                  AddInName, AddInCaption, AddInToolTip, true, 59, contextGUIDs,
                 (int)(vsCommandStatus::vsCommandStatusSupported | vsCommandStatus::vsCommandStatusEnabled),
                 (int)vsCommandStyle::vsCommandStylePict, vsCommandControlType::vsCommandControlTypeButton);
           }
    
           // Obtain the Standard command bar and insert our ToolBar button there
           VisualStudio::CommandBars::CommandBars^ commandBars;
           commandBars = (VisualStudio::CommandBars::CommandBars^)appObject->CommandBars;
           CommandBar^ stdCommandBar = commandBars["Standard"];
           StdCommandBarButton = (CommandBarButton^)command->AddControl(stdCommandBar, stdCommandBar->Controls->Count+1);
        }
        catch(Exception^ e)
        {
            MessageBox::Show(e->ToString());
        }
    }
    
    
    void Connect::ShowAddInWindow()
    {
       try
       {
          if (nullptr == MainWindow)
          {
             // obtain the assembly of the TreeView
             String^ TreeViewFullName = "System.Windows.Forms.TreeView";
             String^ assembly = Reflection::Assembly::GetAssembly(System::Windows::Forms::TreeView::typeid)->Location;
    
             // create the VS style Tool Window
             Object^ UserCtrlObject = nullptr;
             EnvDTE80::Windows2^ win = (EnvDTE80::Windows2^)appObject->Windows;
             MainWindow = win->CreateToolWindow2(addInInstance, assembly, TreeViewFullName, AddInCaption, VSToolWindowGuid, UserCtrlObject);
    
             // set-up the tree view
             FormTreeView = (TreeView^)UserCtrlObject;      
          }
    
          // refresh the content and make the add-in visible
          RefreshTreeView();
          MainWindow->Visible = true;
       }
       catch (Exception^ e)
       {
          MessageBox::Show(e->ToString());
       }
    }
    

我从未尝试过从插件中处理按键事件,但希望你能在这里找到答案。

无论如何,你可以在这里找到许多好的教程或搜索MZTools。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接