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

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

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

希望有代码,谢谢!
...全文
799 7 打赏 收藏 转发到动态 举报
写回复
用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下也兼容的
(题外话:从本次开始 我新增了jQuery EasyUI的专题页面 大家可以关注我的专题页来及时获取最新的EasyUI资源 专题页地址如下http:http://download.csdn.net/album/detail/343 同时也希望转载的那些朋友能保留我资源的说明及出处地址 我花那么多精力制作出来的 你们鼠标点两下就给我转走了还不注明出处 实在是不厚道 本来就是本着分享精神的 为的就是聚集一点人气和提供一个优良的环境来一起学习进步的 请不要抹杀掉我的热情 谢谢 )   时隔4个月之久 EasyUI终于迎来大版本更新了 本次更新内容诸多 除了常规维护外 还新增了3个新组件 都很实用 详细的可以阅读更新说明 里面给了详细的解读 另外 从该版本开始我将会逐步的将EasyUI官方以及第三方较好的插件API整合到API文档当中 并且会对这些插件做一些简单的Demo实现 存放到配套提供的程序包demo文件夹下 以便大家学习和使用 本期文档中将官方提供的所有附加插件的API都整理并存放到Extension节点下了 这些扩展的demo在附带的程序包中已经提供 可以用于参考使用 jQuery EasyUI 1 4版本更新内容: Bug(修复) menu:修复在删除一个菜单项的时候该菜单无法正确自适应高度的问题; datagrid:修复在datagrid宽度太小的时候“fitColumns”方法无法正常工作的问题 Improvement(改进) EasyUI的所有组件已经支持非固定 百分比大小的尺寸设置; menu:添加“showItem” “hideItem”和“resize”方法; menu:基于窗体大小自动调整高度; menu:添加“duration”属性 该属性允许用户自定义隐藏菜单动画的持续时间 以毫秒为单位; validatebox:添加“onBeforeValidate”和“onValidate”事件; combo:从该版本开始combo组件扩展自textbox组件(textbox是1 4中新增的组件); combo:添加“panelMinWidth” “panelMaxWidth” “panelMinHeight”和“panelMaxHeight”属性; searchbox:从该版本开始searchbox组件扩展自textbox组件(textbox是1 4中新增的组件); tree:添加“getRoot”方法 用于返回通过“nodeEl”参数指定的节点的顶部父节点元素 注意:官网的英文API中该函数的说明有误 其说明是none 无参数 实际这里是需要参数的 ; tree:添加“queryParams”属性; datetimebox:添加“spinnerWidth”属性; panel:添加“doLayout”方法 用于控制面板内组件的大小; panel:添加“clear”方法 用于清除面板内的内容; datagrid:允许用户设置百分比宽度的列(该功能真是千呼万唤始出来啊 ); form:添加“ajax” “novalidate”和“queryParams”属性; linkbutton:添加“resize”方法 New Plugin(新组件) textbox:该组件是一个增强的输入字段 它可以让用户非常简单的构建一个表单; datetimespinner:该组件是一个日期和时间的微调组件 它允许我们选择一个特定的日期或时间; filebox:filebox 该组件表单元素中用于上传文件的文件框工具组件 ">(题外话:从本次开始 我新增了jQuery EasyUI的专题页面 大家可以关注我的专题页来及时获取

110,534

社区成员

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

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

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