Visual Studio插件

下载地址:https://github.com/leation/Visual-Studio-Addin 
    基于Microsoft Visual Studio 2008 环境(具体项目类型为“其他项目类型”→“扩展性”→“Visual Studio 外接程序”),用C#语言开发实现的Visual Studio扩展 插件,对.NET项目开发很有用,可以批量执行任务,包括折叠和展开所有项目,批量修改项目的目标平台、输出路径、生成事件和.NET版本,批量创建和加 载项目,快速智能修改项目的dll引用、添加dll引用、拷贝项目依赖项、查看项目dll引用、提交dll到Lib库、检查Lib库是否有重复dll、生 成SQL语句和生成GUID等,在搭建项目和发布系统时很有用,有了它可以避免很多不必要的问题,同时也可以减少很多重复的工作,尤其是在发布系统时可以 保证程序的正确性。在给用户编译发布程序的时候很多人都没有意识到目标平台(x86、x64和Any CPU)和.net版本(2.0或3.5等)的选择 对用户可能带来的不良用户体验(通常程序报错,运行不了,或者某个模块运行不了),即便意识到了这一点也很难保证在不同解决方案配置下(debug和 release等)程序的目标平台、.Net版本和dll版本的正确性,通常debug没有问题,但是一旦发布release版本就会出现很多问题,同时 如果要手工修改相关配置工作量也很大。该插件绝对是世界上独一无二的,当然如果需要更多批处理功能,聪明的你也可以继续发挥哦……
    插件基 于Microsoft Visual Studio 2008环境进行开发,所以插件可以在Microsoft Visual Studio 2008以 上版本的Microsoft Visual Studio 环境中使用,目前测试过Microsoft Visual Studio 2008、 Microsoft Visual Studio 2010、Microsoft Visual Studio 2012和 Microsoft Visual Studio 2013。

using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;
using System.Resources;
using System.Reflection;
using System.Globalization;
using System.Windows.Forms;

namespace Leation.VSAddin
{
	/// 用于实现外接程序的对象。
	/// 
	public class Connect : IDTExtensibility2, IDTCommandTarget
	{
        private DTE2 _app;
        private AddIn _addIn;

        /// 实现外接程序对象的构造函数。请将您的初始化代码置于此方法内。
		public Connect()
		{
		}

		/// 实现 IDTExtensibility2 接口的 OnConnection 方法。接收正在加载外接程序的通知。
		/// 宿主应用程序的根对象。
		/// 描述外接程序的加载方式。
		/// 表示此外接程序的对象。
		/// 
		public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
		{            
            _app = (DTE2)application;
			_addIn = (AddIn)addInInst;

            if (connectMode == ext_ConnectMode.ext_cm_UISetup||connectMode== ext_ConnectMode.ext_cm_AfterStartup||connectMode== ext_ConnectMode.ext_cm_Startup)
            {
                object[] contextGUIDS = new object[] { };
                Commands2 commands = (Commands2)_app.Commands;

                Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_app.CommandBars)["MenuBar"];

                CommandBarControl cmdCtr = this.GetExistCommandBarControl(menuBarCommandBar, "李仙伟");
                if (cmdCtr != null)
                {
                    cmdCtr.Visible = true;
                    return;
                }
                CommandBarControl myToolsControl = menuBarCommandBar.Controls.Add(MsoControlType.msoControlPopup, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                myToolsControl.Caption = "李仙伟";

                CommandBarPopup toolsPopup = (CommandBarPopup)myToolsControl;

                //如果希望添加多个由您的外接程序处理的命令,可以重复此 try/catch 块,
                //  只需确保更新 QueryStatus/Exec 方法,使其包含新的命令名。
                try
                {
                    //将一个命令添加到 Commands 集合:
                    Command command = this.GetExistCommand(commands, "PlatformSetting");
                    if (command == null)
                    {
                        command = commands.AddNamedCommand2(_addIn, "PlatformSetting", "目标平台设置(&P)", "批量修改目标平台", false, 1, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
                    }
                    //将对应于该命令的控件添加到“李仙伟”菜单:
                    if ((command != null) && (toolsPopup != null))
                    {
                        command.AddControl(toolsPopup.CommandBar, 1);
                    }
                }
                catch (System.ArgumentException)
                {
                    //如果出现此异常,原因很可能是由于具有该名称的命令
                    //  已存在。如果确实如此,则无需重新创建此命令,并且
                    //  可以放心忽略此异常。
                }
                try
                {
                    Command command = this.GetExistCommand(commands, "OutPathSetting");
                    if (command == null)
                    {
                        command = commands.AddNamedCommand2(_addIn, "OutPathSetting", "输出路径设置(&O)", "批量修改输出路径设置", false, 1, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
                    }
                    if ((command != null) && (toolsPopup != null))
                    {
                        command.AddControl(toolsPopup.CommandBar, 2);
                    }
                }
                catch (System.ArgumentException)
                {
                }
                try
                {
                    Command command = this.GetExistCommand(commands, "BuildEventSetting");
                    if (command == null)
                    {
                        command = commands.AddNamedCommand2(_addIn, "BuildEventSetting", "生成事件设置(&E)", "批量修改生成事件", false, 1, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
                    }
                    if ((command != null) && (toolsPopup != null))
                    {
                        command.AddControl(toolsPopup.CommandBar, 3);
                    }
                }
                catch (System.ArgumentException)
                {
                }
                try
                {
                    Command command = this.GetExistCommand(commands, "NetFrameweorkSetting");
                    if (command == null)
                    {
                        command = commands.AddNamedCommand2(_addIn, "NetFrameweorkSetting", ".NET版本设置(&N)", "批量修改.NET版本", false, 1, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
                    }
                    if ((command != null) && (toolsPopup != null))
                    {
                        command.AddControl(toolsPopup.CommandBar, 4);
                    }
                }
                catch (System.ArgumentException)
                {
                }
                try
                {
                    Command command = this.GetExistCommand(commands, "CollapseAll");
                    if (command == null)
                    {
                        command = commands.AddNamedCommand2(_addIn, "CollapseAll", "折叠所有项目(&C)", "折叠所有项目", false, 1, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
                    }
                    if ((command != null) && (toolsPopup != null))
                    {
                        command.AddControl(toolsPopup.CommandBar, 5);
                    }
                }
                catch (System.ArgumentException)
                {
                }
                try
                {
                    Command command = this.GetExistCommand(commands, "ExpandAll");
                    if (command == null)
                    {
                        command = commands.AddNamedCommand2(_addIn, "ExpandAll", "展开所有项目(&X)", "展开所有项目", false, 1, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
                    }
                    if ((command != null) && (toolsPopup != null))
                    {
                        command.AddControl(toolsPopup.CommandBar, 6);
                    }
                }
                catch (System.ArgumentException)
                {
                }
                try
                {
                    Command command = this.GetExistCommand(commands, "MultiLoadProjects");
                    if (command == null)
                    {
                        command = commands.AddNamedCommand2(_addIn, "MultiLoadProjects", "批量加载项目", "批量加载项目", false, 1, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
                    }
                    if ((command != null) && (toolsPopup != null))
                    {
                        command.AddControl(toolsPopup.CommandBar, 7);
                    }
                }
                catch (System.ArgumentException)
                {
                }
                try
                {
                    Command command = this.GetExistCommand(commands, "MultiCreateProjects");
                    if (command == null)
                    {
                        command = commands.AddNamedCommand2(_addIn, "MultiCreateProjects", "批量创建项目", "批量创建项目", false, 1, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
                    }
                    if ((command != null) && (toolsPopup != null))
                    {
                        command.AddControl(toolsPopup.CommandBar, 8);
                    }
                }
                catch (System.ArgumentException)
                {
                }
                try
                {
                    Command command = this.GetExistCommand(commands, "DllRefConfig");
                    if (command == null)
                    {
                        command = commands.AddNamedCommand2(_addIn, "DllRefConfig", "修改项目dll引用", "修改项目dll引用", false, 1, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
                    }
                    if ((command != null) && (toolsPopup != null))
                    {
                        command.AddControl(toolsPopup.CommandBar, 9);
                    }
                }
                catch (System.ArgumentException)
                {
                }
                try
                {
                    Command command = this.GetExistCommand(commands, "DllRefFileCopy");
                    if (command == null)
                    {
                        command = commands.AddNamedCommand2(_addIn, "DllRefFileCopy", "拷贝dll的引用(依赖项)", "拷贝dll的引用(依赖项)", false, 1, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
                    }
                    if ((command != null) && (toolsPopup != null))
                    {
                        command.AddControl(toolsPopup.CommandBar, 10);
                    }
                }
                catch (System.ArgumentException)
                {
                }
                try
                {
                    Command command = this.GetExistCommand(commands, "Dll2Lib");
                    if (command == null)
                    {
                        command = commands.AddNamedCommand2(_addIn, "Dll2Lib", "更新dll到Lib", "更新dll到Lib", false, 1, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
                    }
                    if ((command != null) && (toolsPopup != null))
                    {
                        command.AddControl(toolsPopup.CommandBar, 11);
                    }
                }
                catch (System.ArgumentException)
                {
                }
                try
                {
                    Command command = this.GetExistCommand(commands, "SQLCreator");
                    if (command == null)
                    {
                        command = commands.AddNamedCommand2(_addIn, "SQLCreator", "SQL语句生成器", "SQL语句生成器", false, 1, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
                    }
                    if ((command != null) && (toolsPopup != null))
                    {
                        command.AddControl(toolsPopup.CommandBar, 12);
                    }
                }
                catch (System.ArgumentException)
                {
                }
                try
                {
                    Command command = this.GetExistCommand(commands, "GuidCreator");
                    if (command == null)
                    {
                        command = commands.AddNamedCommand2(_addIn, "GuidCreator", "Guid生成器", "Guid生成器", false, 1, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
                    }
                    if ((command != null) && (toolsPopup != null))
                    {
                        command.AddControl(toolsPopup.CommandBar, 13);
                    }
                }
                catch (System.ArgumentException)
                {
                }
                try
                {
                    Command command = this.GetExistCommand(commands, "CheckRepeatDll");
                    if (command == null)
                    {
                        command = commands.AddNamedCommand2(_addIn, "CheckRepeatDll", "检查Lib库重复的dll", "检查Lib库重复的dll", false, 1, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
                    }
                    if ((command != null) && (toolsPopup != null))
                    {
                        command.AddControl(toolsPopup.CommandBar, 14);
                    }
                }
                catch (System.ArgumentException)
                {
                }
                try
                {
                    Command command = this.GetExistCommand(commands, "Selection2Upper");
                    if (command == null)
                    {
                        command = commands.AddNamedCommand2(_addIn, "Selection2Upper", "Selection2Upper", "Selection2Upper", false, 1, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
                    }
                    if ((command != null) && (toolsPopup != null))
                    {
                        command.AddControl(toolsPopup.CommandBar, 15);
                    }
                }
                catch (System.ArgumentException)
                {
                }
                try
                {
                    Command command = this.GetExistCommand(commands, "AboutMe");
                    if (command == null)
                    {
                        command = commands.AddNamedCommand2(_addIn, "AboutMe", "关于(&A)", "关于", false, 1, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
                    }
                    if ((command != null) && (toolsPopup != null))
                    {
                        command.AddControl(toolsPopup.CommandBar, 16);
                    }
                }
                catch (System.ArgumentException)
                {
                }
            }

            //清理临时文件
            DllRefReflectUtility.ClearTempFiles();
		}

		/// 实现 IDTExtensibility2 接口的 OnDisconnection 方法。接收正在卸载外接程序的通知。
		/// 描述外接程序的卸载方式。
		/// 特定于宿主应用程序的参数数组。
		/// 
		public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
		{
            if (_app == null)
            {
                return;
            }
            CommandBar menuBarCommandBar = ((CommandBars)_app.CommandBars)["MenuBar"];
            CommandBarControl cmdCtr = this.GetExistCommandBarControl(menuBarCommandBar, "李仙伟");
            if (cmdCtr != null)
            {
                cmdCtr.Visible = false;
            }          
		}

		/// 实现 IDTExtensibility2 接口的 OnAddInsUpdate 方法。当外接程序集合已发生更改时接收通知。
		/// 特定于宿主应用程序的参数数组。
		/// 		
		public void OnAddInsUpdate(ref Array custom)
        {           

		}

		/// 实现 IDTExtensibility2 接口的 OnStartupComplete 方法。接收宿主应用程序已完成加载的通知。
		/// 特定于宿主应用程序的参数数组。
		/// 
		public void OnStartupComplete(ref Array custom)
        {          

		}

		/// 实现 IDTExtensibility2 接口的 OnBeginShutdown 方法。接收正在卸载宿主应用程序的通知。
		/// 特定于宿主应用程序的参数数组。
		/// 
		public void OnBeginShutdown(ref Array custom)
		{
		}

        /// 实现 IDTCommandTarget 接口的 QueryStatus 方法。此方法在更新该命令的可用性时调用
        /// 要确定其状态的命令的名称。
        /// 该命令所需的文本。
        /// 该命令在用户界面中的状态。
        /// neededText 参数所要求的文本。
        /// 
        public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus status, ref object commandText)
        {
            if (neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
            {
                if (commandName == "Leation.VSAddin.Connect.PlatformSetting")
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.OutPathSetting")
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.BuildEventSetting")
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.NetFrameweorkSetting")
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.CollapseAll")
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.ExpandAll")
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.MultiLoadProjects")
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.MultiCreateProjects")
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.DllRefConfig")
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.DllRefFileCopy")
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.Dll2Lib")
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.SQLCreator")
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.GuidCreator")
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.CheckRepeatDll")
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.Selection2Upper")
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.AboutMe")
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
                    return;
                }
            }
        }

        /// 实现 IDTCommandTarget 接口的 Exec 方法。此方法在调用该命令时调用。
        /// 要执行的命令的名称。
        /// 描述该命令应如何运行。
        /// 从调用方传递到命令处理程序的参数。
        /// 从命令处理程序传递到调用方的参数。
        /// 通知调用方此命令是否已被处理。
        /// 
        public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
        {
            handled = false;
            if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
            {
                if (commandName == "Leation.VSAddin.Connect.PlatformSetting")
                {
                    frmPlatformSetting frm = new frmPlatformSetting(_app);
                    frm.ShowDialog();

                    handled = true;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.OutPathSetting")
                {
                    frmOutPathSetting frm = new frmOutPathSetting(_app);
                    frm.ShowDialog();

                    handled = true;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.BuildEventSetting")
                {
                    frmBuildEventSetting frm = new frmBuildEventSetting(_app);
                    frm.ShowDialog();

                    handled = true;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.NetFrameweorkSetting")
                {
                    frmNetFrameworkSetting frm = new frmNetFrameworkSetting(_app);
                    frm.ShowDialog();

                    handled = true;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.CollapseAll")
                {
                    SolutionExployerUtility utility = new SolutionExployerUtility(_app);
                    utility.CollapseAll();

                    handled = true;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.ExpandAll")
                {
                    SolutionExployerUtility utility = new SolutionExployerUtility(_app);
                    utility.ExpandAll();

                    handled = true;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.MultiLoadProjects")
                {
                    frmMultiLoadProjects frm = new frmMultiLoadProjects(_app);
                    frm.ShowDialog();

                    handled = true;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.MultiCreateProjects")
                {
                    frmMultiCreateProjects frm = new frmMultiCreateProjects(_app);
                    frm.ShowDialog();

                    handled = true;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.DllRefConfig")
                {
                    frmDllRefSetting frm = new  frmDllRefSetting(_app);
                    frm.ShowDialog();

                    handled = true;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.DllRefFileCopy")
                {
                    frmDllRefFileCopy frm = new frmDllRefFileCopy(_app);
                    frm.ShowDialog();

                    handled = true;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.Dll2Lib")
                {
                    frmDll2Lib frm = new frmDll2Lib(_app);
                    frm.ShowDialog();

                    handled = true;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.SQLCreator")
                {
                    Form frm = new Form();
                    frm.FormBorderStyle = FormBorderStyle.Sizable;
                    frm.ShowIcon = false;
                    frm.Width = 800;
                    frm.Height = 520;
                    frm.StartPosition = FormStartPosition.CenterScreen;
                    frm.Text = "SQL语句生成器";
                    SQLCreatorUI ctr = new SQLCreatorUI();
                    ctr.Dock = DockStyle.Fill;
                    frm.Controls.Add(ctr);
                    frm.ShowDialog();

                    handled = true;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.GuidCreator")
                {
                    Form frm = new Form();
                    frm.FormBorderStyle = FormBorderStyle.Sizable;
                    frm.ShowIcon = false;
                    frm.Width = 800;
                    frm.Height = 520;
                    frm.StartPosition = FormStartPosition.CenterScreen;
                    frm.Text = "Guid生成器";
                    GuidCreatorUI ctr = new GuidCreatorUI();
                    ctr.Dock = DockStyle.Fill;
                    frm.Controls.Add(ctr);
                    frm.ShowDialog();

                    handled = true;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.CheckRepeatDll")
                {
                    frmCheckRepeatDll frm = new frmCheckRepeatDll(_app);
                    frm.WindowState = FormWindowState.Maximized;
                    frm.ShowDialog();

                    handled = true;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.Selection2Upper")
                {
                    frmSelection2Upper frm = new frmSelection2Upper();

                    frm.BtnSelection2Upper.Click += delegate(object sender, EventArgs e)
                    {
                        try
                        {
                            TextSelection txtSelection = _app.ActiveDocument.Selection as TextSelection;
                            if (txtSelection != null)
                            {
                                txtSelection.Text = txtSelection.Text.ToUpper();
                            }
                        }
                        catch (Exception ex)
                        {
                            MsgBox.ShowTip(ex.Message);
                        }
                    };
                    frm.BtnSelection2Lower.Click += delegate(object sender, EventArgs e)
                    {
                        try
                        {
                            TextSelection txtSelection = _app.ActiveDocument.Selection as TextSelection;
                            if (txtSelection != null)
                            {
                                txtSelection.Text = txtSelection.Text.ToLower();
                            }
                        }
                        catch (Exception ex)
                        {
                            MsgBox.ShowTip(ex.Message);
                        }
                    };
                    frm.Show();

                    handled = true;
                    return;
                }
                if (commandName == "Leation.VSAddin.Connect.AboutMe")
                {
                    MsgBox.ShowTip("李仙伟rnQQ:744757242rnCopyright(c)2013 Leation");
                    
                    handled = true;
                    return;
                }
            }
        }

        /// 
        /// 查找已经存在的命令
        /// 
        /// Commands2对象
        /// 命令的Name
        /// 
        private Command GetExistCommand(Commands2 commands,string name)
        {
            foreach (Command cmd in commands)
            {
                if (cmd.Name == "Leation.VSAddin.Connect." + name)
                {
                    return cmd;
                }
            }
            return null;
        }

        /// 
        /// 查找已经存在的菜单项
        /// 
        /// 菜单项CommandBar对象
        /// 菜单项的标题
        /// 
        private CommandBarControl GetExistCommandBarControl(CommandBar cmdBar, string caption)
        {
            string toolsMenuName;

            try
            {
                //若要将此命令移动到另一个菜单,则将“工具”一词更改为此菜单的英文版。
                //  此代码将获取区域性,将其追加到菜单名中,然后将此命令添加到该菜单中。
                //  您会在此文件中看到全部顶级菜单的列表
                //  CommandBar.resx.
                string resourceName;
                ResourceManager resourceManager = new ResourceManager("MyAddin2.CommandBar", Assembly.GetExecutingAssembly());
                CultureInfo cultureInfo = new CultureInfo(_app.LocaleID);

                if (cultureInfo.TwoLetterISOLanguageName == "zh")
                {
                    System.Globalization.CultureInfo parentCultureInfo = cultureInfo.Parent;
                    resourceName = String.Concat(parentCultureInfo.Name, caption);
                }
                else
                {
                    resourceName = String.Concat(cultureInfo.TwoLetterISOLanguageName, caption);
                }
                toolsMenuName = resourceManager.GetString(resourceName);
            }
            catch
            {
                //我们试图查找“工具”一词的本地化版本,但未能找到。
                //  默认值为 en-US 单词,该值可能适用于当前区域性。
                toolsMenuName = "李仙伟";
            }
            foreach (CommandBarControl cmdCtr in cmdBar.Controls)
            {
                if (cmdCtr.Caption == caption)
                {
                    return cmdCtr;
                }
            }
            return null;
        }
    }
}

                            

原文地址:https://www.cnblogs.com/xinweichen/p/5864659.html