using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using System.IO;
namespace AEKF
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//加载地图
private void button1_Click(object sender, EventArgs e)
{
loadMapDocument();
}
//加载特定地图
private void button2_Click(object sender, EventArgs e)
{
loadMapDocument2();
}
//加载地图方法
private void loadMapDocument()
{
System.Windows.Forms.OpenFileDialog openFileDialog;
openFileDialog = new OpenFileDialog();
openFileDialog.Title = "打开地图文档";
openFileDialog.Filter = "map documents(*.mxd)|*.mxd";
openFileDialog.ShowDialog();
string filePath = openFileDialog.FileName;
if (axMapControl1.CheckMxFile(filePath))
{
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerHourglass;
axMapControl1.LoadMxFile(filePath, 0, Type.Missing);
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
}
else
{
MessageBox.Show(filePath + "不是有效文档");
}
}
//加载特定地图方法
private void loadMapDocument2()
{
System.Windows.Forms.OpenFileDialog openFileDialog;
openFileDialog = new OpenFileDialog();
openFileDialog.Title = "打开地图文档";
openFileDialog.Filter = "map documents(*.mxd)|*.mxd";
openFileDialog.ShowDialog();
string filePath = openFileDialog.FileName;
if (axMapControl1.CheckMxFile(filePath))
{
IArray arrayMap = axMapControl1.ReadMxMaps(filePath, Type.Missing);
int i;
IMap map;
for (i = 0; i < arrayMap.Count; i++)
{
map = arrayMap.get_Element(i) as IMap;
if (map.Name == "layers")
{
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerHourglass;
axMapControl1.LoadMxFile(filePath, 0, Type.Missing);
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
break;
}
}
}
else
{
MessageBox.Show(filePath + "不是有效地图文档");
}
}
// 打开地图
private void button3_Click(object sender, EventArgs e)
{
loadMapDoc();
}
IMapDocument mapDocument;
//打开地图方法
private void loadMapDoc()
{
mapDocument = new MapDocumentClass();
try
{
System.Windows.Forms.OpenFileDialog openFileDialog;
openFileDialog = new OpenFileDialog();
openFileDialog.Title = "打开地图文档";
openFileDialog.Filter = "map document(*.mxd)|*.mxd";
openFileDialog.ShowDialog();
string filePath = openFileDialog.FileName;
mapDocument.Open(filePath, "");
for (int i = 0; i < mapDocument.MapCount; i++)
{
axMapControl1.Map = mapDocument.get_Map(i);
}
axMapControl1.Refresh();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
//保存地图
private void button4_Click(object sender, EventArgs e)
{
saveDocument();
}
//保存地图方法
private void saveDocument()
{
if (mapDocument.get_IsReadOnly(mapDocument.DocumentFilename) == true)
{
MessageBox.Show("地图文档是只读,无法保存");
}
try
{
mapDocument.Save(mapDocument.UsesRelativePaths, true);
MessageBox.Show("保存地图成功");
}
catch (Exception e)
{
MessageBox.Show("保存地图失败!"+e.ToString());
}
}
//另存地图
private void button5_Click(object sender, EventArgs e)
{
saveASDocument();
}
//另存地图方法
private void saveASDocument()
{
if (mapDocument.get_IsReadOnly(mapDocument.DocumentFilename) == true)
{
MessageBox.Show("地图文档是只读,无法保存");
}
string fileSavePath = @"e:\new.mxd";
try
{
mapDocument.SaveAs(fileSavePath, true, true);
MessageBox.Show("另存地图文档成功");
}
catch(Exception e)
{
MessageBox.Show("另存地图文档失败"+e.ToString());
}
}
//添加图层
private void button6_Click(object sender, EventArgs e)
{
addLayerFile();
}
//添加图层方法
private void addLayerFile()
{
System.Windows.Forms.OpenFileDialog openFileDialog;
openFileDialog = new OpenFileDialog();
openFileDialog.Title = "打开图层文件";
openFileDialog.Filter = "map document(*.lyr)|*.lyr";
openFileDialog.ShowDialog();
string filepath = openFileDialog.FileName;
try
{
if (filepath != "" && filepath != null)
{
axMapControl1.AddLayerFromFile(filepath);
}
}
catch(Exception e)
{
MessageBox.Show("添加图层失败"+e.ToString());
}
}
//添加shape文件
private void button7_Click(object sender, EventArgs e)
{
addShapeFile();
}
//添加shape文件方法
private void addShapeFile()
{
System.Windows.Forms.OpenFileDialog openFileDialog;
openFileDialog = new OpenFileDialog();
openFileDialog.Title = "打开图层文件";
openFileDialog.Filter = "map document(*.shp)|*.shp";
openFileDialog.ShowDialog();
FileInfo fileInfo = new FileInfo(openFileDialog.FileName);
string path = openFileDialog.FileName.Substring(0,openFileDialog.FileName.Length);
try
{
axMapControl1.AddShapeFile(path,fileInfo.Name);
}
catch(Exception e)
{
MessageBox.Show("添加图层失败"+e.ToString());
}
}
//删除文件
private void button8_Click(object sender, EventArgs e)
{
deleteLayer();
}
//删除文件方法
private void deleteLayer()
{
try
{
for (int i = axMapControl1.LayerCount - 1; i >= 0; i--)
{
axMapControl1.DeleteLayer(i);
}
}
catch(Exception e)
{
MessageBox.Show("删除图层失败"+e.ToString());
}
}
//移动图层
private void button9_Click(object sender, EventArgs e)
{
moveLayer();
}
//移动图层方法
private void moveLayer()
{
if (axMapControl1.LayerCount > 0)
{
try
{
axMapControl1.MoveLayerTo(axMapControl1.LayerCount - 1, 0);
}
catch(Exception e)
{
MessageBox.Show("移动图层失败"+e.ToString());
}
}
}
}
}