⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commandtools.cs

📁 代码模版 codtemplate
💻 CS
字号:
using System;
using Microsoft.Office.Core;
using Extensibility;
using EnvDTE;
using System.Windows.Forms;
using System.IO;

namespace CodeTemplate
{
	/// <summary>
	/// Summary description for CommandTools.
	/// </summary>
	internal class CommandTools
	{
		/// <summary>
		/// 
		/// </summary>
		/// <param name="res"></param>
		/// <param name="parent"></param>
		/// <param name="pos"></param>
		/// <returns></returns>
		static public CommandBar AddCommandBar(CommandResource res, CommandBar parent, Int32 pos)
        {
			CommandBar bar = FindCommandBar(res.ProgID);
			if(bar != null)
				Connect.Application.Commands.RemoveCommandBar(bar);

			return (CommandBar)Connect.Application.Commands.AddCommandBar(res.ProgID, vsCommandBarType.vsCommandBarTypeToolbar, parent, pos);
        }

		/// <summary>
		/// 
		/// </summary>
		/// <param name="cmd"></param>
		/// <param name="bar"></param>
		/// <param name="menuPos"></param>
		/// <returns></returns>
		static public CommandBarButton AddCommandBarButton(CommandResource res, Command cmd, CommandBar bar, Int32 menuPos) 
		{
			CommandBarButton btn = (CommandBarButton) cmd.AddControl(bar, menuPos);
			btn.Style = res.BitmapID != 0 ? MsoButtonStyle.msoButtonIcon : MsoButtonStyle.msoButtonCaption;

			return btn;
		}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="menuPos"></param>
        /// <returns></returns>
        static public Command AddCommand(CommandResource res, Boolean enabled)
        {
			Command cmd = FindCommand(res.FullCommand);
			if(cmd != null)
				cmd.Delete();

            Int32 flags = enabled ? (Int32) vsCommandStatus.vsCommandStatusSupported | (Int32) vsCommandStatus.vsCommandStatusEnabled : (Int32) vsCommandStatus.vsCommandStatusSupported;

            object[] contextGUIDS = new object[]{};
            cmd = Connect.Application.Commands.AddNamedCommand(Connect.AddInInstance, res.CommandName, res.ButtonText, res.ToolTip, res.IsMSOButton, res.BitmapID, ref contextGUIDS, flags);

			RebindKeyboard(res, cmd);
            return cmd;
        }

		/// <summary>
		/// 
		/// </summary>
		/// <param name="res"></param>
		static public void RebindKeyboard(CommandResource res)
		{
			try
			{
				Command command = Connect.Application.Commands.Item(res.FullCommand, 0);
				RebindKeyboard(res, command);
			}
			catch(Exception)
			{
			}
		}

		static private EnvDTE.Property GetKeyboardScheme()
		{
			EnvDTE.Property prop = Connect.Application.get_Properties("Environment", "Keyboard").Item("Scheme");
			return prop;
		}

		static private Boolean IsDefaultKeyboardScheme()
		{
			return String.Compare((String)GetKeyboardScheme().Value, "[Default Settings]", true) == 0;
		}

		static private String GetVSDirForUser()
		{
			// HACK: Use the version number from EnvDTE.RegistryRoot
			String version = "7.1"; // Assume Visual Studio 2003 in case of failure
			String regRoot = Connect.Application.RegistryRoot;

			Int32 verPos = regRoot.LastIndexOf('\\');
			if(verPos != -1)
				version = regRoot.Substring(verPos + 1);

			String path = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Microsoft\VisualStudio");
			path = Path.Combine(path, version);
			return path;
		}

		static private void CreateKeyboardSchemaCopy()
		{
			EnvDTE.Property prop = GetKeyboardScheme();
			prop.Value = Path.Combine(GetVSDirForUser(), "Copy of Default Settings.vsk");
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="res"></param>
		/// <param name="command"></param>
		static public void RebindKeyboard(CommandResource res, Command command)
		{
			if(res.KeyBind == null)
				return;

			try
			{
				if(IsDefaultKeyboardScheme())
					CreateKeyboardSchemaCopy();
			}
			catch(Exception)
			{
				MessageBox.Show(
					"Could not create a copy of the default keyboard schema.\n" + 
					"Please create a copy by clicking \"Tools\\Customize\\Keyboard\\Save As\"\n" + 
					"and then clicking \"Rebind Keyboard\" in the Code<Template> toolbar button.", 
					"Code<Template> for Visual Studio.NET", 
					MessageBoxButtons.OK, 
					MessageBoxIcon.Exclamation);

				return;
			}

			try 
			{ 
				command.Bindings = res.KeyBind; 
			}
			catch(Exception) 
			{ 
				MessageBox.Show(
					"The default keyboard scheme cannot be changed. Please create a copy\n" + 
					"by clicking \"Tools\\Customize\\Keyboard\\Save As\" and then clicking\n" +
					"\"Rebind Keyboard\" in the Code<Template> toolbar button.", 
					"Code<Template> for Visual Studio.NET", 
					MessageBoxButtons.OK, 
					MessageBoxIcon.Exclamation
				);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="res"></param>
		/// <returns></returns>
		static public Command FindCommand(String name)
		{
			Command cmd = null;

			try
			{
				cmd = Connect.Application.Commands.Item(name, 0);
			}
			catch(Exception)
			{
			}

			return cmd;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
		static public CommandBar FindCommandBar(String name)
		{
			CommandBar bar = null;

			try
			{
				bar = (CommandBar) Connect.Application.CommandBars[name];
			}
			catch(Exception)
			{
			}

			return bar;
		}
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -