×
您的位置: 首页 > 编程笔记

ArcGIS Engine+C#实例开发教程》第三讲 MapControl与PageLayoutControl同步

C# ArcGIS AR 时间:2010-12-26  查看:516   收藏
摘要:在ArcMap中,能够很方面地进行MapView和Layout View两种视图的切换,而且二者之间的数据是同步显示的。关于两种视图同步的实现方法有多种,可以使用ObjectCopy对象进行数据硬拷贝,而比较简单的方法莫过于二者共享一份地图了,这也是最常用的方法。

教程目录:

第一讲 桌面GIS应用程序框架的建立

第二讲 菜单的添加及其实现

第三讲 MapControl与PageLayoutControl同步

第四讲 状态栏信息的添加与实现

第五讲 鹰眼的实现

第六讲 右键菜单添加与实现

教程Bug及优化方案1

第七讲 图层符号选择器的实现1

第七讲 图层符号选择器的实现2

第八讲 属性数据表的查询显示

------------------------------------------------------------------

在ArcMap中,能够很方面地进行MapView和Layout View两种视图的切换,而且二者之间的数据是同步显示的。
关于两种视图同步的实现方法有多种,可以使用ObjectCopy对象进行数据硬拷贝,而比较简单的方法莫过于二者共享一份地图了,这也是最常用的方法。


1、新建同步类ControlsSynchronizer


在解决方案面板中右击项目名,选择“添加|类”,在类别中选择“Visual C#项目项”,在模板中选择“类”,输入类名“ControlsSynchronizer.cs”,将以下代码覆盖自动生成的代码:

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 "MapControl";
else
return "PageLayoutControl";
}
}

///


/// 取得当前活动的Control
///

public object ActiveControl
{
get
{
if (m_mapControl == null || m_pageLayoutControl == null)
throw new Exception("ControlsSynchronizer::ActiveControl: Either MapControl or 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("ControlsSynchronizer::ActivateMap: Either MapControl or 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;

m_IsMapCtrlactive = true;

//为每一个的framework controls,设置Buddy control为MapControl
this.SetBuddies(m_mapControl.Object);
}
catch (Exception ex)
{
throw new Exception(string.Format("ControlsSynchronizer::ActivateMap: {0}", ex.Message));
}
}

///


/// 激活PagleLayoutControl并减活MapCotrol
///

public void ActivatePageLayout()
{
try
{
if (m_pageLayoutControl == null || m_mapControl == null)
throw new Exception("ControlsSynchronizer::ActivatePageLayout: Either 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("ControlsSynchronizer::ActivatePageLayout: {0}", ex.Message));
}
}

///


/// 给予一个地图, 置换PageLayoutControl和MapControl的focus map
///

///
public void ReplaceMap(IMap newMap)
{
if (newMap == null)
throw new Exception("ControlsSynchronizer::ReplaceMap: New map for replacement is not initialized!");

if (m_pageLayoutControl == null || m_mapControl == null)
throw new Exception("ControlsSynchronizer::ReplaceMap: Either MapControl or PageLayoutControl are not initialized!");

//create a new instance of IMaps collection which is needed by the PageLayout
//创建一个PageLayout需要用到的,新的IMaps collection的实例
IMaps maps = new Maps();
//add the new map to the Maps collection
//把新的地图加到Maps collection里头去
maps.Add(newMap);

bool bIsMapActive = m_IsMapCtrlactive;

//call replace map on the PageLayout in order to replace the focus map
//we must call ActivatePageLayout, since it is the control we call ReplaceMaps
//调用PageLayout的replace map来置换focus map
//我们必须调用ActivatePageLayout,因为它是那个我们可以调用"ReplaceMaps"的Control
this.ActivatePageLayout();
m_pageLayoutControl.PageLayout.ReplaceMaps(maps);

//assign the new map to the MapControl
//把新的地图赋给MapControl
m_mapControl.Map = newMap;

//reset the active tools
//重设active tools
m_pageLayoutActiveTool = null;
m_mapActiveTool = null;

//make sure that the last active control is activated
//确认之前活动的control被激活
if (bIsMapActive)
{
this.ActivateMap();
m_mapControl.ActiveView.Refresh();
}
else
{
this.ActivatePageLayout();
m_pageLayoutControl.ActiveView.Refresh();
}
}

///


/// bind the MapControl and PageLayoutControl together by assigning a new joint focus map
/// 指定共同的Map来把MapControl和PageLayoutControl绑在一起
///

///
///
/// true if the MapControl supposed to be activated first,如果MapControl被首先激活,则为true
public void BindControls(IMapControl3 mapControl, IPageLayoutControl2 pageLayoutControl, bool activateMapFirst)
{
if (mapControl == null || pageLayoutControl == null)
throw new Exception("ControlsSynchronizer::BindControls: Either MapControl or PageLayoutControl are not initialized!");

m_mapControl = MapControl;
m_pageLayoutControl = pageLayoutControl;

this.BindControls(activateMapFirst);
}

///


/// bind the MapControl and PageLayoutControl together by assigning a new joint focus map
/// 指定共同的Map来把MapControl和PageLayoutControl绑在一起
///

/// true if the MapControl supposed to be activated first,如果MapControl被首先激活,则为true
public void BindControls(bool activateMapFirst)
{
if (m_pageLayoutControl == null || m_mapControl == null)
throw new Exception("ControlsSynchronizer::BindControls: Either MapControl or PageLayoutControl are not initialized!");

//create a new instance of IMap
//创造IMap的一个实例
IMap newMap = new MapClass();
newMap.Name = "Map";

//create a new instance of IMaps collection which is needed by the PageLayout
//创造一个新的IMaps collection的实例,这是PageLayout所需要的
IMaps maps = new Maps();
//add the new Map instance to the Maps collection
//把新的Map实例赋给Maps collection
maps.Add(newMap);

//call replace map on the PageLayout in order to replace the focus map
//调用PageLayout的replace map来置换focus map
m_pageLayoutControl.PageLayout.ReplaceMaps(maps);
//assign the new map to the MapControl
//把新的map赋给MapControl
m_mapControl.Map = newMap;

//reset the active tools
//重设active tools
m_pageLayoutActiveTool = null;
m_mapActiveTool = null;

//make sure that the last active control is activated
//确定最后活动的control被激活
if (activateMapFirst)
this.ActivateMap();
else
this.ActivatePageLayout();
}

///


///by passing the applications toolbars and TOC to the synchronization class, it saves you the
///management of the buddy control each time the active control changes. This method ads the framework
///control to an array; once the active control changes, the class iterates through the array and
///calles SetBuddyControl on each of the stored framework control.
///

///
public void AddFrameworkControl(object control)
{
if (control == null)
throw new Exception("ControlsSynchronizer::AddFrameworkControl: Added control is not initialized!");

m_frameworkControls.Add(control);
}

///


/// Remove a framework control from the managed list of controls
///

///
public void RemoveFrameworkControl(object control)
{
if (control == null)
throw new Exception("ControlsSynchronizer::RemoveFrameworkControl: Control to be removed is not initialized!");

m_frameworkControls.Remove(control);
}

///


/// Remove a framework control from the managed list of controls by specifying its index in the list
///

///
public void RemoveFrameworkControlAt(int index)
{
if (m_frameworkControls.Count throw new Exception("ControlsSynchronizer::RemoveFrameworkControlAt: Index is out of range!");

m_frameworkControls.RemoveAt(index);
}

///


/// when the active control changes, the class iterates through the array of the framework controls
/// and calles SetBuddyControl on each of the controls.
///

/// the active control
private void SetBuddies(object buddy)
{
try
{
if (buddy == null)
throw new Exception("ControlsSynchronizer::SetBuddies: Target Buddy Control is not initialized!");

foreach (object obj in m_frameworkControls)
{
if (obj is IToolbarControl)
{
((IToolbarControl)obj).SetBuddyControl(buddy);
}
else if (obj is ITOCControl)
{
((ITOCControl)obj).SetBuddyControl(buddy);
}
}
}
catch (Exception ex)
{
throw new Exception(string.Format("ControlsSynchronizer::SetBuddies: {0}", ex.Message));
}
}
#endregion
}
}

[1] [2] [3] 下一页

 

0% (0)
0% (0)