1、向MapControl添加“图层”选项操作
①设计菜单栏“图层”选项
![](https://img-blog.csdnimg.cn/20210407111409883.png)
②添加“添加ShapeFile图层”代码
private void 添加Shapefile图层ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog;
openFileDialog = new OpenFileDialog();
openFileDialog.Title = "打开shp文件";
openFileDialog.Filter = "shp layer(*.shp)|*.shp";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
mainMapControl.ClearLayers();
string file = openFileDialog.FileName;
string filePath, fileName;
int index = 0;
index = file.LastIndexOf("\\");
filePath = file.Substring(0, index);
fileName = file.Substring(index + 1, file.Length - index - 1);
IWorkspaceFactory workspaceFactory;
IFeatureWorkspace featureWorkspace;
IFeatureLayer featureLayer;
workspaceFactory = new ShapefileWorkspaceFactoryClass();
featureWorkspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile(filePath, 0);
//featureWorkspace = workspaceFactory.OpenFromFile(filePath, 0) as IFeatureWorkspace;
featureLayer = new FeatureLayerClass();
featureLayer.FeatureClass = featureWorkspace.OpenFeatureClass(fileName);
featureLayer.Name = featureLayer.FeatureClass.AliasName;
this.mainMapControl.AddLayer(featureLayer as ILayer);
this.mainMapControl.Refresh();
}
}
打开shp文件:
![](https://img-blog.csdnimg.cn/20210407111556956.png)
③添加“添加CAD文件栅格图层”代码
private void 添加CAD文件栅格图层ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "CAD (*.dwg)|*.dwg";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;
openFileDialog.Multiselect = false;
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string filename = openFileDialog.FileName;
if (filename != "")
{
mainMapControl.ClearLayers();
IWorkspaceFactory pCadWorkspaceFactory = new CadWorkspaceFactoryClass();
IWorkspace pWorkspace = pCadWorkspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(filename), 0);//(@"C:\arcgis\ArcTutor\Data Interoperability\birds_eggs", 0);
ICadDrawingWorkspace pCadDrawingWorkspace = pWorkspace as ICadDrawingWorkspace;
ICadDrawingDataset pCadDataset = pCadDrawingWorkspace.OpenCadDrawingDataset(System.IO.Path.GetFileName(filename)); ;//("template.dwg");
ICadLayer pCadLayer = new CadLayerClass();
pCadLayer.CadDrawingDataset = pCadDataset;
// pCadLayer.Name = System.IO.Path.GetFileName(filename);
mainMapControl.AddLayer(pCadLayer, 0);
}
}
}
打开CAD栅格文件:
![](https://img-blog.csdnimg.cn/20210407111758502.png)
④添加“添加CAD文件矢量图层”代码
private void 添加CAD文件矢量图层ToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();
openFileDialog.Filter = "CAD (*.dwg)|*.dwg";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;
openFileDialog.Multiselect = false;
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string filename = openFileDialog.FileName;
if (filename != "")
{
mainMapControl.ClearLayers();
IWorkspaceFactory pCadWorkspacefactory = new CadWorkspaceFactoryClass();
IFeatureWorkspace pWorkspace = pCadWorkspacefactory.OpenFromFile(System.IO.Path.GetDirectoryName(filename), 0) as IFeatureWorkspace;//将文件路径改为您电脑上的实际CAD文件路径
IFeatureDataset pFeatDataset = pWorkspace.OpenFeatureDataset(System.IO.Path.GetFileName(filename));//改为您的电脑上的实际cad文件名。
//pFeatClassContainer可以管理pFeatDataset中的每个要素集
IFeatureClassContainer pFeatClassContainer;
pFeatClassContainer = pFeatDataset as IFeatureClassContainer;
IFeatureClass pFeatClass; IFeatureLayer pFeatLayer; int i;
//对CAD文件中的要素集进行遍历处理
for (i = 0; i <= pFeatClassContainer.ClassCount - 1; i++)
{
pFeatClass = pFeatClassContainer.get_Class(i);
if (pFeatClass.FeatureType == esriFeatureType.esriFTCoverageAnnotation)
{ //标注类型,必须设置为标注图层
pFeatLayer = new CadAnnotationLayerClass();
}
else
{ //点线面类型
pFeatLayer = new FeatureLayerClass();
}
pFeatLayer.Name = pFeatClass.AliasName;
pFeatLayer.FeatureClass = pFeatClass;
mainMapControl.AddLayer(pFeatLayer, 0);
}
}
}
}
打开CAD矢量文件:
![](https://img-blog.csdnimg.cn/20210407112052397.png)
⑤添加“添加栅格图层”代码
private void 添加栅格图层ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog pOpenFileDialog = new OpenFileDialog();
pOpenFileDialog.CheckFileExists = true;
pOpenFileDialog.Title = "打开Raster文件";
pOpenFileDialog.Filter = "栅格文件 (*.*)|*.bmp;*.tif;*.jpg;*.img|(*.bmp)|*.bmp|(*.tif)|*.tif|(*.jpg)|*.jpg|(*.img)|*.img";
pOpenFileDialog.ShowDialog();
string pRasterFileName = pOpenFileDialog.FileName;
IRasterLayer rasterLayer = new RasterLayerClass();
rasterLayer.CreateFromFilePath(pRasterFileName);
if (rasterLayer != null)
{
IMap pMap = mainMapControl.Map;
IActiveView pActiveView;
pMap.AddLayer(rasterLayer);
pActiveView = pMap as IActiveView;
pActiveView.Refresh();
}
}
打开栅格文件:
![](https://img-blog.csdnimg.cn/20210407112545371.png)
⑥添加“删除图层”代码
private void 删除图层ToolStripMenuItem_Click(object sender, EventArgs e)
{
ILayer pLayer = new FeatureLayerClass();
int count;
IMap pMap = mainMapControl.Map;
IActiveView pActiveView;
pActiveView = pMap as IActiveView;
count = pMap.LayerCount;
pMap.DeleteLayer(pMap.get_Layer(count - 1));
IGraphicsContainer pGraphicscontainer;
pGraphicscontainer = pActiveView as IGraphicsContainer;
pGraphicscontainer.DeleteAllElements();
//pMap.ClearLayers();
pActiveView.Refresh();
}
删除图层前:
点击删除图层:
删除图层后:
⑤添加“另存地图文档”代码
![](https://img-blog.csdnimg.cn/20210407112150125.png)
![](https://img-blog.csdnimg.cn/2021040711215026.png)
![](https://img-blog.csdnimg.cn/20210407112149728.png)
⑦添加“移动图层”代码
private void 移动图层ToolStripMenuItem_Click(object sender, EventArgs e)
{
//IMap pMap = axMapControl1.Map;
//IActiveView pActiveView= pMap as IActiveView;;
// ILayer pLayer;
//pLayer=pMap.get_Layer(0);
//pMap.MoveLayer(pLayer, 1);
mainMapControl.MoveLayerTo(0, 1);
}
2、向MapControl添加“文件”选项操作
①引用命名空间SystemUI
using ESRI.ArcGIS.SystemUI;
②设计菜单栏“文件”选项
![](https://img-blog.csdnimg.cn/20210407112814929.png)
③定义声明变量m_MapDocument、m_mapControl
IMapDocument m_MapDocument = new MapDocumentClass();
private ITOCControl2 m_tocControl;
④添加“新建地图文档”代码
private void 新建地图文档ToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog SaveFileDialog2 = new SaveFileDialog();
SaveFileDialog2.Title = "输入新建地图文档名称";
SaveFileDialog2.Filter = "地图文档 (*.mxd)|*.mxd";
SaveFileDialog2.ShowDialog();
string sFilePath = SaveFileDialog2.FileName;
if (sFilePath == string.Empty)
{
return;
}
m_MapDocument.New(sFilePath);
m_MapDocument.Open(sFilePath, "");
mainMapControl.Map = m_MapDocument.get_Map(0);
}
新建地图文档:
![](https://img-blog.csdnimg.cn/20210407113220519.png)
⑤添加“另存地图文档”代码
private void 另存地图文档ToolStripMenuItem_Click(object sender, EventArgs e)
{
ICommand command = new ControlsSaveAsDocCommandClass();
command.OnCreate(m_mapControl.Object);
command.OnClick();
}
⑥添加“打开地图文档”代码
private void 打开地图文档ToolStripMenuItem_Click(object sender, EventArgs e)
{
//使用OpenFileDialog与MapDocument打开地图文档
OpenFileDialog openFileDialog2 = new OpenFileDialog();
openFileDialog2.Title = "Open Map Document";
openFileDialog2.Filter = "Map Documents (*.mxd)|*.mxd";
openFileDialog2.ShowDialog();
string sFilePath = openFileDialog2.FileName;
if (m_MapDocument.get_IsMapDocument(sFilePath))
{
m_MapDocument.Open(sFilePath, "");
mainMapControl.Map = m_MapDocument.get_Map(0);
mainMapControl.Refresh();
}
另存地图文档ToolStripMenuItem.Enabled = true;
}
打开地图文档:
![](https://img-blog.csdnimg.cn/20210407113354629.png)
⑦添加“保存地图文档”代码
private void 保存地图文档ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (m_MapDocument.get_IsReadOnly(m_MapDocument.DocumentFilename) == true)
{
MessageBox.Show("地图文档是只读的,无法保存");
}
try
{
m_MapDocument.Save(m_MapDocument.UsesRelativePaths, true);
MessageBox.Show("保存地图文档成功");
}
catch (Exception ex)
{
MessageBox.Show("保存地图文档失败!!!" + ex.ToString());
}
}
⑧添加“添加数据”代码
private void 添加数据ToolStripMenuItem_Click(object sender, EventArgs e)
{
ICommand command = new ControlsAddDataCommandClass();
command.OnCreate(m_mapControl.Object);
command.OnClick();
}
⑨添加“退出”代码
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}