/// <summary> /// Called when the AddIn is loaded. This method allows each of the commands to /// store member variables with the objects passed in and ensure that the menu /// items and commands have been properly added to the object model. /// </summary> /// <param name="application"> Root object in the application </param> /// <param name="connectMode"> 'Mode' in which the environment is starting up the addin </param> /// <param name="addIn"> Object representing this AddIn in the Object Model</param> public void OnConnection(EnvDTE._DTE application, Extensibility.ext_ConnectMode connectMode, EnvDTE.AddIn addIn) { try { m_applicationObject = (EnvDTE._DTE)application; m_addInInstance = (EnvDTE.AddIn)addIn; m_strCommandName = "AMDeleteCourse"; m_strName = AMResources.GetLocalizedString("AMDeleteCourseName"); m_strItemText = AMResources.GetLocalizedString("AMDeleteCourseItemText"); string description = AMResources.GetLocalizedString("AMDeleteCourseDescription"); EnvDTE.Commands commands = null; EnvDTE.Command command = null; _CommandBars officeCommandBars = null; CommandBar officeCommandBar = null; CommandBarControl officeCommandControl = null; CommandBar officeAcademic = null; object [] contextGuids; contextGuids = new object[] { }; commands = m_applicationObject.Commands; try { command = commands.AddNamedCommand(m_addInInstance, m_strCommandName, m_strName, description, false, 108, ref contextGuids, (int)(EnvDTE.vsCommandStatus.vsCommandStatusEnabled | EnvDTE.vsCommandStatus.vsCommandStatusSupported)); // Add the new command to the tools menu officeCommandBars = m_applicationObject.CommandBars; string amFacultyMenuItem = AMResources.GetLocalizedString("AMFacultyMenuItem"); try { officeAcademic = (CommandBar)officeCommandBars[amFacultyMenuItem]; } catch { } if (officeAcademic == null) { officeCommandBar = (CommandBar)officeCommandBars["Tools"]; officeAcademic = (CommandBar)m_applicationObject.Commands.AddCommandBar(amFacultyMenuItem, EnvDTE.vsCommandBarType.vsCommandBarTypeMenu, officeCommandBar, 1); } officeCommandControl = command.AddControl((object)officeAcademic, 1); officeCommandControl.Caption = m_strItemText; } catch { // Falling into this simply means that the command was already registered. } } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Exception e = " + e.Message); } }
/// <summary> /// Registers a command and places it on the Tools menu. /// </summary> public void OnConnection(EnvDTE._DTE application, Extensibility.ext_ConnectMode connectMode, EnvDTE.AddIn addIn) { m_applicationObject = (EnvDTE._DTE)application; m_addInInstance = (EnvDTE.AddIn)addIn; m_strCommandName = "AMRemoveCourse"; m_strName = AMResources.GetLocalizedString("AMRemoveCourseName"); m_strItemText = AMResources.GetLocalizedString("AMRemoveCourseItemText"); string strDescription = AMResources.GetLocalizedString("AMRemoveCourseDescription"); EnvDTE.Commands commands = null; EnvDTE.Command command = null; Microsoft.Office.Core._CommandBars officeCommandBars = null; Microsoft.Office.Core.CommandBar officeCommandBar = null; Microsoft.Office.Core.CommandBarControl officeCommandControl = null; Microsoft.Office.Core.CommandBar officeAcademic = null; object [] contextGuids; contextGuids = new object[] { }; commands = m_applicationObject.Commands; try { command = commands.AddNamedCommand(m_addInInstance, m_strCommandName, m_strName, strDescription, false, 108, ref contextGuids, (int)(EnvDTE.vsCommandStatus.vsCommandStatusEnabled | EnvDTE.vsCommandStatus.vsCommandStatusSupported)); // Add the new command to the tools menu officeCommandBars = m_applicationObject.CommandBars; string amStudentMenuItem = AMResources.GetLocalizedString("AMStudentMenuItem"); try { officeAcademic = (CommandBar)officeCommandBars[amStudentMenuItem]; } catch { } if (officeAcademic == null) { officeCommandBar = (CommandBar)officeCommandBars["Tools"]; officeAcademic = (CommandBar)m_applicationObject.Commands.AddCommandBar(amStudentMenuItem, EnvDTE.vsCommandBarType.vsCommandBarTypeMenu, officeCommandBar, 1); } officeCommandControl = command.AddControl((object)officeAcademic, 1); officeCommandControl.Caption = m_strItemText; } catch (System.Exception /*e*/) { // Falling into this simply means that the command was already registered. } }
// add a command to the addin. // the added command will be assigned a full name from the progid of the addin. // VisualStudio will call the Exec function of the DLL of the addin when the command // is executed. // InAddinName : local name of the command within the addin. The full name is a // concat of the progid of the addin and this name. // InBitmapIx : index into collection of icon bitmaps of VisualStudio. // some values: public DTE_Command AddCommand( string InName, ButtonText InButtonText, string InToolTipText, int InBitmapIx) { int defaultCommandDisabled = 16; object[] contextUIGuid = new object[] { }; EnvDTE.Commands commands = mMain.dte2.Commands; EnvDTE.Command addedCmd = commands.AddNamedCommand( addin, InName, InButtonText.Text, InToolTipText, true, InBitmapIx, ref contextUIGuid, defaultCommandDisabled); return(new DTE_Command(mMain, addedCmd)); }