www.pudn.com > ArcGISEngine_CSharp.rar > AddDateTool.cs


/* 
 Copyright 1995-2004 ESRI 
 
 All rights reserved under the copyright laws of the United States. 
 
 You may freely redistribute and use this sample code, with or without modification. 
 
 Disclaimer: THE SAMPLE CODE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED  
 WARRANTIES, INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS  
 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ESRI OR  
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,  
 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF  
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS  
 INTERRUPTION) SUSTAINED BY YOU OR A THIRD PARTY, HOWEVER CAUSED AND ON ANY  
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ARISING IN ANY  
 WAY OUT OF THE USE OF THIS SAMPLE CODE, EVEN IF ADVISED OF THE POSSIBILITY OF  
 SUCH DAMAGE. 
 
 For additional information contact: Environmental Systems Research Institute, Inc. 
 
 Attn: Contracts Dept. 
 
 380 New York Street 
 
 Redlands, California, U.S.A. 92373  
 
 Email: contracts@esri.com 
*/ 
using System; 
using ESRI.ArcGIS.Carto; 
using ESRI.ArcGIS.Display; 
using ESRI.ArcGIS.Geometry; 
using ESRI.ArcGIS.SystemUI; 
using ESRI.ArcGIS.esriSystem; 
using ESRI.ArcGIS.ControlCommands; 
using ESRI.ArcGIS.Utility.BaseClasses; 
using System.Runtime.InteropServices; 
 
namespace CSharpDotNETCommands 
{ 
	///  
	/// Summary description for AddDateTool. 
	///  
 
	[ClassInterface(ClassInterfaceType.None)] 
	[Guid("D880184E-AC81-47E5-B363-781F4DC4528F")] 
	public sealed class AddDateTool : BaseTool 
	{ 
		//The HookHelper object that deals with the hook passed to the OnCreate event 
		private IHookHelper m_HookHelper = new HookHelperClass(); 
 
		//Register in the 'ESRI Controls Commands' component category 
		#region Component Category Registration 
		[ComRegisterFunction()] 
		[ComVisible(false)] 
		static void RegisterFunction(String sKey) 
		{ 
			string fullKey = sKey.Remove(0, 18) + @"\Implemented Categories"; 
			Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(fullKey, true); 
			if (regKey != null) 
			{ 
				regKey.CreateSubKey("{B284D891-22EE-4F12-A0A9-B1DDED9197F4}"); 
			} 
		} 
		[ComUnregisterFunction()] 
		[ComVisible(false)] 
		static void UnregisterFunction(String sKey) 
		{ 
			string fullKey = sKey.Remove(0, 18) + @"\Implemented Categories"; 
			Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(fullKey, true); 
			if (regKey != null) 
			{ 
				regKey.DeleteSubKey("{B284D891-22EE-4F12-A0A9-B1DDED9197F4}"); 
			} 
		} 
		#endregion 
 
		public AddDateTool() 
		{ 
			//Get an array resources in the assmebly 
			string[] res = GetType().Assembly.GetManifestResourceNames(); 
			//Set the tool properties 
			base.m_bitmap = new System.Drawing.Bitmap(GetType().Assembly.GetManifestResourceStream(res[0])); 
			base.m_caption = "Add Date"; 
			base.m_category = "CustomCommands"; 
			base.m_message = "Adds a date element to the page layout"; 
			base.m_name = "CustomCommands_Add Date"; 
			base.m_toolTip = "Add date"; 
		} 
	 
		public override void OnCreate(object hook) 
		{ 
				m_HookHelper.Hook = hook; 
		} 
	 
		public override void OnMouseDown(int Button, int Shift, int X, int Y) 
		{ 
			base.OnMouseDown (Button, Shift, X, Y); 
			 
			//Get the active view 
			IActiveView activeView = m_HookHelper.ActiveView; 
 
			//Create a new text element 
			ITextElement textElement = new TextElementClass(); 
			//Create a text symbol 
			ITextSymbol textSymbol = new TextSymbolClass(); 
			textSymbol.Size = 25; 
 
			//Set the text element properties 
			textElement.Symbol = textSymbol; 
			textElement.Text = DateTime.Now.ToShortDateString(); 
 
			//QI for IElement 
			IElement element = (IElement) textElement; 
			//Create a page point 
			IPoint point = new PointClass(); 
			point = activeView.ScreenDisplay.DisplayTransformation.ToMapPoint(X,Y); 
			//Set the elements geometry 
			element.Geometry = point; 
 
			//Add the element to the graphics container 
			activeView.GraphicsContainer.AddElement(element, 0); 
			//Refresh the graphics 
			activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); 
		} 
	 
		public override bool Enabled 
		{ 
			get 
			{ 
				//Set the enabled property 
				if (m_HookHelper.ActiveView != null)  
				{ 
					return true; 
				} 
				else 
				{ 
					return false; 
				} 
			} 
		} 
	} 
}