利用C#进行AO开发--实现地图文件的打开、保存和另存为功能 (2007-10-27 08:51:26) 利用C#进行AO开发--实现地图文件的打开、保存和另存为功能 (2007-10-27 08:51:26) 由于C#继承了VB界面开发的友好性,所以完全可以利用C#提供的友好界面开发功能来实现winform的程序开发,在安装好了ArcEngine以后就可以利用其提供的控件进行GIS的二次开发,在form窗体上添加MapControl控件,MenuStrip控件(File(Open、Save、SaveAs、Exit)),saveFileDialog控件之后就可以进行编写代码,调试后的代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.Display; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Geometry; using ESRI.ArcGIS.Output; namespace openDocu { public partial class Form1 : Form { public Form1() { InitializeComponent(); } IMapDocument m_MapDcument; //openFileDialog private void LoadMapDocument() { m_MapDcument = new MapDocumentClass(); try { //打开文件对话框选取MXD文件 OpenFileDialog openFileDialog2; openFileDialog2 = new OpenFileDialog(); openFileDialog2.Title = "Open Map Document"; openFileDialog2.Filter = "Map Documents(*.mxd)|*.mxd"; openFileDialog2.ShowDialog(); string sFilePath = openFileDialog2.FileName; //将数据载入pMapDocument并与map控件联系起来 m_MapDcument.Open(sFilePath, ""); int i; for (i = 0; i <= m_MapDcument.MapCount - 1; i++) { //一个IMapDocument对象中可能有多个Map对象,遍历每个map对象 axMapControl1.Map = m_MapDcument.get_Map(i); } //刷新地图 axMapControl1.Refresh(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } //save map private void SaveDocument() { //check that the document is not read only if (m_MapDcument.get_IsReadOnly(m_MapDcument.DocumentFilename) == true) { MessageBox.Show("The map document is read only!"); return; } //save with the current relative path setting m_MapDcument.Save(m_MapDcument.UsesRelativePaths,true); MessageBox.Show("Change saved successfully!"); } //saveas private void SaveAsDocument() { //open a file dialog for saving map document saveFileDialog1.Title = "Sava Map Document As"; saveFileDialog1.Filter = "Map Documents(*.mxd)|*.mxd"; saveFileDialog1.ShowDialog(); //Exit if no map document is select string sFilePath = saveFileDialog1.FileName; if (sFilePath == "") { return; } if (sFilePath == m_MapDcument.DocumentFilename) { //Save change to the current document SaveDocument(); } else { //SaveAs a new docuemnt with relative paths m_MapDcument.SaveAs(sFilePath, true, true); //open document MessageBox.Show("Documnet saved successfully!"); } } private void openToolStripMenuItem_Click(object sender, EventArgs e) { LoadMapDocument(); } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { this.Close(); } private void saveToolStripMenuItem_Click(object sender, EventArgs e) { SaveDocument(); } private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) { SaveAsDocument(); } } }