private void AddApplicationObjectHandlers(EnvDTE80.DTE2 _applicationObject) { // Setup all the solution events solutionEvents = (EnvDTE.SolutionEvents)_applicationObject.Events.SolutionEvents; solutionEvents.BeforeClosing += new _dispSolutionEvents_BeforeClosingEventHandler(solutionEvents_BeforeClosing); solutionEvents.Opened += new _dispSolutionEvents_OpenedEventHandler(solutionEvents_Opened); // Setup all text editor events textEditorEvents = (EnvDTE.TextEditorEvents)_applicationObject.Events.TextEditorEvents; textEditorEvents.LineChanged += new _dispTextEditorEvents_LineChangedEventHandler(textEditorEvents_LineChanged); // Setup all window events windowEvents = (EnvDTE.WindowEvents)_applicationObject.Events.WindowEvents; windowEvents.WindowActivated += new _dispWindowEvents_WindowActivatedEventHandler(windowEvents_WindowActivated); }
private void RegisterEvents(object sender = null, EventArgs e = null) { Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread(); if (((Config)mPlugin.Options).AutoCheckoutOnEdit) { if (_registeredCommands == null) { Log.Info("Adding handlers for automatically checking out text files as you edit them"); _registeredCommands = new List <string>(); var events = (EnvDTE80.Events2)mPlugin.App.Events; mTextDocEvents = events.get_TextDocumentKeyPressEvents(null); _beforeKeyPressEventHandler = new _dispTextDocumentKeyPressEvents_BeforeKeyPressEventHandler(OnBeforeKeyPress); mTextDocEvents.BeforeKeyPress += _beforeKeyPressEventHandler; mTextEditorEvents = events.get_TextEditorEvents(null); _lineChangedEventHandler = new _dispTextEditorEvents_LineChangedEventHandler(OnLineChanged); mTextEditorEvents.LineChanged += _lineChangedEventHandler; foreach (string command in _commands) { if (RegisterHandler(command, OnCheckoutCurrentDocument)) { _registeredCommands.Add(command); } else { Log.Warning("Failed to register {0} to command '{1}'", nameof(OnCheckoutCurrentDocument), command); } } } } else if (_registeredCommands != null) { Log.Info("Removing handlers for automatically checking out text files as you edit them"); foreach (string command in _registeredCommands) { UnregisterHandler(command, OnCheckoutCurrentDocument); } _registeredCommands = null; mTextEditorEvents.LineChanged -= _lineChangedEventHandler; mTextEditorEvents = null; mTextDocEvents.BeforeKeyPress -= _beforeKeyPressEventHandler; mTextDocEvents = null; } }
public AutoCheckoutTextEdit(Plugin plugin) : base(plugin, "AutoCheckoutTextEdit", "Automatically checks out the text file on edits") { if(!Singleton<Config>.Instance.autoCheckoutOnEdit) return; Log.Info("Adding handlers for automatically checking out text files as you edit them"); mTextDocEvents = ((EnvDTE80.Events2)plugin.App.Events).get_TextDocumentKeyPressEvents(null); mTextDocEvents.BeforeKeyPress += new _dispTextDocumentKeyPressEvents_BeforeKeyPressEventHandler(OnBeforeKeyPress); mTextEditorEvents = ((EnvDTE80.Events2)plugin.App.Events).get_TextEditorEvents(null); mTextEditorEvents.LineChanged += new _dispTextEditorEvents_LineChangedEventHandler(OnLineChanged); RegisterHandler("Edit.Delete", OnCheckoutCurrentDocument); RegisterHandler("Edit.DeleteBackwards", OnCheckoutCurrentDocument); RegisterHandler("Edit.Paste", OnCheckoutCurrentDocument); }
public AutoCheckoutTextEdit(Plugin plugin) : base(plugin, "AutoCheckoutTextEdit", "Automatically checks out the text file on edits") { if (!Singleton <Config> .Instance.autoCheckoutOnEdit) { return; } Log.Info("Adding handlers for automatically checking out text files as you edit them"); mTextDocEvents = ((EnvDTE80.Events2)plugin.App.Events).get_TextDocumentKeyPressEvents(null); mTextDocEvents.BeforeKeyPress += new _dispTextDocumentKeyPressEvents_BeforeKeyPressEventHandler(OnBeforeKeyPress); mTextEditorEvents = ((EnvDTE80.Events2)plugin.App.Events).get_TextEditorEvents(null); mTextEditorEvents.LineChanged += new _dispTextEditorEvents_LineChangedEventHandler(OnLineChanged); RegisterHandler("Edit.Delete", OnCheckoutCurrentDocument); RegisterHandler("Edit.DeleteBackwards", OnCheckoutCurrentDocument); RegisterHandler("Edit.Paste", OnCheckoutCurrentDocument); }
/// <summary>Implements the OnConnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being loaded.</summary> /// <param term='application'>Root object of the host application.</param> /// <param term='connectMode'>Describes how the Add-in is being loaded.</param> /// <param term='addInInst'>Object representing this Add-in.</param> /// <seealso class='IDTExtensibility2' /> public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom) { _applicationObject = (DTE2)application; _addInInstance = (AddIn)addInInst; // Setup all the solution events solutionEvents = (EnvDTE.SolutionEvents)_applicationObject.Events.SolutionEvents; solutionEvents.BeforeClosing += new _dispSolutionEvents_BeforeClosingEventHandler(solutionEvents_BeforeClosing); solutionEvents.Opened += new _dispSolutionEvents_OpenedEventHandler(solutionEvents_Opened); // Setup all text editor events textEditorEvents = (EnvDTE.TextEditorEvents)_applicationObject.Events.TextEditorEvents; textEditorEvents.LineChanged += new _dispTextEditorEvents_LineChangedEventHandler(textEditorEvents_LineChanged); // Setup all window events windowEvents = (EnvDTE.WindowEvents)_applicationObject.Events.WindowEvents; windowEvents.WindowActivated += new _dispWindowEvents_WindowActivatedEventHandler(windowEvents_WindowActivated); switch (connectMode) { case ext_ConnectMode.ext_cm_UISetup: // We should never get here, this is temporary UI break; case ext_ConnectMode.ext_cm_Startup: // The add-in was marked to load on startup AddToolWindow(); AddToolWindowMenuItem(); break; case ext_ConnectMode.ext_cm_AfterStartup: // The add-in was loaded by hand after startup using the Add-In Manager // Initialize it in the same way that when is loaded on startup AddToolWindow(); AddToolWindowMenuItem(); break; } }
/// <summary>Implements the OnConnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being loaded.</summary> /// <param term='application'>Root object of the host application.</param> /// <param term='connectMode'>Describes how the Add-in is being loaded.</param> /// <param term='addInInst'>Object representing this Add-in.</param> /// <seealso class='IDTExtensibility2' /> public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom) { _applicationObject = (DTE2)application; _addInInstance = (AddIn)addInInst; AStyle = new AStyleInterface(); AStyle.LoadDefaults(); AStyleInterface.LoadSettings(ref AStyle); if (connectMode == ext_ConnectMode.ext_cm_UISetup) { object[] contextGUIDS = new object[] { }; Commands2 commands = (Commands2)_applicationObject.Commands; #region Set up toolbar var commandBars = (CommandBars)_applicationObject.CommandBars; // Add a new toolbar CommandBar myTemporaryToolbar = null; try { myTemporaryToolbar = commandBars[_toolBarName]; } catch { } if (myTemporaryToolbar == null) myTemporaryToolbar = commandBars.Add(_toolBarName, MsoBarPosition.msoBarTop, System.Type.Missing, false); Command optionsCommand = commands.AddNamedCommand2(_addInInstance, _optionsCommandStr, "Options", "Configure AstyleWrapper", true, 2946, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePict, vsCommandControlType.vsCommandControlTypeButton); Command executeCommand = commands.AddNamedCommand2(_addInInstance, _execCommandStr, "Format selection", "Format selection", true, 611, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePict, vsCommandControlType.vsCommandControlTypeButton); Command switchCommand = commands.AddNamedCommand2(_addInInstance, _switchCommandStr, "Switcher", AStyle.working ? _switchOffStr : _switchOnStr, true, AStyle.working ? 1087 : 1088, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePict, vsCommandControlType.vsCommandControlTypeButton); // Add a new button on that toolbar //_switchCommandBtn = (CommandBarButton)switchCommand.AddControl(myTemporaryToolbar, myTemporaryToolbar.Controls.Count + 1); _switchCommandBtn = (CommandBarButton)switchCommand.AddControl(myTemporaryToolbar, myTemporaryToolbar.Controls.Count + 1); _optionsCommandBtn = (CommandBarButton)optionsCommand.AddControl(myTemporaryToolbar, myTemporaryToolbar.Controls.Count + 1); _execCommandBtn = (CommandBarButton)executeCommand.AddControl(myTemporaryToolbar, myTemporaryToolbar.Controls.Count + 1); // Make visible the toolbar myTemporaryToolbar.Visible = true; #endregion } else { // try get buttons var commandBars = (CommandBars)_applicationObject.CommandBars; try { var myTemporaryToolbar = commandBars[_toolBarName]; _switchCommandBtn = (CommandBarButton)myTemporaryToolbar.Controls[1]; _execCommandBtn = (CommandBarButton)myTemporaryToolbar.Controls[2]; _optionsCommandBtn = (CommandBarButton)myTemporaryToolbar.Controls[3]; } catch { } _hashes = new HashSet<int>(); _textEditorEvents = _applicationObject.Events.get_TextEditorEvents(null); _textEditorEvents.LineChanged += new _dispTextEditorEvents_LineChangedEventHandler(_textEditorEvents_LineChanged); } }
public void Attach(_DTE app) { applicationObject = app; EnvDTE.Events events = applicationObject.Events; OutputWindow outputWindow = (OutputWindow)applicationObject.Windows.Item(Constants.vsWindowKindOutput).Object; //IObjectExplorerService objectExplorer = ServiceCache.GetObjectExplorer(); //var provider = (IObjectExplorerEventProvider)objectExplorer.GetService(typeof(IObjectExplorerEventProvider)); //provider.SelectionChanged += new NodesChangedEventHandler(provider_SelectionChanged); _outputWindowPane = outputWindow.OutputWindowPanes.Add("DTE Event Information - C# Event Watcher"); //Retrieve the event objects from the automation model _windowsEvents = (EnvDTE.WindowEvents)events.get_WindowEvents(null); _textEditorEvents = (EnvDTE.TextEditorEvents)events.get_TextEditorEvents(null); _taskListEvents = (EnvDTE.TaskListEvents)events.get_TaskListEvents(""); _solutionEvents = (EnvDTE.SolutionEvents)events.SolutionEvents; _selectionEvents = (EnvDTE.SelectionEvents)events.SelectionEvents; _outputWindowEvents = (EnvDTE.OutputWindowEvents)events.get_OutputWindowEvents(""); _findEvents = (EnvDTE.FindEvents)events.FindEvents; _dteEvents = (EnvDTE.DTEEvents)events.DTEEvents; _documentEvents = (EnvDTE.DocumentEvents)events.get_DocumentEvents(null); _debuggerEvents = (EnvDTE.DebuggerEvents)events.DebuggerEvents; _commandEvents = (EnvDTE.CommandEvents)events.get_CommandEvents("{00000000-0000-0000-0000-000000000000}", 0); _buildEvents = (EnvDTE.BuildEvents)events.BuildEvents; _miscFilesEvents = (EnvDTE.ProjectItemsEvents)events.MiscFilesEvents; _solutionItemsEvents = (EnvDTE.ProjectItemsEvents)events.SolutionItemsEvents; _globalProjectItemsEvents = ((EnvDTE80.Events2)events).ProjectItemsEvents; _globalProjectsEvents = ((EnvDTE80.Events2)events).ProjectsEvents; _textDocumentKeyPressEvents = ((EnvDTE80.Events2)events).get_TextDocumentKeyPressEvents(null); _codeModelEvents = ((EnvDTE80.Events2)events).get_CodeModelEvents(null); _windowVisibilityEvents = ((EnvDTE80.Events2)events).get_WindowVisibilityEvents(null); _debuggerProcessEvents = ((EnvDTE80.Events2)events).DebuggerProcessEvents; _debuggerExpressionEvaluationEvents = ((EnvDTE80.Events2)events).DebuggerExpressionEvaluationEvents; _publishEvents = ((EnvDTE80.Events2)events).PublishEvents; //Connect to each delegate exposed from each object retrieved above _windowsEvents.WindowActivated += new _dispWindowEvents_WindowActivatedEventHandler(this.WindowActivated); _windowsEvents.WindowClosing += new _dispWindowEvents_WindowClosingEventHandler(this.WindowClosing); _windowsEvents.WindowCreated += new _dispWindowEvents_WindowCreatedEventHandler(this.WindowCreated); _windowsEvents.WindowMoved += new _dispWindowEvents_WindowMovedEventHandler(this.WindowMoved); _textEditorEvents.LineChanged += new _dispTextEditorEvents_LineChangedEventHandler(this.LineChanged); _taskListEvents.TaskAdded += new _dispTaskListEvents_TaskAddedEventHandler(this.TaskAdded); _taskListEvents.TaskModified += new _dispTaskListEvents_TaskModifiedEventHandler(this.TaskModified); _taskListEvents.TaskNavigated += new _dispTaskListEvents_TaskNavigatedEventHandler(this.TaskNavigated); _taskListEvents.TaskRemoved += new _dispTaskListEvents_TaskRemovedEventHandler(this.TaskRemoved); _solutionEvents.AfterClosing += new _dispSolutionEvents_AfterClosingEventHandler(this.AfterClosing); _solutionEvents.BeforeClosing += new _dispSolutionEvents_BeforeClosingEventHandler(this.BeforeClosing); _solutionEvents.Opened += new _dispSolutionEvents_OpenedEventHandler(this.Opened); _solutionEvents.ProjectAdded += new _dispSolutionEvents_ProjectAddedEventHandler(this.ProjectAdded); _solutionEvents.ProjectRemoved += new _dispSolutionEvents_ProjectRemovedEventHandler(this.ProjectRemoved); _solutionEvents.ProjectRenamed += new _dispSolutionEvents_ProjectRenamedEventHandler(this.ProjectRenamed); _solutionEvents.QueryCloseSolution += new _dispSolutionEvents_QueryCloseSolutionEventHandler(this.QueryCloseSolution); _solutionEvents.Renamed += new _dispSolutionEvents_RenamedEventHandler(this.Renamed); _selectionEvents.OnChange += new _dispSelectionEvents_OnChangeEventHandler(this.OnChange); _outputWindowEvents.PaneAdded += new _dispOutputWindowEvents_PaneAddedEventHandler(this.PaneAdded); _outputWindowEvents.PaneClearing += new _dispOutputWindowEvents_PaneClearingEventHandler(this.PaneClearing); _outputWindowEvents.PaneUpdated += new _dispOutputWindowEvents_PaneUpdatedEventHandler(this.PaneUpdated); _findEvents.FindDone += new _dispFindEvents_FindDoneEventHandler(this.FindDone); _dteEvents.ModeChanged += new _dispDTEEvents_ModeChangedEventHandler(this.ModeChanged); _dteEvents.OnBeginShutdown += new _dispDTEEvents_OnBeginShutdownEventHandler(this.OnBeginShutdown); _dteEvents.OnMacrosRuntimeReset += new _dispDTEEvents_OnMacrosRuntimeResetEventHandler(this.OnMacrosRuntimeReset); _dteEvents.OnStartupComplete += new _dispDTEEvents_OnStartupCompleteEventHandler(this.OnStartupComplete); _documentEvents.DocumentClosing += new _dispDocumentEvents_DocumentClosingEventHandler(this.DocumentClosing); _documentEvents.DocumentOpened += new _dispDocumentEvents_DocumentOpenedEventHandler(this.DocumentOpened); _documentEvents.DocumentOpening += new _dispDocumentEvents_DocumentOpeningEventHandler(this.DocumentOpening); _documentEvents.DocumentSaved += new _dispDocumentEvents_DocumentSavedEventHandler(this.DocumentSaved); _debuggerEvents.OnContextChanged += new _dispDebuggerEvents_OnContextChangedEventHandler(this.OnContextChanged); _debuggerEvents.OnEnterBreakMode += new _dispDebuggerEvents_OnEnterBreakModeEventHandler(this.OnEnterBreakMode); _debuggerEvents.OnEnterDesignMode += new _dispDebuggerEvents_OnEnterDesignModeEventHandler(this.OnEnterDesignMode); _debuggerEvents.OnEnterRunMode += new _dispDebuggerEvents_OnEnterRunModeEventHandler(this.OnEnterRunMode); _debuggerEvents.OnExceptionNotHandled += new _dispDebuggerEvents_OnExceptionNotHandledEventHandler(this.OnExceptionNotHandled); _debuggerEvents.OnExceptionThrown += new _dispDebuggerEvents_OnExceptionThrownEventHandler(this.OnExceptionThrown); _commandEvents.AfterExecute += new _dispCommandEvents_AfterExecuteEventHandler(this.AfterExecute); _commandEvents.BeforeExecute += new _dispCommandEvents_BeforeExecuteEventHandler(this.BeforeExecute); _buildEvents.OnBuildBegin += new _dispBuildEvents_OnBuildBeginEventHandler(this.OnBuildBegin); _buildEvents.OnBuildDone += new _dispBuildEvents_OnBuildDoneEventHandler(this.OnBuildDone); _buildEvents.OnBuildProjConfigBegin += new _dispBuildEvents_OnBuildProjConfigBeginEventHandler(this.OnBuildProjConfigBegin); _buildEvents.OnBuildProjConfigDone += new _dispBuildEvents_OnBuildProjConfigDoneEventHandler(this.OnBuildProjConfigDone); _miscFilesEvents.ItemAdded += new _dispProjectItemsEvents_ItemAddedEventHandler(this.MiscFilesEvents_ItemAdded); _miscFilesEvents.ItemRemoved += new _dispProjectItemsEvents_ItemRemovedEventHandler(this.MiscFilesEvents_ItemRemoved); _miscFilesEvents.ItemRenamed += new _dispProjectItemsEvents_ItemRenamedEventHandler(this.MiscFilesEvents_ItemRenamed); _solutionItemsEvents.ItemAdded += new _dispProjectItemsEvents_ItemAddedEventHandler(this.SolutionItemsEvents_ItemAdded); _solutionItemsEvents.ItemRemoved += new _dispProjectItemsEvents_ItemRemovedEventHandler(this.SolutionItemsEvents_ItemRemoved); _solutionItemsEvents.ItemRenamed += new _dispProjectItemsEvents_ItemRenamedEventHandler(this.SolutionItemsEvents_ItemRenamed); _globalProjectItemsEvents.ItemAdded += new _dispProjectItemsEvents_ItemAddedEventHandler(GlobalProjectItemsEvents_ItemAdded); _globalProjectItemsEvents.ItemRemoved += new _dispProjectItemsEvents_ItemRemovedEventHandler(GlobalProjectItemsEvents_ItemRemoved); _globalProjectItemsEvents.ItemRenamed += new _dispProjectItemsEvents_ItemRenamedEventHandler(GlobalProjectItemsEvents_ItemRenamed); _globalProjectsEvents.ItemAdded += new _dispProjectsEvents_ItemAddedEventHandler(GlobalProjectsEvents_ItemAdded); _globalProjectsEvents.ItemRemoved += new _dispProjectsEvents_ItemRemovedEventHandler(GlobalProjectsEvents_ItemRemoved); _globalProjectsEvents.ItemRenamed += new _dispProjectsEvents_ItemRenamedEventHandler(GlobalProjectsEvents_ItemRenamed); _textDocumentKeyPressEvents.AfterKeyPress += new _dispTextDocumentKeyPressEvents_AfterKeyPressEventHandler(AfterKeyPress); _textDocumentKeyPressEvents.BeforeKeyPress += new _dispTextDocumentKeyPressEvents_BeforeKeyPressEventHandler(BeforeKeyPress); _codeModelEvents.ElementAdded += new _dispCodeModelEvents_ElementAddedEventHandler(ElementAdded); _codeModelEvents.ElementChanged += new _dispCodeModelEvents_ElementChangedEventHandler(ElementChanged); _codeModelEvents.ElementDeleted += new _dispCodeModelEvents_ElementDeletedEventHandler(ElementDeleted); _windowVisibilityEvents.WindowHiding += new _dispWindowVisibilityEvents_WindowHidingEventHandler(WindowHiding); _windowVisibilityEvents.WindowShowing += new _dispWindowVisibilityEvents_WindowShowingEventHandler(WindowShowing); _debuggerExpressionEvaluationEvents.OnExpressionEvaluation += new _dispDebuggerExpressionEvaluationEvents_OnExpressionEvaluationEventHandler(OnExpressionEvaluation); _debuggerProcessEvents.OnProcessStateChanged += new _dispDebuggerProcessEvents_OnProcessStateChangedEventHandler(OnProcessStateChanged); _publishEvents.OnPublishBegin += new _dispPublishEvents_OnPublishBeginEventHandler(OnPublishBegin); _publishEvents.OnPublishDone += new _dispPublishEvents_OnPublishDoneEventHandler(OnPublishDone); }
/// <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; } }