我想在Visual Studio 2010的解决方案资源管理器上下文菜单中为特定文件类型添加一个新选项。例如,右键单击*.cs文件将显示现有的上下文菜单以及"我的新选项"。
我正在想这段代码会是什么样子,并想了解一些开发Visual Studio插件的好参考资料。我看到的教程/参考资料明显很糟糕。
谢谢!
我想在Visual Studio 2010的解决方案资源管理器上下文菜单中为特定文件类型添加一个新选项。例如,右键单击*.cs文件将显示现有的上下文菜单以及"我的新选项"。
我正在想这段代码会是什么样子,并想了解一些开发Visual Studio插件的好参考资料。我看到的教程/参考资料明显很糟糕。
谢谢!
我花了大约5个小时来完成这个。
有两个选择,Visual Studio插件(或共享插件)与Visual Studio包。
为了在解决方案资源管理器上创建上下文菜单,不需要使用包,因为它更加复杂,可以给你更多的控制。
所以,新建项目->其他项目类型->可扩展性->Visual Studio插件。
这里是一个演示 - 链接
还有这个我也参考了一些 - 链接
我建议你在上下文菜单工作之前将选项保留在“添加到工具菜单”中,或者提供一个放置设置对话框的位置(如果你不编写工具->选项页面)。
这是连接代码:
_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.
}
}
}
这段代码检查用户选择的是否是一个项目,例如:
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;
}
请注意,在您的代码中,某些字符串名称必须匹配,我还不确定它们是哪些,因为我昨天刚刚做了这个。
<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>
更新:
现在也可以从http://msdn.microsoft.com/en-us/library/ff687173获取VS2010的GAX/GAT。
原始文章:
由于VS非常复杂,因此使用GAX/GAT是可能的,但是目前还没有适用于VS2010版本。我建议从Visual Studio Gallery下载一些示例来尝试理解整个过程,但这并不是一项容易的任务。
希望对你有所帮助。
我发现自己需要在代码编辑器窗口上下文菜单中添加一个项目,最终使用了 cmdBars["Script Context"]
,因为我特别想要它用于JavaScript文件。
作为一种我觉得有用的查找技巧,我使用以下循环将新菜单项添加到Visual Studio中的所有(456个)菜单控件中:
foreach (CommandBar cc in cmdBars)
{
if (cc.Index >= 1 && cc.Index <= 456)
{
command.AddControl(cmdBars[cc.NameLocal]);
}
}
if (cc.Index >= 1 && cc.Index <= 256)
...
if (cc.Index >= 1 && cc.Index <= 128)
...
if (cc.Index >= 64 && cc.Index <= 128)
...etc...