private void onDisconnect() { if (m_socket != null) { m_socket.release(); m_socket = null; } if (m_toolBarObj != null) { m_applicationObject.Commands.RemoveCommandBar(m_toolBarObj); m_toolBarObj = null; } int nCommand = m_commandList.Length; for (int ithCmd = 0; ithCmd < nCommand; ++ithCmd) { CommandObj cmdObj = m_commandList[ithCmd]; if (cmdObj.command != null) { //m_commandList[ithCmd].command.Bindings = ""; m_commandList[ithCmd].command.Delete(); m_commandList[ithCmd].command = null; } } if (m_menuBarObj != null) { m_menuBarObj.Delete(); m_menuBarObj = null; } }
/// <summary>实现 IDTExtensibility2 接口的 OnConnection 方法。接收正在加载外接程序的通知。</summary> /// <param term='application'>宿主应用程序的根对象。</param> /// <param term='connectMode'>描述外接程序的加载方式。</param> /// <param term='addInInst'>表示此外接程序的对象。</param> /// <seealso class='IDTExtensibility2' /> public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom) { m_applicationObject = (DTE2)application; m_addInInstance = (AddIn)addInInst; if ((connectMode == ext_ConnectMode.ext_cm_Startup || connectMode == ext_ConnectMode.ext_cm_AfterStartup) && m_socket == null) { initializeCallback(); object [] contextGUIDS = new object[] { }; Commands2 commands = (Commands2)m_applicationObject.Commands; string toolsMenuName = "Tools"; //将此命令置于“工具”菜单上。 //查找 MenuBar 命令栏,该命令栏是容纳所有主菜单项的顶级命令栏: Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)m_applicationObject.CommandBars)["MenuBar"]; //在 MenuBar 命令栏上查找“工具”命令栏: // CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName]; // CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl; m_menuBarObj = menuBarCommandBar.Controls.Add(MsoControlType.msoControlPopup, Type.Missing, Type.Missing, Type.Missing, true); m_menuBarObj.Caption = "Code Atlas"; CommandBarPopup toolsPopup = (CommandBarPopup)m_menuBarObj; // 增加工具栏 //m_toolBarObj = m_applicationObject.Commands.AddCommandBar("Code Atlas Tools", vsCommandBarType.vsCommandBarTypeToolbar); //如果希望添加多个由您的外接程序处理的命令,可以重复此 try/catch 块, // 只需确保更新 QueryStatus/Exec 方法,使其包含新的命令名。 try { foreach (Command cmd in commands) { foreach (CommandObj cmdObj in m_commandList) { if (cmd.Name == "CodeAtlas.Connect." + cmdObj.name) { cmdObj.command = cmd; } } } int nCommand = m_commandList.Length; for (int ithCmd = 0; ithCmd < nCommand; ++ithCmd) { if (m_commandList[ithCmd].command == null) { m_commandList[ithCmd].command = commands.AddNamedCommand2( m_addInInstance, m_commandList[ithCmd].name, m_commandList[ithCmd].displayName, "Executes the command for CodeAtlas", false, Type.Missing, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStyleText, vsCommandControlType.vsCommandControlTypeButton); } Command cmd = m_commandList[ithCmd].command; //将对应于该命令的控件添加到“工具”菜单: cmd.AddControl(toolsPopup.CommandBar, ithCmd + 1); //cmd.AddControl(m_toolBarObj, ithCmd + 1); string key = m_commandList[ithCmd].key; if (key != null && key.Length > 0) { cmd.Bindings = key; //cmd.Bindings = "文本编辑器::alt+LEFT"; } } } catch (System.ArgumentException exception) { //如果出现此异常,原因很可能是由于具有该名称的命令 // 已存在。如果确实如此,则无需重新创建此命令,并且 // 可以放心忽略此异常。 } m_socket = new SocketThread("127.0.0.1", 12346, "127.0.0.1", 12345, onSocketCallback); m_socket.run(); } }