// 首先确认当前地图文档是否有效 if (null != m_pageLayoutControl.DocumentFilename m_mapControl.CheckMxFile(m_pageLayoutControl.DocumentFilename)) {
// 创建一个新的地图文档实例
IMapDocument mapDoc = new MapDocumentClass(); // 打开当前地图文档
mapDoc.Open(m_pageLayoutControl.DocumentFilename, string.Empty); // 用 PageLayout 中的文档替换当前文档中的 PageLayout 部分
mapDoc.ReplaceContents((IMxdContents)m_pageLayoutControl.PageLayout); // 保存地图文档
mapDoc.Save(mapDoc.UsesRelativePaths, false); mapDoc.Close(); } }
///
/// 另存为地图文档命令 ///
///
private void SaveAs_Click(object sender, EventArgs e) {
&&
// 调用另存为命令
ICommand command = new ControlsSaveAsDocCommandClass(); command.OnCreate(m_controlsSynchronizer.ActiveControl); command.OnClick(); }
///
///
private void Exit_Click(object sender, EventArgs e) {
Application.Exit(); }
3、 编译运行
按 F5 编译运行程序。也许你会发现,菜单命令的实现方式都是类型的。没错,在 AE9.2 中,内置了许多常用的 Command 和 Tool ,如 ControlsAddDataCommandClass 、 ControlsMapZoomInToolClass 、 ControlsMapPanToolClass 等等,这些内置对象在 ESRI.ArcGIS.Controls 命名空间中,你可以对象浏览器中查看。而且这些内置对象的调用方式都类似,如下所示: // 定义
ICommand command = new ControlsSaveAsDocCommandClass(); // 创建
command.OnCreate(m_controlsSynchronizer.ActiveControl); // 调用
command.OnClick();
希望你可以举一反三,去实现更多的你想要的功能。
在下一讲中,我将给大家带来的是 MapControl 与 PageLayoutControl 两种视图同步的实现,
《ArcGIS Engine+C#实例开发教程》第三讲 MapControl与PageLayoutControl同步
在ArcMap中,能够很方面地进行MapView和Layout View两种视图的切换,而且二者之间的数据是同步显示的。 关于两种视图同步的实现方法有多种,可以使用ObjectCopy对象进行数据硬拷贝,而比较简单的方法莫过于二者共享一份地图了,这也是最常用的方法。 1、新建同步类ControlsSynchronizer
在解决方案面板中右击项目名,选择“添加|类”,在类别中选择“Visual C#项目项”,在模板中选择“类”,输入类名“ControlsSynchronizer.cs”,将以下代码覆盖自动生成的代码: using System;
using System.Drawing; using System.Collections;
using System.ComponentModel; using System.Windows.Forms; using System.IO;
using System.Runtime.InteropServices; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.SystemUI;
namespace _sdnMap {
///
/// This class is used to synchronize a gven PageLayoutControl and a MapControl.
/// When initialized, the user must pass the reference of these control to the class, bind /// the control together by calling 'BindControls' which in turn sets a joined Map referenced /// by both control; and set all the buddy controls joined between these two controls.
/// When alternating between the MapControl and PageLayoutControl, you should activate the visible control /// and deactivate the other by calling ActivateXXX.
/// This calss is limited to a situation where the controls are not simultaneously visible. ///
public class ControlsSynchronizer {
#region class members
private IMapControl3 m_mapControl = null;
private IPageLayoutControl2 m_pageLayoutControl = null; private ITool m_mapActiveTool = null;
private ITool m_pageLayoutActiveTool = null; private bool m_IsMapCtrlactive = true;
private ArrayList m_frameworkControls = null; #endregion
#region constructor
///
/// 默认构造函数 ///
public ControlsSynchronizer() {
//初始化ArrayList
m_frameworkControls = new ArrayList(); }
///
/// 构造函数 ///
///
///
public ControlsSynchronizer(IMapControl3 mapControl, IPageLayoutControl2 pageLayoutControl) : this() {
//为类成员赋值
m_mapControl = mapControl;
m_pageLayoutControl = pageLayoutControl; }
#endregion
#region properties ///
/// 取得或设置MapControl ///
public IMapControl3 MapControl {
get { return m_mapControl; } set { m_mapControl = value; } }
///
/// 取得或设置PageLayoutControl ///
public IPageLayoutControl2 PageLayoutControl {
get { return m_pageLayoutControl; } set { m_pageLayoutControl = value; } }
///
/// 取得当前ActiveView的类型 ///
public string ActiveViewType {
get {
if (m_IsMapCtrlactive)
return \ else
return \ } }
///
/// 取得当前活动的Control ///
public object ActiveControl {
get {
if (m_mapControl == null || m_pageLayoutControl == null)
throw new Exception(\MapControl PageLayoutControl are not initialized!\
if (m_IsMapCtrlactive)
return m_mapControl.Object; else
return m_pageLayoutControl.Object; } }
#endregion
#region Methods ///
/// 激活MapControl并解除the PagleLayoutControl ///
public void ActivateMap() {
try {
if (m_pageLayoutControl == null || m_mapControl == null)
throw new Exception(\MapControl PageLayoutControl are not initialized!\
//缓存当前PageLayout的CurrentTool if (m_pageLayoutControl.CurrentTool != null) m_pageLayoutActiveTool m_pageLayoutControl.CurrentTool;
//解除PagleLayout
m_pageLayoutControl.ActiveView.Deactivate(); //激活MapControl
m_mapControl.ActiveView.Activate(m_mapControl.hWnd);
//将之前MapControl最后使用的tool,作为活动的tool,赋给MapControl的CurrentTool if (m_mapActiveTool != null) m_mapControl.CurrentTool = m_mapActiveTool;
or
or
=
m_IsMapCtrlactive = true;
//为每一个的framework controls,设置Buddy control为MapControl this.SetBuddies(m_mapControl.Object); }
catch (Exception ex) {
throw new Exception(string.Format(\ } }
///
/// 激活PagleLayoutControl并减活MapCotrol ///
public void ActivatePageLayout() {
try {
if (m_pageLayoutControl == null || m_mapControl == null)
throw new Exception(\MapControl or PageLayoutControl are not initialized!\
//缓存当前MapControl的CurrentTool
if (m_mapControl.CurrentTool != null) m_mapActiveTool = m_mapControl.CurrentTool;
//解除MapControl
m_mapControl.ActiveView.Deactivate();
//激活PageLayoutControl
m_pageLayoutControl.ActiveView.Activate(m_pageLayoutControl.hWnd);
//将之前PageLayoutControl最后使用的tool,作为活动的tool,赋给PageLayoutControl的CurrentTool
if (m_pageLayoutActiveTool != null) m_pageLayoutControl.CurrentTool = m_pageLayoutActiveTool;
m_IsMapCtrlactive = false;
//为每一个的framework controls,设置Buddy control为PageLayoutControl this.SetBuddies(m_pageLayoutControl.Object); }
catch (Exception ex) {
throw new Exception(string.Format(\ } }
///
/// 给予一个地图, 置换PageLayoutControl和MapControl的focus map ///