首页 > > > VS 2010插件开发无法删除菜单问题
每开一次VS都会增加多一个菜单。'

一下是代码:
OnConnection方法
1 _applicationObject = (DTE2)application;
2 _addInInstance = (AddIn)addInInst;
3 if (connectMode == Extensibility.ext_ConnectMode.ext_cm_UISetup
4 || connectMode == Extensibility.ext_ConnectMode.ext_cm_Startup)
5 {
6 // 如果是安装状态或是插件刚被启动的状态,则创建菜单
7 object[] contextGUIDS = new object[] { };
8
9 //获取IDE环境的Command集合和CommandBar集合
10 //Commands commands = applicationObject.Commands;
11 //_CommandBars commandBars = applicationObject.CommandBars;
12
13
14 Commands2 commands = (Commands2)_applicationObject.Commands;
15 //CommandBar commandBars = (CommandBar)_applicationObject.CommandBars;
16
17 //菜单条对象和工具条对象都是CommandBar类型
18 CommandBar menuObj, toolbarObj;
19 //生成新的子菜单对象,将会被插入到菜单条和工具条对象上
20 try
21 {
22
23 Command commandObj = commands.AddNamedCommand(_addInInstance,
24 "PublishUserManage",
25 "添加用户管理代码",
26 "添加用户管理的代码",
27 true,
28 127,
29 ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled
30
31 );
32
33
34 //添加主菜单
35
36 CommandBarButton buttonObj;
37
38 //创建主菜单项和工具条
39
40 menuObj = (CommandBar)_applicationObject.Commands.AddCommandBar("代码生成(&C)",
41
42 vsCommandBarType.vsCommandBarTypeMenu,
43 ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["MenuBar"], 10);
44 #region 相关帮助信息
45 /*
46 public object AddCommandBar(
47 string Name,//新命令栏的名称
48 vsCommandBarType Type,//用于确定命令栏类型的 vsCommandBarType 常数
49 CommandBar CommandBarParent,//要添加新命令栏的 Office CommandBar 对象
50 int Position// 命令栏中放置新命令栏的索引位置,从 1 开始
51 );
52 vsCommandBarType的取值为:
53 vsCommandBarTypePopup 10 弹出命令栏
54 vsCommandBarTypeToolbar 23 工具栏命令栏
55 vsCommandBarTypeMenu 24 菜单命令栏
56 */
57 #endregion 相关帮助信息
58 //toolbarObj = (CommandBar)_applicationObject.Commands.AddCommandBar("CodeTools(&C)",
59 // vsCommandBarType.vsCommandBarTypeToolbar,
60 // null,
61 // -1);
62 //toolbarObj.Position = Microsoft.Office.Core.MsoBarPosition.msoBarTop;
63 buttonObj = (CommandBarButton)commandObj.AddControl(menuObj, menuObj.Controls.Count + 1);
64 //buttonObj = (CommandBarButton)commandObj.AddControl(toolbarObj, toolbarObj.Controls.Count + 1);
65 buttonObj.Style = MsoButtonStyle.msoButtonIcon;
66
67 //将子菜单加入Project的右键菜单
68 CommandBar projBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)this._applicationObject.CommandBars)["Project"];
69 commandObj.AddControl(projBar, 1);
70
71
72 //将子菜单加入Folder的右键菜单
73 CommandBar folderBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)this._applicationObject.CommandBars)["Folder"];
74 commandObj.AddControl(folderBar, 1);
75
76 }
77 catch (Exception ex)
78 {
79 string error = ex.Message;
80 MessageBox.Show(error);
81 //throw;
82 }
83 }
OnDisconnection方法()
复制代码
1 try
2 {
3 if (disconnectMode == Extensibility.ext_DisconnectMode.ext_dm_HostShutdown ||
4 disconnectMode == Extensibility.ext_DisconnectMode.ext_dm_UserClosed)
5 {
6 Command commandObj = this._applicationObject.Commands.Item("KentAddin.Connect.PublishUserManage", -1);
7 if (commandObj != null)
8 commandObj.Delete();
9 CommandBar menuObj = ((Microsoft.VisualStudio.CommandBars.CommandBars)this._applicationObject.CommandBars)["代码生成(&C)"];
10 //CommandBar toolbarObj = ((Microsoft.VisualStudio.CommandBars.CommandBars)this._applicationObject.CommandBars)["CodeTools(&C)"];
11 if (menuObj != null)
12 {
13 this._applicationObject.Commands.RemoveCommandBar(menuObj);
14 MessageBox.Show("remove");
15 }
16 //if (toolbarObj != null)
17 // this._applicationObject.Commands.RemoveCommandBar(toolbarObj);
18 }
19 }
20 catch (Exception ex)
21 {
22 MessageBox.Show(ex.Message);
23 }来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2249/viewspace-3122162/ ,如需转载,请注明出处,否则将追究法律责任。