ArcEngine - 地图和布局同步
1,定义同步的类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Engine.App_Code {
public class ControlsSynchronizer {
#region private field.
private ESRI.ArcGIS.Controls.IMapControl3 mapCtrl = null;
private ESRI.ArcGIS.Controls.IPageLayoutControl2 pageLayoutCtrl = null; private ESRI.ArcGIS.SystemUI.ITool pageLayoutActiveTool = null;
private ESRI.ArcGIS.SystemUI.ITool mapActiveTool = null; private bool isMapCtrlActive = true; //默认情况MapControl为活动状态. private System.Collections.ArrayList FrameworkControls = null; //存储Toolbar,Toc等.
#endregion #region Property.
public ESRI.ArcGIS.Controls.IMapControl3 MapControl {
get {
return mapCtrl;
} set {
mapCtrl = value;
}
} public ESRI.ArcGIS.Controls.IPageLayoutControl2 PageLayoutControl {
get {
return pageLayoutCtrl;
} set {
pageLayoutCtrl = value;
}
} public string ActiveViewType {
get {
if (isMapCtrlActive)
return "MapControl";
else
return "PageLayoutControl";
}
} public object ActiveControl {
get {
if (mapCtrl == null || pageLayoutCtrl == null)
throw new ArgumentNullException("ControlsSynchronizer::ActiveControl:\r\nEither MapControl or PageLayoutControl are not initialized!");
if (isMapCtrlActive)
return mapCtrl.Object;
else
return pageLayoutCtrl.Object;
}
}
#endregion #region constructor
public ControlsSynchronizer(ESRI.ArcGIS.Controls.IMapControl3 mapCtrl, ESRI.ArcGIS.Controls.IPageLayoutControl2 pageLayoutCtrl)
: this() {
this.mapCtrl = mapCtrl;
this.pageLayoutCtrl = pageLayoutCtrl;
} public ControlsSynchronizer() {
FrameworkControls = new System.Collections.ArrayList();
}
#endregion #region method.
public void ActivateMap() {
try {
if (mapCtrl == null || pageLayoutCtrl == null)
throw new ArgumentNullException("ControlsSynchronizer::ActivateMap:\r\nEither MapControl or PageLayoutControl are not initialized!");
//cache the current tool of the PageLayoutControl.
if (pageLayoutCtrl.CurrentTool != null)
pageLayoutActiveTool = pageLayoutCtrl.CurrentTool;
//Deactivate PageLayoutControl.
pageLayoutCtrl.ActiveView.Deactivate(); //Activate MapControl.
mapCtrl.ActiveView.Activate(mapCtrl.hWnd);
if (mapActiveTool != null)
mapCtrl.CurrentTool = mapActiveTool; isMapCtrlActive = true;
//Set buddy control.
SetBuddies(mapCtrl.Object);
}
catch (System.Exception ex) {
throw new ArgumentException("ControlsSynchronizer::ActivateMap:" + ex.Message);
}
} public void ActivePageLayout() {
try {
if (mapCtrl == null || pageLayoutCtrl == null)
throw new ArgumentNullException("ControlsSynchronizer::ActivateMap:\r\nEither MapControl or PageLayoutControl are not initialized!");
//cache the current tool of the MapControl.
if (mapCtrl.CurrentTool != null)
pageLayoutActiveTool = mapCtrl.CurrentTool; mapCtrl.ActiveView.Deactivate(); pageLayoutCtrl.ActiveView.Activate(pageLayoutCtrl.hWnd); //assign the last active tool that has been used on the PageLayoutControl back as the active tool.
if (pageLayoutActiveTool != null)
pageLayoutCtrl.CurrentTool = pageLayoutActiveTool; isMapCtrlActive = false;
SetBuddies(pageLayoutCtrl.Object);
}
catch (System.Exception ex) {
throw new ArgumentException("ControlsSynchronizer::ActivePageLayout:" + ex.Message);
}
} public void ReplaceMap(ESRI.ArcGIS.Carto.IMap map) {
if (map == null)
throw new ArgumentNullException("ControlsSynchronizer::ReplaceMap:\r\nNew map for replacement is not initialized!");
if (mapCtrl == null || pageLayoutCtrl == null)
throw new ArgumentNullException("ControlsSynchronizer::ReplaceMap:\r\nEither MapControl or PageLayoutControl are not initialized!");
ESRI.ArcGIS.Carto.IMaps maps = new Maps();
maps.Add(map); //在激活PageLayout而替换PageLayout地图前,先记录当前活动的控件(是MapControl还是PageLayoutControl).
bool m_isMapCtrlActive = 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'
ActivePageLayout();
pageLayoutCtrl.PageLayout.ReplaceMaps(maps); //assign the new map to the MapControl.
mapCtrl.Map = map; //reset the active tools.
mapActiveTool = null;
pageLayoutActiveTool = null; //make sure that the last active control is activated.
if (m_isMapCtrlActive) {
ActivateMap();
mapCtrl.ActiveView.Refresh();
}
else {
ActivePageLayout();
pageLayoutCtrl.ActiveView.Refresh();
}
} /// <summary>
/// 将Toc,Toolbar添加到该类中.
/// </summary>
/// <param name="control"></param>
public void AddFrameworkControl(object control) {
if (control == null)
throw new ArgumentNullException("ControlsSynchronizer::AddFrameworkControl:\r\nAdded control is not initialized!");
FrameworkControls.Add(control);
} /// <summary>
/// 将Toc,Toolbar移出该类.
/// </summary>
/// <param name="control"></param>
public void RemoveFrameworkControl(object control) {
if (control == null)
throw new ArgumentNullException("ControlsSynchronizer::RemoveFrameworkControl:\r\nAdded control is not initialized!");
FrameworkControls.Remove(control);
} public void RemoveFrameworkControlAt(int index) {
if (index < || index >= FrameworkControls.Count)
throw new ArgumentOutOfRangeException("ControlsSynchronizer::RemoveFrameworkControlAt:index is out of range");
FrameworkControls.RemoveAt(index);
} public void BindControls(bool activeMapFirst) {
if (mapCtrl == null || pageLayoutCtrl == null)
throw new ArgumentNullException("ControlsSynchronizer::BindControls:\r\nEither MapControl or PageLayoutControl are not initialized!");
ESRI.ArcGIS.Carto.IMap map = new ESRI.ArcGIS.Carto.MapClass();
map.Name = "Map"; ESRI.ArcGIS.Carto.IMaps maps = new Maps();
maps.Add(map); pageLayoutCtrl.PageLayout.ReplaceMaps(maps);
mapCtrl.Map = map; mapActiveTool = null;
pageLayoutActiveTool = null; if (activeMapFirst)
ActivateMap();
else
ActivePageLayout();
} public void BindControls(ESRI.ArcGIS.Controls.IMapControl3 mapCtrl, ESRI.ArcGIS.Controls.IPageLayoutControl2 pageLayoutCtrl, bool activeMapFirst) {
this.mapCtrl = mapCtrl;
this.pageLayoutCtrl = pageLayoutCtrl;
BindControls(activeMapFirst);
} /// <summary>
/// 分别设置TOC,Toolbar的Buddy控件.
/// </summary>
/// <param name="buddy"></param>
private void SetBuddies(object buddy) {
try {
if (buddy == null)
throw new ArgumentNullException("ControlsSynchronizer::SetBuddies:\r\nTarget Buddy Control is not initialized!");
foreach (object ctrl in FrameworkControls) {
if (ctrl is ESRI.ArcGIS.Controls.IToolbarControl)
((ESRI.ArcGIS.Controls.IToolbarControl)ctrl).SetBuddyControl(buddy);
else if (ctrl is ESRI.ArcGIS.Controls.ITOCControl)
((ESRI.ArcGIS.Controls.ITOCControl)ctrl).SetBuddyControl(buddy);
}
}
catch (System.Exception ex) {
throw new ArgumentException("ControlsSynchronizer::SetBuddies:" + ex.Message);
}
}
#endregion
}
}
2,定义辅助类,用于存储地图:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Engine.App_Code {
/// <summary>
/// Implementation of interface IMaps which is eventually a collection of Maps.
/// </summary>
class Maps : ESRI.ArcGIS.Carto.IMaps, IDisposable {
#region private member.
//class member - using internally an ArrayList to manage the Maps collection.
private System.Collections.ArrayList arr_maps = null;
#endregion #region class constructor.
public Maps() {
arr_maps = new System.Collections.ArrayList();
}
#endregion #region dispose member.
/// <summary>
/// Dispose the Maps collection.
/// </summary>
public void Dispose() {
if (arr_maps != null) {
arr_maps.Clear();
arr_maps = null;
}
}
#endregion #region IMap member.
/// <summary>
/// Add the given map the the Map collection.
/// </summary>
/// <param name="Map"></param>
public void Add(ESRI.ArcGIS.Carto.IMap Map) {
if (Map == null)
throw new Exception("Maps::Add:\r\nNew Map is mot initialized!");
arr_maps.Add(Map);
} /// <summary>
/// Get the number of Maps in the collection.
/// </summary>
public int Count {
get {
return arr_maps.Count;
}
} /// <summary>
/// Create a new Map, add it to the collection and return it to the caller.
/// </summary>
/// <returns></returns>
public ESRI.ArcGIS.Carto.IMap Create() {
ESRI.ArcGIS.Carto.IMap mapNew = new ESRI.ArcGIS.Carto.MapClass();
arr_maps.Add(mapNew);
return mapNew;
} /// <summary>
/// Remove the instance of the given Map.
/// </summary>
/// <param name="Map"></param>
public void Remove(ESRI.ArcGIS.Carto.IMap Map) {
arr_maps.Remove(Map);
} /// <summary>
/// Remove the Map at the given index.
/// </summary>
/// <param name="Index"></param>
public void RemoveAt(int Index) {
if (Index < || Index >= arr_maps.Count)
throw new ArgumentOutOfRangeException("Maps::get_Item:Index is out of range!");
arr_maps.RemoveAt(Index);
} /// <summary>
/// Reset the Maps array.
/// </summary>
public void Reset() {
arr_maps.Clear();
} /// <summary>
/// Return the Map at the given index.
/// </summary>
/// <param name="Index"></param>
/// <returns></returns>
public ESRI.ArcGIS.Carto.IMap get_Item(int Index) {
if (Index < || Index >= arr_maps.Count)
throw new ArgumentOutOfRangeException("Maps::get_Item:Index is out of range!");
return (ESRI.ArcGIS.Carto.IMap)arr_maps[Index];
}
#endregion
}
}
3,添加实现BaseCommand的类,用于打开地图:
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls; namespace Engine.App_Code {
/// <summary>
/// Summary description for OpenMxdCommand.
/// </summary>
[Guid("ce02d34f-135a-42f5-9955-80402160acf9")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("Engine.App_Code.OpenMxdCommand")]
public sealed class OpenMxdCommand : BaseCommand {
public string DocumentFileName {
get;
private set;
} #region COM Registration Function(s)
[ComRegisterFunction()]
[ComVisible(false)]
static void RegisterFunction(Type registerType) {
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryRegistration(registerType); //
// TODO: Add any COM registration code here
//
} [ComUnregisterFunction()]
[ComVisible(false)]
static void UnregisterFunction(Type registerType) {
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryUnregistration(registerType); //
// TODO: Add any COM unregistration code here
//
} #region ArcGIS Component Category Registrar generated code
/// <summary>
/// Required method for ArcGIS Component Category registration -
/// Do not modify the contents of this method with the code editor.
/// </summary>
private static void ArcGISCategoryRegistration(Type registerType) {
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
ControlsCommands.Register(regKey); }
/// <summary>
/// Required method for ArcGIS Component Category unregistration -
/// Do not modify the contents of this method with the code editor.
/// </summary>
private static void ArcGISCategoryUnregistration(Type registerType) {
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
ControlsCommands.Unregister(regKey); } #endregion
#endregion private IHookHelper m_hookHelper;
private IMapControl3 mapCtrl = null; private ControlsSynchronizer controlsSynchronizer; public OpenMxdCommand() {
base.m_category = "打开地图文档"; //localizable text
base.m_caption = "打开地图文档"; //localizable text
base.m_message = "打开地图文档"; //localizable text
base.m_toolTip = "打开地图文档"; //localizable text
base.m_name = "打开地图文档"; //unique id, non-localizable (e.g. "MyCategory_MyCommand") try {
string bitmapResourceName = GetType().Name + ".bmp";
base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
}
catch (Exception ex) {
System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
}
} public OpenMxdCommand(ControlsSynchronizer controlsSynchronizer)
: this() {
this.controlsSynchronizer = controlsSynchronizer;
} #region Overridden Class Methods /// <summary>
/// Occurs when this command is created
/// </summary>
/// <param name="hook">Instance of the application</param>
public override void OnCreate(object hook) {
if (hook == null)
return; if (m_hookHelper == null)
m_hookHelper = new HookHelperClass(); m_hookHelper.Hook = hook; if (hook is IToolbarControl) {
IToolbarControl tbarCtrl = (IToolbarControl)hook;
mapCtrl = (IMapControl3)tbarCtrl.Buddy;
}
else if (hook is IMapControl3)
mapCtrl = (IMapControl3)hook;
} /// <summary>
/// Occurs when this command is clicked
/// </summary>
public override void OnClick() {
System.Windows.Forms.OpenFileDialog openMxdDlg = new System.Windows.Forms.OpenFileDialog();
openMxdDlg.Filter = "Map Document(*.mxd)|*.mxd";
openMxdDlg.Multiselect = false;
openMxdDlg.Title = "Open map document";
if (openMxdDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
string fileName = openMxdDlg.FileName;
ESRI.ArcGIS.Carto.IMapDocument mDoc = new ESRI.ArcGIS.Carto.MapDocumentClass();
//权限验证.
if (mDoc.get_IsPresent(fileName) && !mDoc.get_IsPasswordProtected(fileName)) {
//mapCtrl.LoadMxFile(fileName);
//mapCtrl.Extent = mapCtrl.FullExtent; //全幅.
//mapCtrl.ActiveView.Refresh(); mDoc.Open(fileName);
ESRI.ArcGIS.Carto.IMap map = mDoc.get_Map();
mDoc.SetActiveView((ESRI.ArcGIS.Carto.IActiveView)map);
controlsSynchronizer.PageLayoutControl.PageLayout = mDoc.PageLayout;
controlsSynchronizer.ReplaceMap(map);
DocumentFileName = fileName;
mDoc.Close(); //?.
}
}
} #endregion
}
}
4,在tabControl的 SelectedIndexChanged事件添加响应方法:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) {
if (tabControl1.SelectedIndex == )
synchronizer.ActivateMap();
else
synchronizer.ActivePageLayout();
}
ArcEngine - 地图和布局同步的更多相关文章
- arcgis js之地图分屏同步
arcgis js之地图分屏同步 原理: 新建两个map两个view或者一个map两个view.对地图进行移动事件绑定,在地图移动时同步地图方位 代码: views.forEach((view) =& ...
- ArcEngine地图窗口指定区域导出指定DPI多格式---delphi/C#实现
delphi/C#实现,其他语言稍微改下就行了.AE的编码各个语言都差不多,这里也没用到某一语言的特性. 函数特点: 1.可以精确导出指定范围的图形要素 2.支持多格式.TIF, .EMF,.GIF, ...
- ArcEngine 地图导航 查找路径 经纬度坐标导航 最优路径分析
本文来自CSDN博客.转载请标明出处 http//blog.csdn.net/zdb330906531 需求:依据经纬度坐标.取得两个起点与终点,显示最优路径实现导航. 參考官方样例后.我在arcMa ...
- ArcEngine中的缩放地图
在ArcEngine地图操作中,缩放地图的功能经常用到,这里做一个小结. 缩放地图一般可分为以下几种情况: 1.缩放地图:与放大地图相对,一般是手动绘制区域或固定比例缩放,可调用命令或Expand函数 ...
- ArcGIS Engine开发之地图基本操作(3)
地图数据的加载 一.加载Shapefile数据 Shapefile文件是目前主流的一种空间数据的文件存储方式,也是不同GIS软件进行数据格式转换常用的中间格式.加载Shapefile数据的方式有两种: ...
- Arcgis, ArcEngine, Arcgis Server使用开发汇总 索引
ArcGIS系列软件license及安装: Arcgis SDE10.1 和 Arcgis server10.1的授权文件license tnt_esri.dat Arcgis8.1安装license ...
- Google Map Api 谷歌地图接口整理
一:基本知识: 1. 使用谷歌地图 API 的第一步就是要注册一个 API 密钥,需要注重一下两点: 1.假如使用 API 的页面还没有发布,只是在本地调试,可以不用密钥,随便用个字符串代替就可以了. ...
- android中百度地图定位的实现方法(仅适用于真机+WIFI联网环境)
注意:此代码的环境是:真机(系统版本为Android4.2.2)+WIFI联网(才能实现最后的运行结果):使用虚拟机调试会出现各种问题. 第一步:下载SDK和申请Key 到百度的网站http://de ...
- 布局 android
1.线性布局 LinearLayout又称作线性布局,是一种非常常用的布局.通过android:orientation属性指定了排列方向是vertical还是horizontal. 如果LinearL ...
随机推荐
- JS 函数参数
1.简单的无参函数调用 function Test1(Func) { Func(); } function Test2() { alert("我要被作为函数参数啦!"); } // ...
- unity中数据的持久化存储
unity 提供了PlayerPrefs这个类用于存储游戏数据到电脑硬盘中. 这个类有10个函数可以使用 Class Functions类函数 SetInt Sets the value of the ...
- Sql Server批量停止作业
CREATE Proc [dbo].[Proc_StopJob] as begin declare @I int declare @JobID uniqueidentifier -- 1. creat ...
- 关于t分布的证明
- EAT/IAT Hook
标 题: EAT/IAT Hook 作 者: Y4ng 时 间: 2013-08-21 链 接: http://www.cnblogs.com/Y4ng/p/EAT_IAT_HOOK.html #in ...
- tbschedule
tbschedule 此文档内部包括:1.设计目标说明2.主要概念解释3.涉及的数据对象说明4.涉及的业务接口说明5.Sleep模式和NotSleep模式的区别6.使用过程中的注意事项 1.调度器的设 ...
- pure.css 学习记录
兼容性记录: IE 8+ Latest Stable: Firefox, Chrome, Safari iOS 6-8 Android 4.x 处理兼容性 <!--[if lte IE 8]&g ...
- 当开始输入文字以及完成文字输入时,变换text field的背景以及系统自带一键删除的 叉叉
当开始输入文字以及完成文字输入时,变换text field的背景. -(BOOL) textFieldShouldBeginEditing:(UITextField *)textField{ [tex ...
- iOS 第三方 需要 引用的库
================================================== AFNetWorking 是基于 nsurlconnection 所以不需要引入库 === ...
- android应用开发之Window,View和WindowManager .
ViewManager vm = a.getWindowManager(); vm.add(view,l); window :一个抽象的窗口基类,控制顶层窗口的外观和行为.作为顶层窗口,可控制窗口背 ...