将项目添加到 AddIn 中的 Visual Studio 文件夹右键菜单

Posted

技术标签:

【中文标题】将项目添加到 AddIn 中的 Visual Studio 文件夹右键菜单【英文标题】:Add an Item to the visual studio folder right-click menu within AddIn 【发布时间】:2013-05-10 02:23:34 【问题描述】:

我想在 Visual Studio 2012 解决方案资源管理器中的右键单击中添加一个菜单项=>添加菜单。单击自定义项目时,我可以使用我的模板添加项目。 我开发了一个 Visual Studio 插件来做到这一点,但我遇到了一些麻烦。我可以将菜单项添加到右键菜单中,但无法满足我的要求。

    菜单项应该是“添加”的子菜单。不是根项目。

    我还需要仅当我右键单击名为“Areas”的文件夹时才显示菜单项。我不希望它在我右键单击其他文件夹时显示。

这是我的OnConnection 功能代码。我怎样才能改变它以满足我的要求。

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;

            //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:
            var bars=((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars);

            Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = bars["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 vsBarFolder = cmdBars["Web Project Folder"];
            CommandBar vsBarWebFolder = cmdBars["Web Folder"];

            //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, "ModuleAddin", "Add a Project", "Executes the command for ModuleAddin", 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)
                
                    //command.AddControl(toolsPopup.CommandBar, 1);
                    command.AddControl(vsBarFolder);
                    //CommandBarButton button = (CommandBarButton)command.AddControl(vsBarFolder, 3);
                    //button.BeginGroup = true;
                
            
            catch (System.ArgumentException argEx)
            
                System.Diagnostics.Debug.Write("Exception in HintPaths:" + argEx.ToString());
            
        
    

【问题讨论】:

对于您的请求 2,您可能想看看这篇文章。 diaryofaninja.com/blog/2014/02/18/… 【参考方案1】:

您不需要插件。

链接:http://nickmeldrum.com/blog/how-to-run-powershell-scripts-from-solution-explorer-in-visual-studio-2010

复制粘贴的博文...

第 1 步:添加“运行 powershell 脚本”作为外部工具

    在 Visual Studio 中,转到菜单:工具 |外部工具 点击“添加”按钮

    添加以下表单值:

    标题:“在输出窗口中运行 Powershell 脚本” 命令:“C:\windows\system32\windowspowershell\v1.0\powershell.exe” 参数:" -file "$(ItemPath)" 初始目录:“$(ItemDir)” 勾选“使用输出窗口” (退出时关闭现在将自动开启)

    点击“应用”按钮

    点击“添加”按钮

    添加以下表单值:

    标题:“在 VS 之外运行 powershell 脚本” 命令:“C:\windows\system32\windowspowershell\v1.0\powershell.exe” 参数:" -file "$(ItemPath)" 初始目录:“$(ItemDir)” 不要勾选“使用输出窗口” 勾选“退出时关闭” 点击“确定”按钮

它们应该看起来像这样:

第 2 步:奇怪的步骤,相信我!

检查它在外部工具列表中的索引位置。默认情况下,我的位置是 6 和 7。(我认为默认情况下 Create GUID 是第 1 位!)

第 3 步:将其连接到上下文菜单

    转到菜单:工具 |定制 |命令 点击“上下文菜单”单选选项 向下滚动到“项目和解决方案上下文菜单 | 项”(噩梦般的长菜单,输入“Proj”即可大致找到正确的位置) 点击“添加命令”按钮 选择类别:“工具”和命令:“外部命令 7”(或您从“奇怪的步骤 2”获得的任何位置) 点击“确定”按钮 然后设置第二条命令: 选择类别:“工具”和命令:“外部命令 8”(或其他任何您的位置) 再次点击“确定”按钮 移动它们直到您对它们的订单感到满意(我通常将它们放在“打开方式...”下方的某个位置)

第 4 步:添加键盘快捷键

    转到菜单:工具 |选项 选择环境 |键盘部分 在列表中找到 Tools.ExternalCommandN 项(又是噩梦般的长列表,输入“工具”让您再次大致到达那里) 为每个命令选择快捷键:我喜欢 CTRL SHIFT PCTRL SHIFT ALT P 分别

【讨论】:

这太棒了 很棒的定制!现在我只需要找出如何自动化这个过程来为此编写一个设置,我会 100% 高兴。 @C4u 你有办法为此编写安装脚本吗? @crush 实现自动化的唯一方法是解析位于“AppData\Local”(在 Windows 中)路径中的 CurrentSettings.vssetings 文件。

以上是关于将项目添加到 AddIn 中的 Visual Studio 文件夹右键菜单的主要内容,如果未能解决你的问题,请参考以下文章

如何在visual studio 2012中的花括号之间添加垂直线?

使用 Visual Studio 数据库项目中的数据将外键约束添加到现有表

如何将插件添加到 Visual Studio Cordova 项目

在 Visual Studio 2008 中将目录添加到 CAB 项目

将未包含的头文件添加到 Visual Studio 项目中的最新检查

将 C++ Win32 控制台项目类集成到 Visual Studio 2008 中的 Visual C++(Windows 窗体应用程序)项目中