简体   繁体   English

Visual Studio 2010插件 - 将上下文菜单添加到解决方案资源管理器中

[英]Visual Studio 2010 Plug-in - Adding a context-menu to the Solution Explorer

I want to add a new option in Visual Studio 2010's solution explorer's context menu for a specific file type. 我想在Visual Studio 2010的解决方案资源管理器的特定文件类型的上下文菜单中添加一个新选项。 So for example, right clicking on a *.cs file will show the existing context menu plus "my new option". 因此,例如,右键单击* .cs文件将显示现有的上下文菜单以及“我的新选项”。

I'm wondering what the code would look like; 我想知道代码会是什么样的; and would love a pointer to a good reference for developing visual studio plug-ins. 并且喜欢指向开发visual studio插件的良好参考指针。 The tutorials/references I'm seeing are conspicuously horrid. 我看到的教程/参考文章非常可怕。

Thanks! 谢谢!

It took me about 5 hours to do this. 这花了我大约5个小时。

There are 2 options, Visual studio Add-in (or shared Add-in) vs Visual studio package. 有两个选项,Visual Studio加载项(或共享加载项)与Visual Studio包。

The package is far more complicated to give you far more control, but for a context menu on the solution explorer it is not needed. 该软件包对于提供更多控制要复杂得多,但对于解决方案资源管理器上的上下文菜单则不需要。

So new project-> Other Project Types -> Extensibility -> Visual Studio Add-in. 所以新项目 - >其他项目类型 - >可扩展性 - > Visual Studio加载项。

Here's a walk-through - Link 这是一个演练 - 链接

Also This one I followed some - Link 还有一个我跟着一些 - 链接

I recommend you leave on the option for add to tools menu until you have the context menu working, or to provide a place to put a settings dialog (if you don't write a Tool-> options page. 我建议您在上下文菜单工作之前留下添加到工具菜单的选项,或者提供放置设置对话框的位置(如果您没有编写工具 - >选项页面。

Here's the connection code: 这是连接代码:

  _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;
        if (connectMode == ext_ConnectMode.ext_cm_UISetup)
        {
            object[] contextGUIDS = new object[] { };
            Commands2 commands = (Commands2)_applicationObject.Commands;
            string toolsMenuName = "Tools";

            //Place the command on the tools menu.
            //Find the MenuBar command bar, which is the top-level command bar holding all the main menu items:
            Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["MenuBar"];

            //Find the Tools command bar on the MenuBar command bar:
            CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName];
            CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;
            // get popUp command bars where commands will be registered.
            CommandBars cmdBars = (CommandBars)(_applicationObject.CommandBars);
            CommandBar vsBarItem = cmdBars["Item"]; //the pop up for clicking a project Item
            CommandBar vsBarWebItem = cmdBars["Web Item"];
            CommandBar vsBarMultiItem = cmdBars["Cross Project Multi Item"];
            CommandBar vsBarFolder = cmdBars["Folder"];
            CommandBar vsBarWebFolder = cmdBars["Web Folder"];
            CommandBar vsBarProject = cmdBars["Project"]; //the popUpMenu for right clicking a project
            CommandBar vsBarProjectNode = cmdBars["Project Node"];

            //This try/catch block can be duplicated if you wish to add multiple commands to be handled by your Add-in,
            //  just make sure you also update the QueryStatus/Exec method to include the new command names.
            try
            {
                //Add a command to the Commands collection:
                Command command = commands.AddNamedCommand2(_addInInstance, "HintPaths", "HintPaths", "Executes the command for HintPaths", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);

                //Add a control for the command to the tools menu:
                if ((command != null) && (toolsPopup != null))
                {
                    //command.AddControl(toolsPopup.CommandBar, 1);
                    command.AddControl(vsBarProject); 
                }
            }
            catch (System.ArgumentException argEx)
            {
                System.Diagnostics.Debug.Write("Exception in HintPaths:" + argEx.ToString());
                //If we are here, then the exception is probably because a command with that name
                //  already exists. If so there is no need to recreate the command and we can 
                //  safely ignore the exception.
            }
        }
    }

This code checks to see if what the user has selected is a project for instance: 此代码检查用户选择的内容是否为项目,例如:

  private Project GetProject()
    {
        if (_applicationObject.Solution == null || _applicationObject.Solution.Projects == null || _applicationObject.Solution.Projects.Count < 1)
            return null;
        if (_applicationObject.SelectedItems.Count == 1 && _applicationObject.SelectedItems.Item(1).Project != null)
            return _applicationObject.SelectedItems.Item(1).Project;
        return null;
    }

Note that certain string names in your code have to match up and I'm not sure which ones they are quite yet as I just did this yesterday. 请注意,代码中的某些字符串名称必须匹配,我不确定它们是哪一个,就像我昨天刚刚做的那样。

I found that the best way to go was to make a Visual Studio Package instead of an Visual Studio Add-in. 我发现最好的方法是创建一个Visual Studio包而不是Visual Studio外接程序。 The vsix deployment experience is so slick - the whole thing was a really easy experience. vsix部署体验非常灵活 - 整个过程非常简单。 It only supports Visual Studio 2010, but that was good enough in my case. 它只支持Visual Studio 2010,但在我的情况下这已经足够了。

Here is the resulting vsct: 这是结果vsct:

<Commands package="guidBingfooPluginPkg">
    <Groups>
      <Group guid="guidBingfooPluginCmdSet" id="MyMenuGroup" priority="0x0600">
        <Parent guid="guidSHLMainMenu" id="IDM_VS_CTXT_ITEMNODE"/>
      </Group>
    </Groups>

    <Buttons>
      <Button guid="guidBingfooPluginCmdSet" id="cmdidfooLocalBox" priority="0x0100" type="Button">
        <Parent guid="guidBingfooPluginCmdSet" id="MyMenuGroup" />
        <!-- <Icon guid="guidImages" id="bmpPic1" /> -->
        <CommandFlag>DynamicVisibility</CommandFlag>
        <Strings>
          <CommandName>cmdidfooLocalBox</CommandName>
          <ButtonText>View in foo</ButtonText>
        </Strings>
      </Button>

      <Button guid="guidBingfooPluginCmdSet" id="cmdidfooTestBed" priority="0x0100" type="Button">
        <Parent guid="guidBingfooPluginCmdSet" id="MyMenuGroup" />
        <CommandFlag>DynamicVisibility</CommandFlag>
        <Strings>
          <CommandName>cmdidfooTestBed</CommandName>
          <ButtonText>View in foo on Test Beds</ButtonText>
        </Strings>
      </Button>

    </Buttons>

    <Bitmaps>
      <Bitmap guid="guidImages" href="Resources\Images_32bit.bmp" usedList="bmpPic1, bmpPic2, bmpPicSearch, bmpPicX, bmpPicArrows"/>
    </Bitmaps>
  </Commands>

  <Symbols>
    <GuidSymbol name="guidBingfooPluginPkg" value="{62c4a13c-cc61-44a0-9e47-33111bd323ce}" />

    <GuidSymbol name="guidBingfooPluginCmdSet" value="{59166210-d88c-4259-9809-418bc332b0ab}">
      <IDSymbol name="MyMenuGroup" value="0x1020" />
      <IDSymbol name="cmdidfooLocalBox" value="0x0100" />
      <IDSymbol name="cmdidfooTestBed" value="0x0101" />
    </GuidSymbol>

    <GuidSymbol name="guidImages" value="{2dff8307-a49a-4951-a236-82e047385960}" >
      <IDSymbol name="bmpPic1" value="1" />
      <IDSymbol name="bmpPic2" value="2" />
      <IDSymbol name="bmpPicSearch" value="3" />
      <IDSymbol name="bmpPicX" value="4" />
      <IDSymbol name="bmpPicArrows" value="5" />
    </GuidSymbol>
  </Symbols>
</CommandTable>

UPDATE: 更新:

GAX/GAT for VS2010 also available from http://msdn.microsoft.com/en-us/library/ff687173 适用于VS2010的GAX / GAT也可从http://msdn.microsoft.com/en-us/library/ff687173获得。

ORIGINAL POST 原始邮政

Well is horrid because VS is really complex. 好可怕因为VS非常复杂。 Using GAX/GAT was possible, but there's no VS2010 Version yet . 使用GAX / GAT是可能的,但还没有VS2010版本 What I suggest is downloading some samples from the Visual Studio Gallery to try to understand how the whole thing works, sadly not an easy task. 我建议从Visual Studio Gallery下载一些样本,试图了解整个过程是如何工作的,遗憾的是这不是一件容易的事。

HTH HTH

I found myself having to add an item to the code editor window context menu, which ended up being cmdBars["Script Context"] as I was wanting it specifically for JavaScript files. 我发现自己必须在代码编辑器窗口上下文菜单中添加一个项目,最终是cmdBars["Script Context"]因为我特别想要它用于JavaScript文件。

As a technique for finding this which I felt useful sharing, I added the new menu item to all (456) menu controls in visual studio with the following loop: 作为一种找到我觉得有用的技术,我将新菜单项添加到visual studio中的所有(456)菜单控件中,并使用以下循环:

foreach (CommandBar cc in cmdBars)
{
    if (cc.Index >= 1 && cc.Index <= 456)
    {
        command.AddControl(cmdBars[cc.NameLocal]);
    }
}

I then narrowed this using a divide and conquer technique by adjusting the bounds of the loop: 然后我通过调整循环的边界使用分而治之技术来缩小它:

    if (cc.Index >= 1 && cc.Index <= 256)
    ...
    if (cc.Index >= 1 && cc.Index <= 128)
    ...
    if (cc.Index >= 64 && cc.Index <= 128)
    ...etc...

Until I eventually found what I was looking for. 直到我最终找到了我想要的东西。

(The related question for this is at Visual Studio 2010 Plug-in - Adding a context-menu to the Editor Window ) (与此相关的问题是在Visual Studio 2010插件中 - 将上下文菜单添加到编辑器窗口

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio 2010插件 - 将上下文菜单添加到编辑器窗口 - Visual Studio 2010 Plug-in - Adding a context-menu to the Editor Window Visual Studio加载项-将上下文菜单项添加到solution-explorer - Visual Studio Add-In - adding a context menu item to solution-explorer Visual Studio插件,用于快速搜索解决方案中的文件 - Plug-in for Visual Studio for quick-searching files in solution Visual Studio AddIn:如何将项目特定命令添加到解决方案资源管理器上下文菜单 - Visual Studio AddIn: How do I Add Item Specific Commands to the Solution Explorer Context Menu Visual Studio包:设置自定义解决方案资源管理器上下文菜单项的可见性 - Visual Studio Package: Settings the visibility of a custom Solution Explorer context menu item 是否有Visual Studio 2010插件使用LINQ / XPATH查询XML文件? - Is there a plug-in for Visual Studio 2010 to query XML file using LINQ/XPATH? 无法访问Visual Studio 2010解决方案资源管理器中的文件夹 - Cannot access folders in Visual Studio 2010 Solution Explorer 在Visual Studio 2010的解决方案资源管理器中禁用添加引用 - Disabled Add Reference in Solution Explorer on Visual Studio 2010 Visual Studio 2010问题-解决方案和源代码管理资源管理器不匹配-将项目添加到子文件夹仍然映射到根目录 - Visual Studio 2010 Issue - Solution & Source Control explorer mismatch - Adding projects to sub-folder still maps to root 在Visual Studio解决方案资源管理器中将菜单项添加到.cs文件(仅)? - Add menu item to .cs files (only) in Visual Studio solution explorer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM