2011-04-22 87 views
5

新手Visual Studio擴展問題:我想創建一個擴展,監視擊鍵和報告統計到一個可停靠的窗口(「可停靠」像解決方案資源管理器或屬性窗口)。我無法找到在編輯器中提供的不僅僅是語法高亮顯示的好教程。視覺工作室擴展與對接窗口

這一切都可以作爲擴展嗎?或者我需要創建一個加載項?我最感興趣哪些課程來完成這些高級任務?

回答

2

您將能夠通過Visual Studio加載項輕鬆實現您的想法。如果你想創建VS風格的ToolWindow,那麼按照下面的步驟。在這個例子中,VS ToolWindow託管一個TreeView,但是您可以使用您選擇的控件。我更喜歡C++ CLI,但通常以下步驟對C#或VB.Net也應該是有效的。

  1. 創建一個新的插件通過文件 - >新建 - >項目 - > OtherProjectTypes-> Extensibility-> VS外接
  2. 實現以下到您的外接類

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

我從來沒有試圖從一個外接程序處理關鍵事件,但我希望你能找到答案here

無論如何,你可以找到很多很好的教程here或搜索MZTools。