www.pudn.com > ArcGISEngine_CSharp.rar > Form1.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 System.Windows.Forms; 
using ESRI.ArcGIS.SystemUI; 
using ESRI.ArcGIS.Carto; 
using ESRI.ArcGIS.Display; 
using ESRI.ArcGIS.Geometry; 
using ESRI.ArcGIS.esriSystem; 
using ESRI.ArcGIS.TOCControl; 
using ESRI.ArcGIS.ToolbarControl; 
 
namespace Controls 
{ 
	///  
	/// Summary description for Form1. 
	///  
	public class Form1 : System.Windows.Forms.Form 
	{ 
 
		private ESRI.ArcGIS.PageLayoutControl.AxPageLayoutControl axPageLayoutControl1; 
		private ESRI.ArcGIS.MapControl.AxMapControl axMapControl1; 
		private ESRI.ArcGIS.TOCControl.AxTOCControl axTOCControl1; 
		private ESRI.ArcGIS.ToolbarControl.AxToolbarControl axToolbarControl1; 
		 
		private const int WM_ENTERSIZEMOVE = 0x231; 
		private const int WM_EXITSIZEMOVE = 0x232; 
 
		private IAoInitialize m_AoInitialize = new AoInitializeClass();                 //The initialization object 
		private IToolbarMenu m_ToolbarMenu = new ToolbarMenuClass();					//The popup menu 
		private IEnvelope m_Envelope;													//The envelope drawn on the MapControl 
		private Object m_FillSymbol;													//The symbol used to draw the envelope on the MapControl 
		private ITransformEvents_VisibleBoundsUpdatedEventHandler visBoundsUpdatedE;	//The PageLayoutControl's focus map events	 
		private ICustomizeDialog m_CustomizeDialog = new CustomizeDialogClass();		//The CustomizeDialog used by the ToolbarControl 
		private ICustomizeDialogEvents_OnStartDialogEventHandler startDialogE;			//The CustomizeDialog start event 
		private ICustomizeDialogEvents_OnCloseDialogEventHandler closeDialogE;			//The CustomizeDialog close event  
		private System.Windows.Forms.CheckBox chkCustomize; 
 
		///  
		/// Required designer variable. 
		///  
		private System.ComponentModel.Container components = null; 
 
		public Form1() 
		{ 
			// 
			// Required for Windows Form Designer support 
			// 
			InitializeComponent(); 
 
			// 
			// TODO: Add any constructor code after InitializeComponent call 
			// 
		} 
 
		///  
		/// Clean up any resources being used. 
		///  
		protected override void Dispose( bool disposing ) 
		{ 
			if( disposing ) 
			{ 
				if (components != null)  
				{ 
					components.Dispose(); 
				} 
			} 
			base.Dispose( disposing ); 
		} 
 
		#region Windows Form Designer generated code 
		///  
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor. 
		///  
		private void InitializeComponent() 
		{ 
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1)); 
			this.chkCustomize = new System.Windows.Forms.CheckBox(); 
			this.axPageLayoutControl1 = new ESRI.ArcGIS.PageLayoutControl.AxPageLayoutControl(); 
			this.axMapControl1 = new ESRI.ArcGIS.MapControl.AxMapControl(); 
			this.axTOCControl1 = new ESRI.ArcGIS.TOCControl.AxTOCControl(); 
			this.axToolbarControl1 = new ESRI.ArcGIS.ToolbarControl.AxToolbarControl(); 
			((System.ComponentModel.ISupportInitialize)(this.axPageLayoutControl1)).BeginInit(); 
			((System.ComponentModel.ISupportInitialize)(this.axMapControl1)).BeginInit(); 
			((System.ComponentModel.ISupportInitialize)(this.axTOCControl1)).BeginInit(); 
			((System.ComponentModel.ISupportInitialize)(this.axToolbarControl1)).BeginInit(); 
			this.SuspendLayout(); 
			//  
			// chkCustomize 
			//  
			this.chkCustomize.Location = new System.Drawing.Point(528, 8); 
			this.chkCustomize.Name = "chkCustomize"; 
			this.chkCustomize.Size = new System.Drawing.Size(80, 24); 
			this.chkCustomize.TabIndex = 4; 
			this.chkCustomize.Text = "Customize"; 
			this.chkCustomize.CheckedChanged += new System.EventHandler(this.chkCustomize_CheckedChanged); 
			//  
			// axPageLayoutControl1 
			//  
			this.axPageLayoutControl1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)  
				| System.Windows.Forms.AnchorStyles.Left)  
				| System.Windows.Forms.AnchorStyles.Right))); 
			this.axPageLayoutControl1.Location = new System.Drawing.Point(208, 32); 
			this.axPageLayoutControl1.Name = "axPageLayoutControl1"; 
			this.axPageLayoutControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axPageLayoutControl1.OcxState"))); 
			this.axPageLayoutControl1.Size = new System.Drawing.Size(400, 400); 
			this.axPageLayoutControl1.TabIndex = 5; 
			this.axPageLayoutControl1.OnMouseDown += new ESRI.ArcGIS.PageLayoutControl.IPageLayoutControlEvents_OnMouseDownEventHandler(this.axPageLayoutControl1_OnMouseDown); 
			this.axPageLayoutControl1.OnPageLayoutReplaced += new ESRI.ArcGIS.PageLayoutControl.IPageLayoutControlEvents_OnPageLayoutReplacedEventHandler(this.axPageLayoutControl1_OnPageLayoutReplaced); 
			//  
			// axMapControl1 
			//  
			this.axMapControl1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)  
				| System.Windows.Forms.AnchorStyles.Left))); 
			this.axMapControl1.Location = new System.Drawing.Point(8, 296); 
			this.axMapControl1.Name = "axMapControl1"; 
			this.axMapControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMapControl1.OcxState"))); 
			this.axMapControl1.Size = new System.Drawing.Size(200, 136); 
			this.axMapControl1.TabIndex = 6; 
			this.axMapControl1.OnAfterDraw += new ESRI.ArcGIS.MapControl.IMapControlEvents2_OnAfterDrawEventHandler(this.axMapControl1_OnAfterDraw); 
			//  
			// axTOCControl1 
			//  
			this.axTOCControl1.Location = new System.Drawing.Point(8, 32); 
			this.axTOCControl1.Name = "axTOCControl1"; 
			this.axTOCControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axTOCControl1.OcxState"))); 
			this.axTOCControl1.Size = new System.Drawing.Size(200, 264); 
			this.axTOCControl1.TabIndex = 7; 
			this.axTOCControl1.OnEndLabelEdit += new ESRI.ArcGIS.TOCControl.ITOCControlEvents_OnEndLabelEditEventHandler(this.axTOCControl1_OnEndLabelEdit); 
			//  
			// axToolbarControl1 
			//  
			this.axToolbarControl1.Location = new System.Drawing.Point(8, 8); 
			this.axToolbarControl1.Name = "axToolbarControl1"; 
			this.axToolbarControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axToolbarControl1.OcxState"))); 
			this.axToolbarControl1.Size = new System.Drawing.Size(512, 26); 
			this.axToolbarControl1.TabIndex = 8; 
			//  
			// Form1 
			//  
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 
			this.ClientSize = new System.Drawing.Size(616, 438); 
			this.Controls.Add(this.axTOCControl1); 
			this.Controls.Add(this.axMapControl1); 
			this.Controls.Add(this.axPageLayoutControl1); 
			this.Controls.Add(this.chkCustomize); 
			this.Controls.Add(this.axToolbarControl1); 
			this.Name = "Form1"; 
			this.Text = "Form1"; 
			this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing); 
			this.Load += new System.EventHandler(this.Form1_Load); 
			((System.ComponentModel.ISupportInitialize)(this.axPageLayoutControl1)).EndInit(); 
			((System.ComponentModel.ISupportInitialize)(this.axMapControl1)).EndInit(); 
			((System.ComponentModel.ISupportInitialize)(this.axTOCControl1)).EndInit(); 
			((System.ComponentModel.ISupportInitialize)(this.axToolbarControl1)).EndInit(); 
			this.ResumeLayout(false); 
 
		} 
		#endregion 
 
		///  
		/// The main entry point for the application. 
		///  
		[STAThread] 
		static void Main()  
		{ 
			Application.Run(new Form1()); 
		} 
 
		private void Form1_Load(object sender, System.EventArgs e) 
		{ 
 
			//Create a new AoInitialize object 
			if (m_AoInitialize == null) 
			{ 
				System.Windows.Forms.MessageBox.Show("Unable to initialize. This application cannot run!"); 
				this.Close(); 
			} 
			//Determine if the product is available 
			esriLicenseStatus licenseStatus = (esriLicenseStatus) m_AoInitialize.IsProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine); 
			if (licenseStatus == esriLicenseStatus.esriLicenseAvailable) 
			{ 
				licenseStatus = (esriLicenseStatus) m_AoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeEngine); 
				if (licenseStatus != esriLicenseStatus.esriLicenseCheckedOut) 
				{ 
					System.Windows.Forms.MessageBox.Show("The initialization failed. This application cannot run!"); 
					this.Close(); 
				} 
			} 
			else	 
			{				 
				System.Windows.Forms.MessageBox.Show("The ArcGIS Engine product is unavailable. This application cannot run!"); 
				this.Close(); 
			} 
		 
			//Suppress drawing while resizing 
			this.SetStyle(ControlStyles.EnableNotifyMessage,true); 
 
			//Create the customize dialog for the ToolbarControl 
			CreateCustomizeDialog(); 
 
			//Create symbol used on the MapControl 
			CreateOverviewSymbol(); 
 
			//Set label editing to manual 
			axTOCControl1.LabelEdit = esriTOCControlEdit.esriTOCControlManual; 
 
			string progID; 
			//Add generic commands 
			progID = "esriControlToolsGeneric.ControlsOpenDocCommand"; 
			axToolbarControl1.AddItem(progID, -1 , -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); 
			//Add PageLayout navigation commands 
			progID = "esriControlToolsPageLayout.ControlsPageZoomInTool"; 
			axToolbarControl1.AddItem(progID, -1, -1, true, 0, esriCommandStyles.esriCommandStyleIconOnly); 
			progID = "esriControlToolsPageLayout.ControlsPageZoomOutTool"; 
			axToolbarControl1.AddItem(progID, -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); 
			progID = "esriControlToolsPageLayout.ControlsPagePanTool"; 
			axToolbarControl1.AddItem(progID, -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); 
			progID = "esriControlToolsPageLayout.ControlsPageZoomWholePageCommand"; 
			axToolbarControl1.AddItem(progID, -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); 
			progID = "esriControlToolsPageLayout.ControlsPageZoomPageToLastExtentBackCommand"; 
			axToolbarControl1.AddItem(progID, -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); 
			progID = "esriControlToolsPageLayout.ControlsPageZoomPageToLastExtentForwardCommand"; 
			axToolbarControl1.AddItem(progID, -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); 
			//Add Map naviagtion commands 
			progID = "esriControlToolsMapNavigation.ControlsMapZoomInTool"; 
			axToolbarControl1.AddItem(progID, -1, -1, true, 0, esriCommandStyles.esriCommandStyleIconOnly); 
			progID = "esriControlToolsMapNavigation.ControlsMapZoomOutTool"; 
			axToolbarControl1.AddItem(progID, -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); 
			progID = "esriControlToolsMapNavigation.ControlsMapPanTool"; 
			axToolbarControl1.AddItem(progID, -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); 
			progID = "esriControlToolsMapNavigation.ControlsMapFullExtentCommand"; 
			axToolbarControl1.AddItem(progID, -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); 
			//Add custom date tool 
			progID = "CSharpDotNETCommands.AddDateTool"; 
			axToolbarControl1.AddItem(progID, -1, -1, true, 0, esriCommandStyles.esriCommandStyleIconAndText); 
 
			//Share the ToolbarControl's command pool 
			m_ToolbarMenu.CommandPool = axToolbarControl1.CommandPool; 
			//Add commands to the ToolbarMenu 
			progID = "esriControlToolsPageLayout.ControlsPageZoomInFixedCommand"; 
			m_ToolbarMenu.AddItem(progID, -1, -1, false, esriCommandStyles.esriCommandStyleIconAndText); 
			progID = "esriControlToolsPageLayout.ControlsPageZoomOutFixedCommand"; 
			m_ToolbarMenu.AddItem(progID, -1, -1, false, esriCommandStyles.esriCommandStyleIconAndText); 
			progID = "esriControlToolsPageLayout.ControlsPageZoomWholePageCommand"; 
			m_ToolbarMenu.AddItem(progID, -1, -1, false, esriCommandStyles.esriCommandStyleIconAndText); 
			progID = "esriControlToolsPageLayout.ControlsPageZoomPageToLastExtentBackCommand"; 
			m_ToolbarMenu.AddItem(progID, -1, -1, true, esriCommandStyles.esriCommandStyleIconAndText); 
			progID = "esriControlToolsPageLayout.ControlsPageZoomPageToLastExtentForwardCommand"; 
			m_ToolbarMenu.AddItem(progID, -1, -1, false, esriCommandStyles.esriCommandStyleIconAndText); 
			//Set the hook to the PageLayoutControl 
			m_ToolbarMenu.SetHook(axPageLayoutControl1); 
 
 
			//Load a pre-authored map document into the PageLayoutControl using relative paths 
			string fileName = @"..\..\..\..\..\..\..\..\Data\ArcGIS_Engine_Developer_Guide\Gulf of St. Lawrence.mxd"; 
			if (axPageLayoutControl1.CheckMxFile(fileName))  
			{ 
				axPageLayoutControl1.LoadMxFile(fileName,""); 
			} 
 
			//Set buddy controls 
			axTOCControl1.SetBuddyControl(axPageLayoutControl1); 
			axToolbarControl1.SetBuddyControl(axPageLayoutControl1); 
		} 
 
		private void OnVisibleBoundsUpdated(IDisplayTransformation sender, bool sizeChanged)  
		{ 
			//Set the extent to the new visible extent 
			m_Envelope = sender.VisibleBounds; 
			//Refresh the MapControl's foreground phase 
			axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewForeground, null, null); 
		} 
 
		private void OnStartDialog() 
		{ 
			axToolbarControl1.Customize = true; 
		} 
 
		private void OnCloseDialog() 
		{ 
			axToolbarControl1.Customize = false; 
			chkCustomize.Checked = false; 
		} 
 
		private void CreateOverviewSymbol() 
		{ 
			//Get the IRGBColor interface 
			IRgbColor color = new RgbColor(); 
			//Set the color properties 
			color.RGB = 255; 
 
			//Get the ILine symbol interface 
			ILineSymbol outline = new SimpleLineSymbol(); 
			//Set the line symbol properties 
			outline.Width = 1.5; 
			outline.Color = color; 
 
			//Get the IFillSymbol interface 
			ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbolClass();  
			//Set the fill symbol properties 
			simpleFillSymbol.Outline = outline; 
			simpleFillSymbol.Style = esriSimpleFillStyle.esriSFSHollow; 
			m_FillSymbol = simpleFillSymbol; 
		} 
 
		private void CreateCustomizeDialog() 
		{ 
			//Set the customize dialog events  
			startDialogE = new ICustomizeDialogEvents_OnStartDialogEventHandler(OnStartDialog); 
			((ICustomizeDialogEvents_Event)m_CustomizeDialog).OnStartDialog += startDialogE; 
			closeDialogE = new ICustomizeDialogEvents_OnCloseDialogEventHandler(OnCloseDialog); 
			((ICustomizeDialogEvents_Event)m_CustomizeDialog).OnCloseDialog += closeDialogE;			 
			 
			//Set the title 
			m_CustomizeDialog.DialogTitle = "Customize ToolbarControl Items"; 
			//Show the 'Add from File' button 
			m_CustomizeDialog.ShowAddFromFile = true; 
			//Set the ToolbarControl that new items will be added to 
			m_CustomizeDialog.SetDoubleClickDestination(axToolbarControl1); 
		} 
 
		private void chkCustomize_CheckedChanged(object sender, System.EventArgs e) 
		{ 
			//Show or hide the customize dialog 
			if (chkCustomize.Checked == false) 
			{ 
				m_CustomizeDialog.CloseDialog(); 
				axToolbarControl1.Customize = false; 
			} 
			else 
			{ 
				m_CustomizeDialog.StartDialog(axToolbarControl1.hWnd); 
				axToolbarControl1.Customize = true; 
			} 
		} 
 
		private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) 
		{ 
			//Release COM objects and shut down the AoInitilaize object 
			ESRI.ArcGIS.Utility.COMSupport.AOUninitialize.Shutdown(); 
			m_AoInitialize.Shutdown(); 
		} 
 
		private void axPageLayoutControl1_OnMouseDown(object sender, ESRI.ArcGIS.PageLayoutControl.IPageLayoutControlEvents_OnMouseDownEvent e) 
		{ 
			//Popup the ToolbarMenu 
			if (e.button == 2) 
			{ 
				m_ToolbarMenu.PopupMenu(e.x,e.y,axPageLayoutControl1.hWnd); 
			}	 
		} 
 
		private void axTOCControl1_OnEndLabelEdit(object sender, ESRI.ArcGIS.TOCControl.ITOCControlEvents_OnEndLabelEditEvent e) 
		{ 
			//If the new label is an empty string then prevent the edit 
			string newLabel = e.newLabel; 
			if (newLabel.Trim() == "")  
			{ 
				e.canEdit = false; 
			} 
		} 
 
		private void axMapControl1_OnAfterDraw(object sender, ESRI.ArcGIS.MapControl.IMapControlEvents2_OnAfterDrawEvent e) 
		{ 
			if (m_Envelope == null) 
			{ 
				return; 
			}  
 
			//If the foreground phase has drawn			 
			esriViewDrawPhase viewDrawPhase = (esriViewDrawPhase) e.viewDrawPhase; 
			if (viewDrawPhase == esriViewDrawPhase.esriViewForeground) 
			{					 
				IGeometry geometry = m_Envelope;	 
				axMapControl1.DrawShape(geometry, ref m_FillSymbol); 
			} 
		} 
 
		private void axPageLayoutControl1_OnPageLayoutReplaced(object sender, ESRI.ArcGIS.PageLayoutControl.IPageLayoutControlEvents_OnPageLayoutReplacedEvent e) 
		{ 
			//Get the IActiveView of the focus map in the PageLayoutControl 
			IActiveView activeView = (IActiveView) axPageLayoutControl1.ActiveView.FocusMap; 
			//Trap the ITranformEvents of the PageLayoutCntrol's focus map			 
			visBoundsUpdatedE = new ITransformEvents_VisibleBoundsUpdatedEventHandler(OnVisibleBoundsUpdated); 
			((ITransformEvents_Event)activeView.ScreenDisplay.DisplayTransformation).VisibleBoundsUpdated += visBoundsUpdatedE; 
			//Get the extent of the focus map 
			m_Envelope = activeView.Extent; 
 
			//Load the same pre-authored map document into the MapControl 
			axMapControl1.LoadMxFile(axPageLayoutControl1.DocumentFilename,null,null); 
			//Set the extent of the MapControl to the full extent of the data 
			axMapControl1.Extent = axMapControl1.FullExtent; 
		} 
	 
		protected override void OnNotifyMessage(System.Windows.Forms.Message m) 
		{ 
        /*This method of suppressing resize drawing works by examining the windows messages  
        sent to the form. When a form starts resizing, windows sends the WM_ENTERSIZEMOVE  
        Windows(messge). At this point we suppress drawing to the MapControl and  
        PageLayoutControl and draw using a "stretchy bitmap". When windows sends the  
        WM_EXITSIZEMOVE the form is released from resizing and we resume with a full  
        redraw at the new extent. 
 
        Note in DotNet forms we can not simply use the second parameter in a Form_Load  
        event to automatically detect when a form is resized as follows: 
        AxPageLayoutControl1.SuppressResizeDrawing(False, Me.Handle.ToInt32) 
        This results in a System.NullException when the form closes (after layers have been  
        loaded). This is a limitation caused by .Net's particular implementation of its  
        windows message pump which conflicts with "windows subclassing" used to watch the 
        forms window.*/ 
 
			base.OnNotifyMessage (m); 
			 
			if (m.Msg == WM_ENTERSIZEMOVE) 
			{ 
				axMapControl1.SuppressResizeDrawing(true, 0); 
				axPageLayoutControl1.SuppressResizeDrawing(true, 0); 
			} 
			else if (m.Msg == WM_EXITSIZEMOVE) 
			{ 
				axMapControl1.SuppressResizeDrawing(false, 0); 
				axPageLayoutControl1.SuppressResizeDrawing(false, 0); 
			} 
		} 
	} 
}