ArcEngine 地图与布局的联动 收藏
ArcEngine中地图与布局的联动是一个令人头痛的问题!帮助中提供的联动类只是一个【简单】的联动类(暂且容许我这么说),用在实际的项目总是不能达到预期的效果的,虽然实现了地图与布局的联动,但是却不能将改动后的地图与布局部分分开保存下来,最后经过了两天的奋斗终于找到了可行的解决办法。
并将联动控制做成了一个控件以方便使用
以下为源代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.SystemUI;
namespace ForestManagement.Controls
{
[ToolboxBitmapAttribute(typeof(SynchronizerControl), "SynchronizerControl.bmp")]
public partial class SynchronizerControl : UserControl
{
public SynchronizerControl()
{
InitializeComponent();
}
private AxMapControl mapControl;
[Description("要绑定的MapControl"),Category("绑定项")]
public AxMapControl MapControl
{
get { return mapControl; }
set { mapControl = value; }
}
private AxPageLayoutControl pageLayoutControl;
[Description("要绑定的PagelayoutControl"),Category("绑定项")]
public AxPageLayoutControl PageLayoutControl
{
get { return pageLayoutControl; }
set { pageLayoutControl = value; }
}
//绑定控制类也就是帮助中的那个类
private ControlsSynchronizer controlsSyn;
public ControlsSynchronizer ControlsSyn
{
get { return controlsSyn; }
}
private DevComponents.DotNetBar.Bar mapActiveTool;
public DevComponents.DotNetBar.Bar MapActiveTool
{
get { return mapActiveTool; }
set { mapActiveTool = value; }
}
private DevComponents.DotNetBar.Bar pageActiveTool;
public DevComponents.DotNetBar.Bar PageActiveTool
{
get { return pageActiveTool; }
set { pageActiveTool = value; }
}
private AxToolbarControl mapActiveTool1;
public AxToolbarControl MapActiveTool1
{
get { return mapActiveTool1; }
set { mapActiveTool1 = value; }
}
private AxToolbarControl pageActiveTool1;
public AxToolbarControl PageActiveTool1
{
get { return pageActiveTool1; }
set { pageActiveTool1 = value; }
}
//绑定控制
public void BingControls()
{
IMapControl3 m_mapcontrol = (IMapControl3)mapControl.Object;
IPageLayoutControl2 m_pagelayoutControl = (IPageLayoutControl2)pageLayoutControl.Object;
controlsSyn = new ControlsSynchronizer(m_mapcontrol, m_pagelayoutControl);
controlsSyn.BindControls(true);
controlsSyn.AddFrameworkControl(mapActiveTool1);
controlsSyn.AddFrameworkControl(pageActiveTool1);
}
//绑定控制
public void BindControlsInToolBar()
{
IMapControl3 m_mapcontrol = (IMapControl3)mapControl.Object;
IPageLayoutControl2 m_pagelayoutControl = (IPageLayoutControl2)pageLayoutControl.Object;
controlsSyn = new ControlsSynchronizer(m_mapcontrol, m_pagelayoutControl);
controlsSyn.BindControls(true);
controlsSyn.AddFrameworkControl(mapActiveTool1.Object);
controlsSyn.AddFrameworkControl(pageActiveTool1.Object);
}
private void SynchronizerControl_Load(object sender, EventArgs e)
{
}
//激活地图
public void ActivateMap()
{
controlsSyn.ActivateMap();
}
//激活布局
public void ActivatePageLayout()
{
controlsSyn.ActivatePageLayout();
}
}
}
在程序中只要在设置该控件的MapControl和PageLayoutControl和MapActiveTool以及PageActiveTool,主界面加载是调用BingControls()和在地图和布局切换是调用ActivateMap()和ActivatePageLayout()
进行相应的激活,但这样也不能保证联动。在打开文件时做了一些处理:在进行地图打开时进行地图的替换操作
自己写了一个打开地图的command,源代码如下,解释的比较详尽了,构造函数中用到了上述的联动控件
源代码如下:
public override void OnClick()
{
//创建一个新的 OpenFile dialog
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Map Documents (*.mxd)|*.mxd";
dlg.Multiselect = false;
dlg.Title = "Open Map Document";
if (dlg.ShowDialog() == DialogResult.OK)
{
string docName = dlg.FileName;
IMapDocument mapDoc = new MapDocumentClass();
//判断文档是否是加密的。如果没有加密就打开并加载文档
if (mapDoc.get_IsPresent(docName) && !mapDoc.get_IsPasswordProtected(docName))
{
mapDoc.Open(docName, string.Empty);
//先加载PageLayout,并将地图赋给MapControl
m_controlsSynchronizer.PageLayoutControl.LoadMxFile(docName, null);
m_controlsSynchronizer.ReplaceMap(m_controlsSynchronizer.PageLayoutControl.ActiveView.FocusMap);
mapDoc.Close();
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ybgiser/archive/2009/02/24/3932345.aspx