#region 一些相关引用
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.esriSystem;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.SystemUI;
#endregion
namespace mapcontrol
{
public partial class Form1 : Form
{
//定义的全局变量
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private IMapDocument m_MapDocument;
public Form1()
{
InitializeComponent();
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
//打开对话框的设置
openFileDialog1= new OpenFileDialog() ;
openFileDialog1.Title = "Open Map Document";
openFileDialog1.Filter = "Map Documents (*.mxd)|*.mxd";
openFileDialog1.ShowDialog();
// 判断打开路径是否为空
string sFilePath = openFileDialog1.FileName;
if (sFilePath == "")
{
return;
}
//打开文档,调用OpenDocument()函数
OpenDocument((sFilePath));
}
private void OpenDocument(string sFilePath)
{
//创建m_Mapdocument
m_MapDocument = new MapDocumentClass();
m_MapDocument.Open(sFilePath, "");
//遍历每个map对象
for (int i = 0; i < m_MapDocument.MapCount; i++)
{
axMapControl1.Map = m_MapDocument.get_Map(i);
}
}
private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveDocument(); //调用SaveDocument 函数
}
private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
{
//打开保存对话框
saveFileDialog1=new SaveFileDialog();
saveFileDialog1.Title = "保存地图文档";
saveFileDialog1.Filter = "Map Documents (*.mxd)|*.mxd";
saveFileDialog1.ShowDialog();
//判断打开路径是否为空
string sFilePath = saveFileDialog1.FileName;
if (sFilePath == "")
{
return;
}
if (sFilePath == m_MapDocument.DocumentFilename)
{
//保存变更后文档
SaveDocument();
}
else
{
//另存
m_MapDocument.SaveAs(sFilePath, true, true);
//打开另存文件
OpenDocument((sFilePath));
MessageBox.Show("保存成功!");
}
}
private void SaveDocument()
{
//判断文档是否为只读文件
if (m_MapDocument.get_IsReadOnly(m_MapDocument.DocumentFilename) == true)
{
MessageBox.Show("这个地图文档是只读文件!");
return;
}
//保存
m_MapDocument.Save(m_MapDocument.UsesRelativePaths, true);
MessageBox.Show("保存成功!");
}
}
}
打开和保存mapcontrol中的地图
最新推荐文章于 2025-06-24 13:01:26 发布