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

📄 form1.cs

📁 好不容易找到的用多种语言写的ArcGisEngine二次开发程序
💻 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.esriSystem;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Carto;

namespace SaveMapDocument
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		public System.Windows.Forms.TextBox txtMapDocument;
		private ESRI.ArcGIS.ToolbarControl.AxToolbarControl axToolbarControl1;
		public System.Windows.Forms.Button cmdOpen;
		public System.Windows.Forms.Button cmdSave;
		public System.Windows.Forms.Button cmdSaveAs;
		private ESRI.ArcGIS.PageLayoutControl.AxPageLayoutControl axPageLayoutControl1;
		private ESRI.ArcGIS.TOCControl.AxTOCControl axTOCControl1;
		private System.Windows.Forms.OpenFileDialog openFileDialog1;
		private System.Windows.Forms.SaveFileDialog saveFileDialog1;
		private IAoInitialize m_AoInitialize = new AoInitializeClass();
		private IMapDocument m_MapDocument;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.txtMapDocument = new System.Windows.Forms.TextBox();
			this.axToolbarControl1 = new ESRI.ArcGIS.ToolbarControl.AxToolbarControl();
			this.cmdOpen = new System.Windows.Forms.Button();
			this.cmdSave = new System.Windows.Forms.Button();
			this.cmdSaveAs = new System.Windows.Forms.Button();
			this.axPageLayoutControl1 = new ESRI.ArcGIS.PageLayoutControl.AxPageLayoutControl();
			this.axTOCControl1 = new ESRI.ArcGIS.TOCControl.AxTOCControl();
			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
			((System.ComponentModel.ISupportInitialize)(this.axToolbarControl1)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.axPageLayoutControl1)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.axTOCControl1)).BeginInit();
			this.SuspendLayout();
			// 
			// txtMapDocument
			// 
			this.txtMapDocument.AcceptsReturn = true;
			this.txtMapDocument.AutoSize = false;
			this.txtMapDocument.BackColor = System.Drawing.SystemColors.Window;
			this.txtMapDocument.Cursor = System.Windows.Forms.Cursors.IBeam;
			this.txtMapDocument.Enabled = false;
			this.txtMapDocument.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.txtMapDocument.ForeColor = System.Drawing.SystemColors.WindowText;
			this.txtMapDocument.Location = new System.Drawing.Point(8, 424);
			this.txtMapDocument.MaxLength = 0;
			this.txtMapDocument.Name = "txtMapDocument";
			this.txtMapDocument.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtMapDocument.Size = new System.Drawing.Size(513, 19);
			this.txtMapDocument.TabIndex = 7;
			this.txtMapDocument.Text = "";
			// 
			// 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(640, 26);
			this.axToolbarControl1.TabIndex = 8;
			// 
			// cmdOpen
			// 
			this.cmdOpen.BackColor = System.Drawing.SystemColors.Control;
			this.cmdOpen.Cursor = System.Windows.Forms.Cursors.Default;
			this.cmdOpen.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.cmdOpen.ForeColor = System.Drawing.SystemColors.ControlText;
			this.cmdOpen.Location = new System.Drawing.Point(528, 48);
			this.cmdOpen.Name = "cmdOpen";
			this.cmdOpen.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.cmdOpen.Size = new System.Drawing.Size(121, 33);
			this.cmdOpen.TabIndex = 11;
			this.cmdOpen.Text = "Open Document...";
			this.cmdOpen.Click += new System.EventHandler(this.cmdOpen_Click);
			// 
			// cmdSave
			// 
			this.cmdSave.BackColor = System.Drawing.SystemColors.Control;
			this.cmdSave.Cursor = System.Windows.Forms.Cursors.Default;
			this.cmdSave.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.cmdSave.ForeColor = System.Drawing.SystemColors.ControlText;
			this.cmdSave.Location = new System.Drawing.Point(528, 128);
			this.cmdSave.Name = "cmdSave";
			this.cmdSave.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.cmdSave.Size = new System.Drawing.Size(121, 33);
			this.cmdSave.TabIndex = 10;
			this.cmdSave.Text = "Save Document";
			this.cmdSave.Click += new System.EventHandler(this.cmdSave_Click);
			// 
			// cmdSaveAs
			// 
			this.cmdSaveAs.BackColor = System.Drawing.SystemColors.Control;
			this.cmdSaveAs.Cursor = System.Windows.Forms.Cursors.Default;
			this.cmdSaveAs.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.cmdSaveAs.ForeColor = System.Drawing.SystemColors.ControlText;
			this.cmdSaveAs.Location = new System.Drawing.Point(528, 88);
			this.cmdSaveAs.Name = "cmdSaveAs";
			this.cmdSaveAs.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.cmdSaveAs.Size = new System.Drawing.Size(121, 33);
			this.cmdSaveAs.TabIndex = 9;
			this.cmdSaveAs.Text = "Save Document As...";
			this.cmdSaveAs.Click += new System.EventHandler(this.cmdSaveAs_Click);
			// 
			// axPageLayoutControl1
			// 
			this.axPageLayoutControl1.Location = new System.Drawing.Point(168, 40);
			this.axPageLayoutControl1.Name = "axPageLayoutControl1";
			this.axPageLayoutControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axPageLayoutControl1.OcxState")));
			this.axPageLayoutControl1.Size = new System.Drawing.Size(352, 376);
			this.axPageLayoutControl1.TabIndex = 12;
			// 
			// axTOCControl1
			// 
			this.axTOCControl1.Location = new System.Drawing.Point(8, 40);
			this.axTOCControl1.Name = "axTOCControl1";
			this.axTOCControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axTOCControl1.OcxState")));
			this.axTOCControl1.Size = new System.Drawing.Size(152, 376);
			this.axTOCControl1.TabIndex = 13;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(656, 454);
			this.Controls.Add(this.axTOCControl1);
			this.Controls.Add(this.axPageLayoutControl1);
			this.Controls.Add(this.cmdOpen);
			this.Controls.Add(this.cmdSave);
			this.Controls.Add(this.cmdSaveAs);
			this.Controls.Add(this.axToolbarControl1);
			this.Controls.Add(this.txtMapDocument);
			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.axToolbarControl1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.axPageLayoutControl1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.axTOCControl1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			//Initialize the application
			if (CheckOutLicenses(esriLicenseProductCode.esriLicenseProductCodeEngine) != esriLicenseStatus.esriLicenseCheckedOut)
			{
				if (CheckOutLicenses(esriLicenseProductCode.esriLicenseProductCodeArcView) != esriLicenseStatus.esriLicenseCheckedOut)
				{
					if (CheckOutLicenses(esriLicenseProductCode.esriLicenseProductCodeArcEditor) != esriLicenseStatus.esriLicenseCheckedOut)
					{
						if (CheckOutLicenses(esriLicenseProductCode.esriLicenseProductCodeArcInfo) != esriLicenseStatus.esriLicenseCheckedOut)
						{
							System.Windows.Forms.MessageBox.Show("The initialization failed. This application cannot run!");
							this.Close();
						}
					}
				}
			}

			//Add toolbar definitions to the ToolbarControl
			axToolbarControl1.AddToolbarDef("esriControlCommands.ControlsPageLayoutToolbar", -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
			axToolbarControl1.AddToolbarDef("esriControlCommands.ControlsGraphicElementToolbar", -1, true, 0, esriCommandStyles.esriCommandStyleIconOnly);

			//Set buddy control
			axToolbarControl1.SetBuddyControl(axPageLayoutControl1);
			axTOCControl1.SetBuddyControl(axPageLayoutControl1);

			cmdSave.Enabled = false;
			cmdSaveAs.Enabled = false;

		}

		private void cmdOpen_Click(object sender, System.EventArgs e)
		{
			//Open a file dialog for opening map documents
			openFileDialog1.Title = "Open Map Document";
			openFileDialog1.Filter = "Map Documents (*.mxd)|*.mxd";
			openFileDialog1.ShowDialog();

			// Exit if no map document is selected
			string sFilePath = openFileDialog1.FileName;
			if (sFilePath == "") 
			{
				return;
			}
			
			//Open document
			OpenDocument((sFilePath));

			if (cmdSave.Enabled == false)
			{
				cmdSave.Enabled = true;
			}					
			if (cmdSaveAs.Enabled == false)
			{
				cmdSaveAs.Enabled = true;
			}	
		}

		private void cmdSave_Click(object sender, System.EventArgs e)
		{
			//Save changes to the current document
			SaveDocument();
		}

		private void cmdSaveAs_Click(object sender, System.EventArgs e)
		{
			//Open a file dialog for saving map documents
			saveFileDialog1.Title = "Save Map Document As";
			saveFileDialog1.Filter = "Map Documents (*.mxd)|*.mxd";
			saveFileDialog1.ShowDialog();

			//Exit if no map document is selected
			string sFilePath = saveFileDialog1.FileName;
			if (sFilePath == "")
			{
				return;
			}

			if (sFilePath == m_MapDocument.DocumentFilename)
			{
				//Save changes to the current document
				SaveDocument();
			}
			else
			{
				//SaveAs a new document with relative paths
				m_MapDocument.SaveAs(sFilePath, true, true);
				//Open document
				OpenDocument((sFilePath));
				MessageBox.Show("Document saved successfully!");
			}
		}

		private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			//Release COM objects and shutdown
			ESRI.ArcGIS.Utility.COMSupport.AOUninitialize.Shutdown();
			m_AoInitialize.Shutdown();
		}

		private void OpenDocument(string sFilePath)
		{
			//Create a new map document
			m_MapDocument = new MapDocumentClass();
			//Open the map document selected
			m_MapDocument.Open(sFilePath,"");
			//Set the PageLayoutControl page layout to the map document page layout
			axPageLayoutControl1.PageLayout = m_MapDocument.PageLayout;
			txtMapDocument.Text = m_MapDocument.DocumentFilename;
		}

		private void SaveDocument()
		{
			//Check that the document is not read only
			if (m_MapDocument.get_IsReadOnly(m_MapDocument.DocumentFilename) == true) 
			{
				MessageBox.Show("This map document is read only!");
				return;
			}
			//Save with the current relative path setting
			m_MapDocument.Save(m_MapDocument.UsesRelativePaths,true);
			MessageBox.Show("Changes saved successfully!");
		}

		private esriLicenseStatus CheckOutLicenses(esriLicenseProductCode productCode) 
		{
			esriLicenseStatus licenseStatus = esriLicenseStatus.esriLicenseUnavailable;
			
			m_AoInitialize = new AoInitializeClass();
			if (m_AoInitialize == null) return licenseStatus;


			//Determine if the product is available
			licenseStatus = m_AoInitialize.IsProductCodeAvailable(productCode);
			if (licenseStatus == esriLicenseStatus.esriLicenseAvailable)
			{
				licenseStatus = m_AoInitialize.Initialize(productCode);
			}
			return licenseStatus;
		}
	}
}

⌨️ 快捷键说明

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