private void addTemporaryButtonTo(Command command, CommandBarPopup popup, string caption) { CommandBarButton button = (CommandBarButton)command.AddControl(popup.CommandBar, popup.Controls.Count + 1); button.Caption = caption; button.Style = MsoButtonStyle.msoButtonCaption; button.BeginGroup = false; button.Visible = true; _buttons.Add(button); }
private void setupCommandBars() { _iceConfigurationCmd = null; try { _iceConfigurationCmd = _applicationObject.Commands.Item(_addInInstance.ProgID + ".IceConfiguration", -1); } catch(ArgumentException) { object[] contextGUIDS = new object[] { }; _iceConfigurationCmd = ((Commands2)_applicationObject.Commands).AddNamedCommand2(_addInInstance, "IceConfiguration", "Ice Configuration...", "Ice Configuration...", true, -1, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton); } if(_iceConfigurationCmd == null) { MessageBox.Show("Error initializing Ice Visual Studio Add-in.\n" + "Cannot create required commands", "Ice Visual Studio Add-in", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0); return; } CommandBar toolsCmdBar = ((CommandBars)_applicationObject.CommandBars)["Tools"]; _iceConfigurationCmd.AddControl(toolsCmdBar, toolsCmdBar.Controls.Count + 1); CommandBar projectCmdBar = projectCommandBar(); _iceConfigurationCmd.AddControl(projectCmdBar, projectCmdBar.Controls.Count + 1); }
public void AddCommand() { try { if (string.IsNullOrEmpty(this.MenuName)) { // No menu required return; } Command cmdTmp; CommandBars cmdBars = (CommandBars)appObj.CommandBars; // Check any old versions of the command are not still hanging around try { cmdTmp = appObj.Commands.Item(this.CommandName, UNKNOWN_CMD_ID); cmdTmp.Delete(); } catch { } // this is an empty array for passing into the AddNamedCommand method object[] contextUIGUIDs = null; cmdTmp = appObj.Commands.AddNamedCommand( this.addIn, this.GetType().Name, this.ButtonText, this.ToolTip, true, this.Bitmap, ref contextUIGUIDs, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled); foreach (string sMenuName in this.MenuName.Split(',')) { CommandBar pluginCmdBar = null; if (_cachedCommandBars.ContainsKey(sMenuName)) { pluginCmdBar = _cachedCommandBars[sMenuName]; } else { #if DENALI //in VS2010, performance of cmdBars[sMenuName] is terrible: http://www.mztools.com/articles/2011/MZ2011005.aspx //plus looking up cmdBars["Other Windows"] failsof the ones we're looking for are there //performs better when you look at the root level of the CommandBars since most foreach (CommandBar bar in cmdBars) { if (bar.Name == sMenuName) { pluginCmdBar = bar; break; } } //if not yet found, then recurse if (pluginCmdBar == null) { foreach (CommandBar bar in cmdBars) { pluginCmdBar = RecurseCommandBarToFindCommandBarByName(bar, sMenuName); if (pluginCmdBar != null) break; } } #else pluginCmdBar = cmdBars[sMenuName]; #endif } if (pluginCmdBar == null) { System.Windows.Forms.MessageBox.Show("Cannot get the " + this.MenuName + " menubar"); } else { if (!_cachedCommandBars.ContainsKey(sMenuName)) { _cachedCommandBars.Add(sMenuName, pluginCmdBar); } pluginCmd = cmdTmp; CommandBarButton btn; if (sMenuName == "Tools") { if (toolsCommandBarPopup == null) { toolsCommandBarPopup = (CommandBarPopup)pluginCmdBar.Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, 1, true); toolsCommandBarPopup.CommandBar.Name = "BIDSHelperToolsCommandBarPopup"; toolsCommandBarPopup.Caption = "BIDS Helper"; } btn = pluginCmd.AddControl(toolsCommandBarPopup.CommandBar, 1) as CommandBarButton; SetCustomIcon(btn); btn.BeginGroup = BeginMenuGroup; toolsCommandBarPopup.Visible = true; } else if (AddCommandToMultipleMenus) { //note, this doesn't look recursively through command bars, so non-top level command bars like "Other Windows" won't work using this option foreach (CommandBar bar in (CommandBars)(appObj.CommandBars)) { if (bar.Name == sMenuName) { if (!ShouldPositionAtEnd) { btn = pluginCmd.AddControl(bar, 1) as CommandBarButton; } else { btn = pluginCmd.AddControl(bar, bar.Controls.Count - 1) as CommandBarButton; } SetCustomIcon(btn); btn.BeginGroup = BeginMenuGroup; } } } else { if (!ShouldPositionAtEnd) { btn = pluginCmd.AddControl(pluginCmdBar, 1) as CommandBarButton; } else { btn = pluginCmd.AddControl(pluginCmdBar, pluginCmdBar.Controls.Count - 1) as CommandBarButton; } SetCustomIcon(btn); btn.BeginGroup = BeginMenuGroup; } } } } catch (System.Exception e) { MessageBox.Show("Problem registering " + this.FullName + " command: " + e.Message + "\r\n" + e.StackTrace); } }
/// <summary> /// Adds an command button to Visual Studio for the give command. /// </summary> /// <param name="command"></param> /// <param name="cmdBar"></param> /// <param name="buttonStyle"></param> /// <returns></returns> private CommandBarButton AddCommandToCmdBar(Command command, CommandBar cmdBar, MsoButtonStyle buttonStyle) { CommandBarButton tempCmdBarButton; tempCmdBarButton = (CommandBarButton)command.AddControl(cmdBar, cmdBar.Controls.Count + 1); tempCmdBarButton.BeginGroup = true; tempCmdBarButton.Style = buttonStyle; return tempCmdBarButton; }
private void setupMenu() { object[] contextGuids = new object[] { }; Commands2 commands = (Commands2)applicationObject.Commands; string toolsMenuName; try { string resourceName; ResourceManager resourceManager = new ResourceManager("PaZu.CommandBar", Assembly.GetExecutingAssembly()); CultureInfo cultureInfo = new CultureInfo(applicationObject.LocaleID); if (cultureInfo.TwoLetterISOLanguageName == "zh") { CultureInfo parentCultureInfo = cultureInfo.Parent; resourceName = String.Concat(parentCultureInfo.Name, "Tools"); } else { resourceName = String.Concat(cultureInfo.TwoLetterISOLanguageName, "Tools"); } toolsMenuName = resourceManager.GetString(resourceName); } catch { toolsMenuName = "Tools"; } Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((CommandBars)applicationObject.CommandBars)["MenuBar"]; CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName]; CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl; try { // todo: need to add custom icon instead of the icon 487 from VS // and also change vsCommandStyle.vsCommandStyleText to // vsCommandStyle.vsCommandStylePictAndText jiraCommand = commands.AddNamedCommand2( addInInstance, "PaZuShowHide", "Toggle Atlassian Connector Window", "Shows or hides Atlassian Connector window", true, 487, ref contextGuids, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStyleText, vsCommandControlType.vsCommandControlTypeButton); if ((jiraCommand != null) && (toolsPopup != null)) { jiraCommand.AddControl(toolsPopup.CommandBar, 1); } } catch (ArgumentException e) { Debug.WriteLine(e.Message); } }
/// <summary> /// Implements the OnConnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being loaded. /// </summary> /// <param name="application"> /// Root object of the host application. /// </param> /// <param name="connectMode"> /// Describes how the Add-in is being loaded. /// </param> /// <param name="addInInst"> /// Object representing this Add-in. /// </param> /// <param name="custom"> /// Array of custom params /// </param> public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom) { applicationObjectField = (DTE2)application; addInInstanceField = (AddIn)addInInst; // Only execute the startup code if the connection mode is a startup mode if (connectMode == ext_ConnectMode.ext_cm_Startup) { var contextGUIDS = new object[] { }; try { // Create a Command with name SolnExplContextMenuCS and then add it to the "Item" menubar for the SolutionExplorer commandField = applicationObjectField.Commands.AddNamedCommand(addInInstanceField, "Xsd2CodeAddin", "Run Xsd2Code generation", "Xsd2Code", true, 372, ref contextGUIDS, (int) vsCommandStatus. vsCommandStatusSupported + (int) vsCommandStatus. vsCommandStatusEnabled); projectCmdBarField = ((CommandBars)applicationObjectField.CommandBars)["Item"]; if (projectCmdBarField == null) { MessageBox.Show("Cannot get the Project menubar", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else commandField.AddControl(projectCmdBarField, 1); } catch (Exception) { } } }
private void AddControlToToolsMenu(Command command, CommandBarPopup toolsPopup) { if (command != null && toolsPopup != null) command.AddControl(toolsPopup.CommandBar, 1); }
private void ShowRightClickShortcutInCodeWindow(Command commandToShow) { Microsoft.VisualStudio.CommandBars.CommandBar commandBar = ((CommandBars)this._applicationObject.CommandBars)["Code Window"]; CommandBarControl commandBarControl = null; foreach (CommandBarControl commandBarControl2 in commandBar.Controls) { if (commandBarControl2.Caption == "Add to CodeKeep") { commandBarControl = commandBarControl2; break; } } if (commandBarControl != null) { commandBarControl.Visible = true; return; } commandToShow.AddControl(commandBar, 1); }
/// <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) { _applicationObject = (DTE2)application; _addInInstance = (AddIn)addInInst; if ((connectMode == ext_ConnectMode.ext_cm_UISetup || connectMode == Extensibility.ext_ConnectMode.ext_cm_Startup)&&!_initialized) { _usefulFunctions = new UsefulFunctions(_applicationObject, _addInInstance); string keyGlobal = "Global::"; outliner.setApplicationObject(_applicationObject); docEvents = _applicationObject.Events.get_DocumentEvents(null); docEvents.DocumentOpened += new _dispDocumentEvents_DocumentOpenedEventHandler(docEvents_DocumentOpened); eventTextEditor = _applicationObject.Events.get_TextEditorEvents(null); eventTextEditor.LineChanged += new _dispTextEditorEvents_LineChangedEventHandler(eventTextEditor_LineChanged); EnvDTE80.Events2 events = (EnvDTE80.Events2)_applicationObject.Events; eventTextEditor2 = events.get_TextDocumentKeyPressEvents(null); eventTextEditor2.BeforeKeyPress += new _dispTextDocumentKeyPressEvents_BeforeKeyPressEventHandler(eventTextEditor2_BeforeKeyPress); object[] contextGUIDS = new object[] { }; Commands2 commands = (Commands2)_applicationObject.Commands; string toolsMenuName; try { //若要将此命令移动到另一个菜单,则将“工具”一词更改为此菜单的英文版。 // 此代码将获取区域性,将其追加到菜单名中,然后将此命令添加到该菜单中。 // 您会在此文件中看到全部顶级菜单的列表 // CommandBar.resx. string resourceName; ResourceManager resourceManager = new ResourceManager("JrtCoder.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"); keyGlobal = "全局::"; } 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"]; //在 MenuBar 命令栏上查找“工具”命令栏: CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName]; CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl; //如果希望添加多个由您的外接程序处理的命令,可以重复此 try/catch 块, // 只需确保更新 QueryStatus/Exec 方法,使其包含新的命令名。 try { try { formatAllCmd = _applicationObject.Commands.Item(GetFullCmd(FormatAll), -1); formatSelectedCmd = _applicationObject.Commands.Item(GetFullCmd(FormatSelected), -1); formatSettingsCmd = _applicationObject.Commands.Item(GetFullCmd(FormatSettings), -1); AddRegionCmd = _applicationObject.Commands.Item(GetFullCmd(AddRegion), -1); } catch (System.Exception e) { } try { if (formatAllCmd == null) { formatAllCmd = commands.AddNamedCommand2(_addInInstance, FormatAll, "格式化Js", "格式化全部javascript代码", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton); } if (formatSelectedCmd == null) { formatSelectedCmd = commands.AddNamedCommand2(_addInInstance, FormatSelected, "格式化选定的Js", "格式化选定的javascript代码", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton); try { CommandBar cb = _usefulFunctions.GetCommandBar("HTML Context", null); formatSelectedCmd.AddControl(cb, cb.accChildCount + 1); cb = _usefulFunctions.GetCommandBar("Script Context", null); formatSelectedCmd.AddControl(cb, cb.accChildCount + 1); cb = _usefulFunctions.GetCommandBar("ASPX Context", null); formatSelectedCmd.AddControl(cb, cb.accChildCount + 1); } catch (System.Exception) { } } if (AddRegionCmd == null) { AddRegionCmd = commands.AddNamedCommand2(_addInInstance, AddRegion, "加入折叠区域", "加入折叠区域", true, 72, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton); try { CommandBar cb = _usefulFunctions.GetCommandBar("HTML Context", null); AddRegionCmd.AddControl(cb, cb.accChildCount + 1); cb = _usefulFunctions.GetCommandBar("Script Context", null); AddRegionCmd.AddControl(cb, cb.accChildCount + 1); cb = _usefulFunctions.GetCommandBar("ASPX Context", null); AddRegionCmd.AddControl(cb, cb.accChildCount + 1); } catch (System.Exception) { } } if (formatSettingsCmd == null) { formatSettingsCmd = commands.AddNamedCommand2(_addInInstance, FormatSettings, "js格式化参数选项", "js格式化参数配置", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton); } formatAllCmd.Bindings = new object[] { keyGlobal + "ctrl+R,ctrl+F" }; formatSelectedCmd.Bindings = new object[] { keyGlobal + "ctrl+R,ctrl+G" }; AddRegionCmd.Bindings = new object[] { keyGlobal + "ctrl+alt+K" }; } catch (System.Exception e) { } foreach (CommandBarControl control in toolsPopup.Controls) { if (control.Caption == "JrtCoder") { jrtCoderCmd = control as CommandBarPopup; break; } } if (jrtCoderCmd == null) { try { jrtCoderCmd = (CommandBarPopup)toolsPopup.Controls.Add(vsCommandBarType.vsCommandBarTypePopup, 1, null, 1, false); jrtCoderCmd.Caption = "JrtCoder"; if ((jrtCoderCmd != null)) { formatAllCmd.AddControl(jrtCoderCmd.CommandBar, 1); formatSelectedCmd.AddControl(jrtCoderCmd.CommandBar, 2); formatSettingsCmd.AddControl(jrtCoderCmd.CommandBar, 3); } } catch (System.Exception e) { } } jrtCoderCmd.Visible = true; } catch (System.ArgumentException) { //如果出现此异常,原因很可能是由于具有该名称的命令已存在。如果确实如此,则无需重新创建此命令,并且可以放心忽略此异常。 } _initialized = true; } }