gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 03:52:39 29 4
gpt4 key购买 nike

我想在 Visual Studio 2010 的解决方案资源管理器的上下文菜单中为特定文件类型添加一个新选项。因此,例如,右键单击 *.cs 文件将显示现有的上下文菜单以及“我的新选项”。

我想知道代码会是什么样子;并且会喜欢指向开发 visual studio 插件的良好引用的指针。我看到的教程/引用资料非常糟糕。

谢谢!

最佳答案

完成这件事我花了大约 5 个小时。

有 2 个选项,Visual studio 插件(或共享插件)与 Visual studio 包。

这个包要复杂得多,可以给你更多的控制权,但对于解决方案资源管理器上的上下文菜单来说,它是不需要的。

新建项目 -> 其他项目类型 -> 可扩展性 -> Visual Studio 加载项。

这是一个演练 - Link

还有这个我关注了一些 - Link

我建议您保留添加到工具菜单的选项,直到上下文菜单正常工作,或者提供放置设置对话框的位置(如果您不编写工具-> 选项页面。

这是连接代码:

  _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;
}

请注意,您的代码中的某些字符串名称必须匹配,我不确定它们是完全匹配的,因为我昨天才这样做。

关于c# - Visual Studio 2010 插件 - 将上下文菜单添加到解决方案资源管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3017063/

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