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 ...
随机推荐
- [c#]asp.net开发微信公众平台(7)前6篇的整体框架demo源码
这里给出的demo是具备整体框架的微信公众平台源码, 所谓demo就是拿过去就可以直接演示使用的东西, 当然不会具备非常详细的具体到业务层面.数据层面的东西, 每个人都可以在此基础上自由发挥, 只 ...
- JDK6和JDK7中的substring()方法
substring(int beginIndex, int endIndex)在JDK6与JDK7中的实现方式不一样,理解他们的差异有助于更好的使用它们.为了简单起见,下面所说的substring() ...
- 解决CentOS 5.8在虚拟机环境下如何桥接上网
1.虚拟机的网卡配置如下图所示: 2.在CentOS 5.8的命令行界面:输入如下指令 然后准备修改里面的网关地址和自己的IP地址 3.同时查看自己的IP地址和网关 4.在第二步里面修改,网关地址应该 ...
- Request 分别获取具有相同 name 属性表单元素值
html 中是允许多个具有相同name属性的元素的,例如 <div> <input name="txtName" id="txtFirstName&qu ...
- python学习第七天 -- dict 和set
今天主要学习关于python 的dict(全称dictionary)和set.dict的用法跟javascript 中map表类似,key + value结构语言.而set,准确来说,只是key的集合 ...
- 2016年最受欢迎中国开源软件TOP 20
开源软件对程序员来说是一个经常接触的软件,作为一个经常接触的软件,当然想知道自己用的软件受欢迎程度,基于此,开源中国在近日公布“2016年度最受欢迎中国开源软件评选”结果,在TOP20榜单中,前5名分 ...
- 注解 @Resource与@Autowired与@Component的使用
在java代码中使用@Autowired或@Resource注解方式进行装配,这两个注解的区别是:@Autowired 默认按类型装配,@Resource默认按名称装配,当找不到与名称匹配的bean才 ...
- Cmake的install与file命令的区别
实际上他们两个可以达到一个目标(对于文件操作),但是又有本质上的区别,文档没有细看,但是一般利于项目的管理,使用install,install命令如果在cmake命令中没有指名install参数,实际 ...
- Google GFS文件系统深入分析
Google GFS文件系统深入分析 现在云计算渐成潮流,对大规模数据应用.可伸缩.高容错的分布式文件系统的需求日渐增长.Google根据自身的经验打造的这套针对大量廉价客户机的Google GFS文 ...
- 【转】Service Intent must be explicit的解决方法
原文网址:http://blog.csdn.net/shenzhonglaoxu/article/details/42675287 今天在学习android的Service组件的时候,在Android ...