在許多Arcgis Engine的練習代碼中,演示的MapControl 與PageLayeroutControl都是分開兩次打開地圖文件來實現。但是,如果這樣的話,PageLayoutControl與MapControl不能同步,即當改變MapControl中的內容時無法在PageLayoutControl控件裏面立即顯示出來。若想要實現兩者的同步,只要把兩個控件的共享提個地圖圖層即可。具體實現過程如下:
首先,新建一個類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
定義一些成員變量,第一個IMapControl3 接口變量是用於獲取對form1中的MapControl的控制。下面的 IPageLayoutControl2也同樣是爲了控制PageLayoutControl。第三個和第四個是爲了記錄mapControl 與PageLayeroutControl的活動狀態,ToolbarControl 關聯以上兩個圖層控件時需要此接口。第五個布爾型變量是爲了記錄當前活動的空間是否爲MapControl 。第六個爲一個ArrayList對象,用於存儲MapControl和PageLayoutControl的Object對象。
構造函數,初始化ArrayList ,並給MapControl和PageLayoutControl 賦值。其中,自定義的構造函數中調用了默認構造函數。:this();
#region constructor
/// <summary>
/// 默認構造函數
/// </summary>
public ControlsSynchronizer()
{
//初始化ArrayList
m_frameworkControls = new ArrayList();
}
/// <summary>
/// 構造函數
/// </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>
/// 取得或設置MapControl
/// </summary>
public IMapControl3 MapControl
{
get { return m_mapControl; }
set { m_mapControl = value; }
}
/// <summary>
/// 取得或設置PageLayoutControl
/// </summary>
public IPageLayoutControl2 PageLayoutControl
{
get { return m_pageLayoutControl; }
set { m_pageLayoutControl = value; }
}
/// <summary>
/// 取得當前ActiveView的類型
/// </summary>
public string ActiveViewType
{
get
{
if (m_IsMapCtrlactive)
return "MapControl";
else
return "PageLayoutControl";
}
}
/// <summary>
/// 取得當前活動的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!");
//緩存當前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:\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!");
//緩存當前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:\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
//創建一個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();
}
}
/// <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
//創造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();
}
/// <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
其中,給PageLayoutControl進行賦值時使用了一個自定義的Maps類來對IMAP接口進行初始化,Maps類的定義如下:
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
/// <summary>
/// Dispose the collection
/// </summary>
public void Dispose()
{
if (m_array != null)
{
m_array.Clear();
m_array = null;
}
}
#endregion
#region IMaps Members
/// <summary>
/// Remove the Map at the given index
/// </summary>
/// <param name="Index"></param>
public void RemoveAt(int Index)
{
if (Index > m_array.Count || Index < 0)
throw new Exception("Maps::RemoveAt:\r\nIndex is out of range!");
m_array.RemoveAt(Index);
}
/// <summary>
/// Reset the Maps array
/// </summary>
public void Reset()
{
m_array.Clear();
}
/// <summary>
/// Get the number of Maps in the collection
/// </summary>
public int Count
{
get
{
return m_array.Count;
}
}
/// <summary>
/// Return the Map at the given index
/// </summary>
/// <param name="Index"></param>
/// <returns></returns>
public IMap get_Item(int Index)
{
if (Index > m_array.Count || Index < 0)
throw new Exception("Maps::get_Item:\r\nIndex is out of range!");
return m_array[Index] as IMap;
}
/// <summary>
/// Remove the instance of the given Map
/// </summary>
/// <param name="Map"></param>
public void Remove(IMap Map)
{
m_array.Remove(Map);
}
/// <summary>
/// Create a new Map, add it to the collection and return it to the caller
/// </summary>
/// <returns></returns>
public IMap Create()
{
IMap newMap = new MapClass();
m_array.Add(newMap);
return newMap;
}
/// <summary>
/// Add the given Map to the collection
/// </summary>
/// <param name="Map"></param>
public void Add(IMap Map)
{
if (Map == null)
throw new Exception("Maps::Add:\r\nNew Map is mot initialized!");
m_array.Add(Map);
}
#endregion
}
由於綁定MapControl 與PageLayeroutControl,在打開文件的代碼函數時就需要對其進行綁定。因此需要重寫一個打開文件的函數,如下:
public override void OnClick()
{
// TODO: Add OpenNewMapDocument.OnClick implementation
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);
IMap map = mapDoc.get_Map(0);
m_controlsSynchronizer.ReplaceMap(map);
mapDoc.Close();
}
}
}
在上面函數中,打開地圖文件以後。將map傳入ReplaceMap函數中,用於初始化控件地圖。
當以上函數定義完以後,就需要在form1的Lord函數中對其初始化。代碼如下:
m_mapControl = (IMapControl3)this.axMapControl1.Object;
m_pageLayoutControl = (IPageLayoutControl2)this.axPageLayoutControl1.Object;
//初始化controls synchronization calss
m_controlsSynchronizer = new
ControlsSynchronizer(m_mapControl, m_pageLayoutControl);
//把MapControl和PageLayoutControl綁定起來(兩個都指向同一個Map),然後設置MapControl爲活動的Control
m_controlsSynchronizer.BindControls(true);
//爲了在切換MapControl和PageLayoutControl視圖同步,要添加Framework Control
m_controlsSynchronizer.AddFrameworkControl(axToolbarControl1.Object);
m_controlsSynchronizer.AddFrameworkControl(this.axTOCControl1.Object);
// 添加打開命令按鈕到工具條
OpenNewMapDocument openMapDoc = new OpenNewMapDocument(m_controlsSynchronizer);
axToolbarControl1.AddItem(openMapDoc, -1, 0, false, -1, esriCommandStyles.esriCommandStyleIconOnly);
------------------------------------------------------------------------------------end-----------------------------------------------------------