gpt4 book ai didi

visual-studio-2010 - 带有停靠窗口的 Visual Studio 扩展

转载 作者:行者123 更新时间:2023-12-04 21:24:37 26 4
gpt4 key购买 nike

新手 Visual Studio 扩展问题:我想创建一个扩展来监视击键并将统计信息报告到可停靠窗口(“可停靠”,如解决方案资源管理器或属性窗口)。我无法找到在编辑器中提供的不仅仅是语法高亮的优秀教程。

这一切都可以作为扩展吗?还是我需要创建一个加载项?我对完成这些高级任务最感兴趣的类(class)是什么?

最佳答案

您将能够通过 Visual Studio 加载项轻松实现您的想法。如果要创建 VS 样式的 ToolWindow,请按照以下步骤操作。在示例中,VS ToolWindow 承载一个 TreeView,但您可以使用您选择的控件。我更喜欢 C++ CLI,但通常以下步骤也适用于 C# 或 VB.Net。

  • 通过 File->New->Project->OtherProjectTypes->Extensibility->VS Add-In 创建一个新的 Add-In
  • 在您的加载项类中实现以下内容
    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。

    关于visual-studio-2010 - 带有停靠窗口的 Visual Studio 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5757458/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com