三种方法加载MXD文档 (LoadMxFile方法和IMapDocument方法和IMapDocument方法)
LoadMxFile方法加载MXD文档:
private void loadMxFile方法ToolStripMenuItem_Click(object sender, EventArgs e) { //加载数据前如果有数据则清空 try { OpenFileDialog pOpenFileDialog = new OpenFileDialog();//打开文件 pOpenFileDialog.CheckFileExists = true;//判断文件存在 pOpenFileDialog.Title = "打开地图文档"; pOpenFileDialog.Filter = "ArcMap文档(*.mxd)|*.mxd;|ArcMap模板(*.mxt)|*.mxt|发布地图文件(*.pmf)|*.pmf|所有地图格式(*.mxd;*.mxt;*.pmf)|*.mxd;*.mxt;*.pmf"; pOpenFileDialog.Multiselect = false; //不允许多个文件同时选择 pOpenFileDialog.RestoreDirectory = true; //存储打开的文件路径 if (pOpenFileDialog.ShowDialog() == DialogResult.OK) { string pFileName = pOpenFileDialog.FileName; if (pFileName == "") { return; } if (mainMapControl.CheckMxFile(pFileName)) //MapControl的CheckMxFile方法检查地图文档是否为效的地图文档,返回值为Bool { ClearAllData(); mainMapControl.LoadMxFile(pFileName);//MapControl的LoadMxFile方法 加载地图文档 } else { MessageBox.Show(pFileName + "是无效的地图文档!", "信息提示"); return; } } } catch (Exception ex) { MessageBox.Show("打开地图文档失败" + ex.Message); } } #region 封装的方法 private void ClearAllData() { if (mainMapControl.Map != null && mainMapControl.Map.LayerCount > 0) { //新建mainMapControl中Map IMap dataMap = new MapClass(); dataMap.Name = "Map"; mainMapControl.DocumentFilename = string.Empty; mainMapControl.Map = dataMap; //新建EagleEyeMapControl中Map IMap eagleEyeMap = new MapClass(); eagleEyeMap.Name = "eagleEyeMap"; EagleEyeMapControl.DocumentFilename = string.Empty; EagleEyeMapControl.Map = eagleEyeMap; } } #endregion
#region IMapDocument方法加载Mxd文档文件 private void iMapDocument方法ToolStripMenuItem_Click(object sender, EventArgs e) { try { OpenFileDialog pOpenFileDialog = new OpenFileDialog(); pOpenFileDialog.CheckFileExists = true; pOpenFileDialog.Title = "打开地图文档"; pOpenFileDialog.Filter = "ArcMap文档(*.mxd)|*.mxd;|ArcMap模板(*.mxt)|*.mxt|发布地图文件(*.pmf)|*.pmf|所有地图格式(*.mxd;*.mxt;*.pmf)|*.mxd;*.mxt;*.pmf"; pOpenFileDialog.Multiselect = false; pOpenFileDialog.RestoreDirectory = true; if (pOpenFileDialog.ShowDialog() == DialogResult.OK) { string pFileName = pOpenFileDialog.FileName; if (pFileName == "") { return; } if (mainMapControl.CheckMxFile(pFileName)) //检查地图文档有效性 { //将数据载入pMapDocument并与Map控件关联 IMapDocument pMapDocument = new MapDocument();//using ESRI.ArcGIS.Carto; pMapDocument.Open(pFileName, ""); //获取Map中激活的地图文档 mainMapControl.Map = pMapDocument.ActiveView.FocusMap; mainMapControl.ActiveView.Refresh(); } else { MessageBox.Show(pFileName + "是无效的地图文档!", "信息提示"); return; } } } catch (Exception ex) { MessageBox.Show("打开地图文档失败" + ex.Message); } } #endregion
#region ControlsOpenDocCommandClass加载地图 private void controlsOpenDocCommandClass方法ToolStripMenuItem_Click(object sender, EventArgs e) { ICommand command = new ControlsOpenDocCommandClass(); command.OnCreate(mainMapControl.Object); command.OnClick(); } #endregion