///
if (newMap == null)
throw new Exception(\map for replacement is not initialized!\
if (m_pageLayoutControl == null || m_mapControl == null) throw new Exception(\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,因为它是那个我们可以调用\的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绑在一起 ///
///
///
///
public void BindControls(IMapControl3 mapControl, IPageLayoutControl2 pageLayoutControl, bool activateMapFirst) {
if (mapControl == null || pageLayoutControl == null) throw new Exception(\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绑在一起 ///
///
public void BindControls(bool activateMapFirst) {
if (m_pageLayoutControl == null || m_mapControl == null) throw new Exception(\MapControl or PageLayoutControl are not initialized!\
//create a new instance of IMap
//创造IMap的一个实例
IMap newMap = new MapClass(); newMap.Name = \
//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 application's 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(\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(\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 < index)
throw new Exception(\
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. ///
///
try {
if (buddy == null)
throw new Exception(\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(\ } }
#endregion } }
2、新建Maps类
在同步类中,要用到Maps类,用于管理地图对象。与新建同步类ControlsSynchronizer类似,我们新建一Maps类,其所有代码如下所示:
using System;
using System.Collections;
using System.Collections.Generic; using System.Text;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.Carto;
namespace _sdnMap {
[Guid(\ [ClassInterface(ClassInterfaceType.None)] [ProgId(\
public class Maps : IMaps, IDisposable {
//class member - using internally an ArrayList to manage the Maps collection private ArrayList m_array = null;
#region class constructor public Maps() {
m_array = new ArrayList(); }
#endregion
#region IDisposable Members
///
/// Dispose the collection /// public void Dispose() {
if (m_array != null) {
m_array.Clear(); m_array = null; } }
#endregion
#region IMaps Members
///
/// Remove the Map at the given index ///
///
if (Index > m_array.Count || Index < 0)