开发VS2008插件怎么添加菜单项

gqqnb 2009-12-23 07:07:54
已经在VS中按向导生成了插件代码,要怎么样才能在用户右击文档弹出的菜单(有重构、组织using等)里加入自己的项目,并制作响应方法?

除此以外,如何添加工具条和按钮?

希望有代码,谢谢!
...全文
818 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
fuzhimei 2012-08-26
  • 打赏
  • 举报
回复
public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{

handled = false;
if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if(commandName == "PropertyBuild.Connect.PropertyBuild")
{
handled = true;
CreateNewFileWithCopyrightInfo();
return;
}
}
}
private DTE2 applicationObject;

public void CreateNewFileWithCopyrightInfo()
{

try
{
// Create a copyright text for each code item in the project
// Get the array of projects from the active solution
System.Array theProjects = (System.Array)applicationObject.ActiveSolutionProjects;
EnvDTE.Project theProject = null;
// make sure there is an active solution containing projects
if (theProjects.Length > 0)
{
// This add-in only works for the first project in the solution,
// This can be extended for multiple projects by putting a for loop here like
// foreach (Project theProject in theProjects)
theProject = (EnvDTE.Project)(theProjects.GetValue(0));
// Go through each project item in the project
foreach (ProjectItem p in theProject.ProjectItems)
{
Trace.WriteLine("ProjectItem = " + p.Name);
// Check to see if its a c# file
if (p.Name.Substring(p.Name.Length - 3, 3) == ".cs")
{
// Open the file as a source code file
EnvDTE.Window theWindow = p.Open(Constants.vsViewKindCode);
//Get a handle to the new document in the open window
TextDocument objTextDoc = (TextDocument)theWindow.Document.Object("TextDocument");
EditPoint objEditPoint = (EditPoint)objTextDoc.StartPoint.CreateEditPoint();
//Create an EditPoint and add some text.
objEditPoint.Insert("// This Code was created by Microgold Software Inc. for educational purposes" + @" " + "// Copyright Microgold Software Inc. " + DateTime.Now.ToLongDateString() + " ");
// Get the filename from the project item and save the file with the copyright information
string fName = p.get_FileNames(0);
p.SaveAs(fName);
}
// end if ".cs" file
}
// end for each project item
}
// end if any projects exists in projects collection
}
// end try block
catch (Exception e)
{
Trace.WriteLine(e.Message.ToString());
}

}
private AddIn _addInInstance;
}

上面的贴可同时生成右键快捷方式和工具菜单栏上的按钮
fuzhimei 2012-08-26
  • 打赏
  • 举报
回复
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;
string toolsMenuName;

try
{
//若要将此命令移动到另一个菜单,则将“工具”一词更改为此菜单的英文版。
// 此代码将获取区域性,将其追加到菜单名中,然后将此命令添加到该菜单中。
// 您会在此文件中看到全部顶级菜单的列表
// CommandBar.resx.
string resourceName;
ResourceManager resourceManager = new ResourceManager("PropertyBuild.CommandBar", Assembly.GetExecutingAssembly());
CultureInfo cultureInfo = new CultureInfo(_applicationObject.LocaleID);

if(cultureInfo.TwoLetterISOLanguageName == "zh")
{
System.Globalization.CultureInfo parentCultureInfo = cultureInfo.Parent;
resourceName = String.Concat(parentCultureInfo.Name, "Tools");
}
else
{
resourceName = String.Concat(cultureInfo.TwoLetterISOLanguageName, "Tools");
}
toolsMenuName = resourceManager.GetString(resourceName);
}
catch
{
//我们试图查找“工具”一词的本地化版本,但未能找到。
// 默认值为 en-US 单词,该值可能适用于当前区域性。
toolsMenuName = "Tools";
}

//将此命令置于“工具”菜单上。
//查找 MenuBar 命令栏,该命令栏是容纳所有主菜单项的顶级命令栏:
Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["MenuBar"];
string menuName = "Code Window";
//在 MenuBar 命令栏上查找“工具”命令栏:
CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName];
CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;

CommandBar contextCommandBar = ((CommandBars)_applicationObject.CommandBars)[menuName];
//如果希望添加多个由您的外接程序处理的命令,可以重复此 try/catch 块,
// 只需确保更新 QueryStatus/Exec 方法,使其包含新的命令名。
try
{
//将一个命令添加到 Commands 集合:
Command command = commands.AddNamedCommand2(_addInInstance, "PropertyBuild", "PropertyBuild", "Executes the command for PropertyBuild", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);

//将对应于该命令的控件添加到“工具”菜单:
if ((command != null) && (toolsPopup != null) && contextCommandBar!=null)
{

command.AddControl(contextCommandBar, 1);
command.AddControl(toolsPopup.CommandBar, 1);
}
}
catch(System.ArgumentException)
{
//如果出现此异常,原因很可能是由于具有该名称的命令
// 已存在。如果确实如此,则无需重新创建此命令,并且
// 可以放心忽略此异常。
}
}
}
gqqnb 2010-05-19
  • 打赏
  • 举报
回复

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;
string menuName = "Code Window";

//获取右键菜单的命令组。
CommandBar contextCommandBar = ((CommandBars)_applicationObject.CommandBars)[menuName];
try
{
//将一个命令添加到 Commands 集合:
Command command = commands.AddNamedCommand2(_addInInstance, "DoWithConfirm", "排序代码(带确认)", "显示一个排序窗口,供手动排序",
true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
(int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);

//将对应于该命令的控件添加到“工具”菜单:
if ((command != null) && (contextCommandBar != null))
{
command.AddControl(contextCommandBar, 1);
}
}
catch (System.ArgumentException)
{
//如果出现此异常,原因很可能是由于具有该名称的命令
// 已存在。如果确实如此,则无需重新创建此命令,并且
// 可以放心忽略此异常。
}

try
{
Command command = commands.AddNamedCommand2(_addInInstance, "DoWithoutConfirm", "排序代码", "不显示排序窗口,直接按设置的规则排序",
true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
(int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);

//将对应于该命令的控件添加到“工具”菜单:
if ((command != null) && (contextCommandBar != null))
{
command.AddControl(contextCommandBar, 1);
}
}
catch (System.ArgumentException)
{ }
}
}

public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
handled = false;
if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if (commandName == "AutoPropertyAddin.Connect.DoWithConfirm")
{
//处理事件
handled = true;
return;
}

if (commandName == "AutoPropertyAddin.Connect.DoWithoutConfirm")
{
//处理事件
handled = true;
return;
}
}
}


代码的效果是在代码编辑器区域内在右键菜单顶加两个菜单项。
lindenrty 2009-12-24
  • 打赏
  • 举报
回复
写在哪里无所谓。。保证你需要用前执行一次就可以了
最极端的可以写在初始化里面
小D 2009-12-23
  • 打赏
  • 举报
回复
路过,,,学习
gqqnb 2009-12-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lindenrty 的回复:]
C# codeif(poWatchContextMenu==null)
{
_CommandBars commandBars= Application.CommandBars;
CommandBar vsBarProject= Application.CommandBars["Code Window"];
poWatchContextMenu= vsBarProject.Controls.Add(MsoControlType.msoControlButton,1,"",1,true);
poWatchContextMenu.Caption="My Watch";
poWatchContextMenu.TooltipText="";
CommandBarEvents menuItemHandler= (CommandBarEvents)Application.DTE.Events.get_CommandBarEvents(poWatchContextMenu);
menuItemHandler.Click+=new _dispCommandBarControlEvents_ClickEventHandler(Watch_Click);

}

当时是在2003下做的 一个监视工具 2008下也兼容的
[/Quote]
谢谢啊。
但是我新手啦,来点上下文呐。还有写在哪个方法里?
lindenrty 2009-12-23
  • 打赏
  • 举报
回复

if(poWatchContextMenu == null)
{
_CommandBars commandBars = Application.CommandBars;
CommandBar vsBarProject = Application.CommandBars["Code Window"];
poWatchContextMenu = vsBarProject.Controls.Add(MsoControlType.msoControlButton, 1, "", 1, true);
poWatchContextMenu.Caption = "My Watch";
poWatchContextMenu.TooltipText = "";
CommandBarEvents menuItemHandler = (CommandBarEvents)Application.DTE.Events.get_CommandBarEvents(poWatchContextMenu);
menuItemHandler.Click += new _dispCommandBarControlEvents_ClickEventHandler(Watch_Click);

}


当时是在2003下做的 一个监视工具 2008下也兼容的

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧