第三講 MapControl與PageLayoutControl同步
在ArcMap中,能夠很方面地進(jìn)行MapView和Layout View兩種視圖的切換,,而且二者之間的數(shù)據(jù)是同步顯示的,。
關(guān)于兩種視圖同步的實(shí)現(xiàn)方法有多種,可以使用ObjectCopy對象進(jìn)行數(shù)據(jù)硬拷貝,,而比較簡單的方法莫過于二者共享一份地圖了,,這也是最常用的方法,。
1,、新建同步類ControlsSynchronizer
在解決方案面板中右擊項(xiàng)目名,選擇“添加|類”,,在類別中選擇“Visual C#項(xiàng)目項(xiàng)”,,在模板中選擇“類”,輸入類名“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
{
/// <summary>
/// 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.
/// </summary>
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
/// <summary>
/// 默認(rèn)構(gòu)造函數(shù)
/// </summary>
public ControlsSynchronizer()
{
//初始化ArrayList
m_frameworkControls = new ArrayList();
}
/// <summary>
/// 構(gòu)造函數(shù)
/// </summary>
/// <param name="mapControl"></param>
/// <param name="pageLayoutControl"></param>
public ControlsSynchronizer(IMapControl3 mapControl, IPageLayoutControl2 pageLayoutControl)
: this()
{
//為類成員賦值
m_mapControl = mapControl;
m_pageLayoutControl = pageLayoutControl;
}
#endregion
#region properties
/// <summary>
/// 取得或設(shè)置MapControl
/// </summary>
public IMapControl3 MapControl
{
get { return m_mapControl; }
set { m_mapControl = value; }
}
/// <summary>
/// 取得或設(shè)置PageLayoutControl
/// </summary>
public IPageLayoutControl2 PageLayoutControl
{
get { return m_pageLayoutControl; }
set { m_pageLayoutControl = value; }
}
/// <summary>
/// 取得當(dāng)前ActiveView的類型
/// </summary>
public string ActiveViewType
{
get
{
if (m_IsMapCtrlactive)
return "MapControl";
else
return "PageLayoutControl";
}
}
/// <summary>
/// 取得當(dāng)前活動的Control
/// </summary>
public object ActiveControl
{
get
{
if (m_mapControl == null || m_pageLayoutControl == null)
throw new Exception("ControlsSynchronizer::ActiveControl:\r\nEither MapControl or PageLayoutControl are not initialized!");
if (m_IsMapCtrlactive)
return m_mapControl.Object;
else
return m_pageLayoutControl.Object;
}
}
#endregion
#region Methods
/// <summary>
/// 激活MapControl并解除the PagleLayoutControl
/// </summary>
public void ActivateMap()
{
try
{
if (m_pageLayoutControl == null || m_mapControl == null)
throw new Exception("ControlsSynchronizer::ActivateMap:\r\nEither MapControl or PageLayoutControl are not initialized!");
//緩存當(dāng)前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,設(shè)置Buddy control為MapControl
this.SetBuddies(m_mapControl.Object);
}
catch (Exception ex)
{
throw new Exception(string.Format("ControlsSynchronizer::ActivateMap:\r\n{0}", ex.Message));
}
}
/// <summary>
/// 激活PagleLayoutControl并減活MapCotrol
/// </summary>
public void ActivatePageLayout()
{
try
{
if (m_pageLayoutControl == null || m_mapControl == null)
throw new Exception("ControlsSynchronizer::ActivatePageLayout:\r\nEither MapControl or PageLayoutControl are not initialized!");
//緩存當(dāng)前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,設(shè)置Buddy control為PageLayoutControl
this.SetBuddies(m_pageLayoutControl.Object);
}
catch (Exception ex)
{
throw new Exception(string.Format("ControlsSynchronizer::ActivatePageLayout:\r\n{0}", ex.Message));
}
}
/// <summary>
/// 給予一個地圖, 置換PageLayoutControl和MapControl的focus map
/// </summary>
/// <param name="newMap"></param>
public void ReplaceMap(IMap newMap)
{
if (newMap == null)
throw new Exception("ControlsSynchronizer::ReplaceMap:\r\nNew map for replacement is not initialized!");
if (m_pageLayoutControl == null || m_mapControl == null)
throw new Exception("ControlsSynchronizer::ReplaceMap:\r\nEither MapControl or PageLayoutControl are not initialized!");
//create a new instance of IMaps collection which is needed by the PageLayout
//創(chuàng)建一個PageLayout需要用到的,新的IMaps collection的實(shí)例
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'
//調(diào)用PageLayout的replace map來置換focus map
//我們必須調(diào)用ActivatePageLayout,因?yàn)樗悄莻€我們可以調(diào)用"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
//重設(shè)active tools
m_pageLayoutActiveTool = null;
m_mapActiveTool = null;
//make sure that the last active control is activated
//確認(rèn)之前活動的control被激活
if (bIsMapActive)
{
this.ActivateMap();
m_mapControl.ActiveView.Refresh();
}
else
{
this.ActivatePageLayout();
m_pageLayoutControl.ActiveView.Refresh();
}
}
/// <summary>
/// bind the MapControl and PageLayoutControl together by assigning a new joint focus map
/// 指定共同的Map來把MapControl和PageLayoutControl綁在一起
/// </summary>
/// <param name="mapControl"></param>
/// <param name="pageLayoutControl"></param>
/// <param name="activateMapFirst">true if the MapControl supposed to be activated first,如果MapControl被首先激活,則為true</param>
public void BindControls(IMapControl3 mapControl, IPageLayoutControl2 pageLayoutControl, bool activateMapFirst)
{
if (mapControl == null || pageLayoutControl == null)
throw new Exception("ControlsSynchronizer::BindControls:\r\nEither MapControl or PageLayoutControl are not initialized!");
m_mapControl = MapControl;
m_pageLayoutControl = pageLayoutControl;
this.BindControls(activateMapFirst);
}
/// <summary>
/// bind the MapControl and PageLayoutControl together by assigning a new joint focus map
/// 指定共同的Map來把MapControl和PageLayoutControl綁在一起
/// </summary>
/// <param name="activateMapFirst">true if the MapControl supposed to be activated first,如果MapControl被首先激活,則為true</param>
public void BindControls(bool activateMapFirst)
{
if (m_pageLayoutControl == null || m_mapControl == null)
throw new Exception("ControlsSynchronizer::BindControls:\r\nEither MapControl or PageLayoutControl are not initialized!");
//create a new instance of IMap
//創(chuàng)造IMap的一個實(shí)例
IMap newMap = new MapClass();
newMap.Name = "Map";
//create a new instance of IMaps collection which is needed by the PageLayout
//創(chuàng)造一個新的IMaps collection的實(shí)例,這是PageLayout所需要的
IMaps maps = new Maps();
//add the new Map instance to the Maps collection
//把新的Map實(shí)例賦給Maps collection
maps.Add(newMap);
//call replace map on the PageLayout in order to replace the focus map
//調(diào)用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
//重設(shè)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();
}
/// <summary>
///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.
/// </summary>
/// <param name="control"></param>
public void AddFrameworkControl(object control)
{
if (control == null)
throw new Exception("ControlsSynchronizer::AddFrameworkControl:\r\nAdded control is not initialized!");
m_frameworkControls.Add(control);
}
/// <summary>
/// Remove a framework control from the managed list of controls
/// </summary>
/// <param name="control"></param>
public void RemoveFrameworkControl(object control)
{
if (control == null)
throw new Exception("ControlsSynchronizer::RemoveFrameworkControl:\r\nControl to be removed is not initialized!");
m_frameworkControls.Remove(control);
}
/// <summary>
/// Remove a framework control from the managed list of controls by specifying its index in the list
/// </summary>
/// <param name="index"></param>
public void RemoveFrameworkControlAt(int index)
{
if (m_frameworkControls.Count < index)
throw new Exception("ControlsSynchronizer::RemoveFrameworkControlAt:\r\nIndex is out of range!");
m_frameworkControls.RemoveAt(index);
}
/// <summary>
/// when the active control changes, the class iterates through the array of the framework controls
/// and calles SetBuddyControl on each of the controls.
/// </summary>
/// <param name="buddy">the active control</param>
private void SetBuddies(object buddy)
{
try
{
if (buddy == null)
throw new Exception("ControlsSynchronizer::SetBuddies:\r\nTarget 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:\r\n{0}", ex.Message));
}
}
#endregion
}
}