{
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(\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 < index) throw new Exception(\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. ///
///
try {
if (buddy == null)
throw new Exception(\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(\ex.Message)); } }
#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)
throw new Exception(\ m_array.RemoveAt(Index); }
///
/// Reset the Maps array ///
public void Reset() {
m_array.Clear(); }
///
/// Get the number of Maps in the collection /// public int Count
{
get {
return m_array.Count; } }
///
/// Return the Map at the given index ///
///
public IMap get_Item(int Index) {
if (Index > m_array.Count || Index < 0)
throw new Exception(\ return m_array[Index] as IMap; }
///
/// Remove the instance of the given Map ///
///
m_array.Remove(Map); }
///
/// Create a new Map, add it to the collection and return it to the caller ///
///
IMap newMap = new MapClass(); m_array.Add(newMap); return newMap; }
///
/// Add the given Map to the collection ///
///
if (Map == null)
throw new Exception(\ m_array.Add(Map); }
#endregion } }
3、新建打开文档类OpenNewMapDocument
由于从工具栏自带的打开按钮打开地图文档的时候,不会自动进行两种视图之间的同步,所以我们要自己派生一个OpenNewMapDocument类,用于打开地图文档。 右击项目名,选择“添加|类”,再选择ArcGIS类别中的BaseCommand模板,输入类名为“OpenNewMapDocument.cs”。 首先添加引用:
using System.Windows.Forms; using ESRI.ArcGIS.Carto;
//再添加如下成员变量:
private ControlsSynchronizer m_controlsSynchronizer = null; //修改默认的构造函数如下所示: //添加参数
public OpenNewMapDocument(ControlsSynchronizer controlsSynchronizer) {
//
// TODO: Define values for the public properties // //设定相关属性值
base.m_category = \ base.m_caption = \
base.m_message = \should work in ArcMap/MapControl/PageLayoutControl\//localizable text
base.m_toolTip = \
base.m_name = \ //unique id, non-localizable (e.g. \
//初始化m_controlsSynchronizer
m_controlsSynchronizer = controlsSynchronizer; try {
//
// TODO: change bitmap name if necessary //
string bitmapResourceName = GetType().Name + \
base.m_bitmap = new Bitmap(GetType(), bitmapResourceName); }
catch (Exception ex) {
System.Diagnostics.Trace.WriteLine(ex.Message, \ } }
再在OnClick函数中添加如下代码: public override void OnClick() {
// TODO: Add OpenNewMapDocument.OnClick implementation OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = \ dlg.Multiselect = false;
dlg.Title = \
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(); } } }
在添加类时,模板会自动添加一个名为“OpenNewMapDocument.bmp”的图标,你可以自己修改或者替换为打开的