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

📄 connect.cs

📁 代码模版 codtemplate
💻 CS
📖 第 1 页 / 共 2 页
字号:
namespace CodeTemplate
{
	using System;
	using System.IO;
	using Microsoft.Office.Core;
	using Extensibility;
	using System.Runtime.InteropServices;
	using EnvDTE;
    using System.Drawing;
    using System.Windows.Forms;
	using System.Collections;
	using System.Text;

	/// <summary>
	///   The Object for implementing an Add-in.
	/// </summary>
	/// <seealso class='IDTExtensibility2' />
	[Guid("B487AFD9-EFB2-41C7-A772-B1F968AF8EB3"), ProgId("CodeTemplate")]
	public class Connect: Extensibility.IDTExtensibility2, IDTCommandTarget, IWin32Window
	{
		private static CommandResource s_crShowMenu = new CommandResource(
			"CodeTemplate",					// ProgID
			"CTShowMenu",					// Command name
			"CTShowMenu",					// Button text
			"Display template menu",		// Tooltip
			102,							// Bitmap ID
			false,							// Is Office bitmap
			null							// Key binding
		);

		private static CommandResource s_crInsertTemplate = new CommandResource(
			"CodeTemplate",					// ProgID
			"CTInsertTemplate",				// Command name
			"CTInsertTemplate",				// Button text
			"Insert Template",				// Tooltip
			0,								// Bitmap ID
			false,							// Is Office bitmap
			Configuration.KeyBinding		// Key binding
		);

		private static _DTE s_app;
        private static AddIn s_addInInstance;
		private TemplateFileManager m_tplMgr;

        internal static _DTE Application
        {
            get { return s_app; }
        }

        internal static AddIn AddInInstance
        {
            get { return s_addInInstance; }
        }

		internal static CommandResource ShowMenuCommand
		{
			get { return s_crShowMenu; }
		}

		internal static CommandResource InsertTemplateCommand
		{
			get { return s_crInsertTemplate; }
		}

        /// <summary>
		///		Implements the constructor for the Add-in Object.
		///		Place your initialization code within this method.
		/// </summary>
		public Connect()
		{
			m_tplMgr = new TemplateFileManager(this);
		}

		/// <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, Extensibility.ext_ConnectMode connectMode, Object addInInst, ref System.Array custom)
		{
			s_app = (_DTE)application;
			s_addInInstance = (AddIn)addInInst;

            if(connectMode == Extensibility.ext_ConnectMode.ext_cm_UISetup)
            {
                try
                {
					Command cmd = CommandTools.AddCommand(s_crShowMenu, true);
					CommandBar bar = CommandTools.AddCommandBar(s_crShowMenu, null, 0);
					CommandBarButton btn = CommandTools.AddCommandBarButton(s_crShowMenu, cmd, bar, 1);

					cmd = CommandTools.AddCommand(s_crInsertTemplate, true);

					bar.Enabled = true;
					bar.Visible = true;
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message, ex.Source);
                }

				return;
            }

			// Ignore any errors creating the sample templates
			try
			{
				TemplateFile.CreateTemplateFromSampleFile();
			}
			catch
			{
			}
		}

		/// <summary>
		///     Implements the OnDisconnection method of the IDTExtensibility2 interface.
		///     Receives notification that the Add-in is being unloaded.
		/// </summary>
		/// <param term='disconnectMode'>
		///      Describes how the Add-in is being unloaded.
		/// </param>
		/// <param term='custom'>
		///      Array of parameters that are host application specific.
		/// </param>
		/// <seealso class='IDTExtensibility2' />
		public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom)
		{
		}

		/// <summary>
		///      Implements the OnAddInsUpdate method of the IDTExtensibility2 interface.
		///      Receives notification that the collection of Add-ins has changed.
		/// </summary>
		/// <param term='custom'>
		///      Array of parameters that are host application specific.
		/// </param>
		/// <seealso class='IDTExtensibility2' />
		public void OnAddInsUpdate(ref System.Array custom)
		{
		}

		/// <summary>
		///      Implements the OnStartupComplete method of the IDTExtensibility2 interface.
		///      Receives notification that the host application has completed loading.
		/// </summary>
		/// <param term='custom'>
		///      Array of parameters that are host application specific.
		/// </param>
		/// <seealso class='IDTExtensibility2' />
		public void OnStartupComplete(ref System.Array custom)
		{
		}

		/// <summary>
		///      Implements the OnBeginShutdown method of the IDTExtensibility2 interface.
		///      Receives notification that the host application is being unloaded.
		/// </summary>
		/// <param term='custom'>
		///      Array of parameters that are host application specific.
		/// </param>
		/// <seealso class='IDTExtensibility2' />
		public void OnBeginShutdown(ref System.Array custom)
		{
		}

		/// <summary>
		///      Implements the QueryStatus method of the IDTCommandTarget interface.
		///      This is called when the command's availability is updated
		/// </summary>
		/// <param term='commandName'>
		///		The name of the command to determine state for.
		/// </param>
		/// <param term='neededText'>
		///		Text that is needed for the command.
		/// </param>
		/// <param term='status'>
		///		The state of the command in the user interface.
		/// </param>
		/// <param term='commandText'>
		///		Text requested by the neededText parameter.
		/// </param>
		/// <seealso class='Exec' />
		public void QueryStatus(String commandName, EnvDTE.vsCommandStatusTextWanted neededText, ref EnvDTE.vsCommandStatus status, ref Object commandText)
		{
            status = vsCommandStatus.vsCommandStatusUnsupported;

            if(neededText == EnvDTE.vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
            {
                if(commandName == s_crShowMenu.FullCommand || commandName == s_crInsertTemplate.FullCommand)
                    status = vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
            }
		}

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private Boolean TemplateItemsEnabled()
        {
			Window wnd = Application.ActiveWindow;
			if(wnd == null)
				return false;

            Document doc = wnd.Document;
			if(doc == null || doc.Type != "Text" || doc.ReadOnly)
				return false;

			TemplateFile tplFile = m_tplMgr.GetTemplateFileForActiveDocument();
			if(tplFile.IsALoadedTemplateFile(doc.FullName) || !tplFile.IsOK)
				return false;

			return true;
        }

		/// <summary>
		///      Implements the Exec method of the IDTCommandTarget interface.
		///      This is called when the command is invoked.
		/// </summary>
		/// <param term='commandName'>
		///		The name of the command to execute.
		/// </param>
		/// <param term='executeOption'>
		///		Describes how the command should be run.
		/// </param>
		/// <param term='varIn'>
		///		Parameters passed from the caller to the command handler.
		/// </param>
		/// <param term='varOut'>
		///		Parameters passed from the command handler to the caller.
		/// </param>
		/// <param term='handled'>
		///		Informs the caller if the command was handled or not.
		/// </param>
		/// <seealso class='Exec' />
		public void Exec(String commandName, EnvDTE.vsCommandExecOption executeOption, ref Object varIn, ref Object varOut, ref Boolean handled)
		{
			handled = false;

			if(executeOption == EnvDTE.vsCommandExecOption.vsCommandExecOptionDoDefault)
			{
				try
				{
					if(commandName == s_crShowMenu.FullCommand)
					{
						DoShowMenuCommand();
						handled = true;
					}

					else if(commandName == s_crInsertTemplate.FullCommand)
					{
						DoInsertTemplateCommand();
						handled = true;
					}
				}
				catch(Exception ex)
				{
					MessageBox.Show(ex.Message, ex.Source);
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		private void DoShowMenuCommand()
		{
			Point pt;

			try
			{
				CommandBar bar = Application.CommandBars[s_crShowMenu.ProgID];
				CommandBarControl control = bar.Controls[1];
				pt = new Point(control.Left, control.Top);

⌨️ 快捷键说明

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