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 ...
随机推荐
- 如何让MyEclispe中英文切换
我们通过网上的一些汉化办法汉化了我们的MyEclipse,可是我们有时候想切回英文版怎么办? 方法一:我们可以通过修改MyEclipse配置文件的办法来从中文恢复到英文, -Duser.languag ...
- android sax解析xml 文件 动态加载标题
要解决一个问题 : 问题描述为 把标题动态的加载到 listView子布局中 我们首先通过 java程序写一个把标题写到xml文件的程序.这个程序会在以后讲解. 现在截图 已经写好的xm文件格式如下 ...
- jQuery Scroll div滚动条样式更改
<script type="text/javascript" src="js/jquery-1.2.6.min.js"></script> ...
- Android ndk第一步,构建jni headers
转载请注明出处:http://www.cnblogs.com/fpzeng/p/4281801.html 源码请见 https://github.com/fpzeng/HelloJNI PC系统: u ...
- SalutJs
SalutJs 前言 卤煮在公司之初接触到的是一个微信APP应用.前端技术采用的是Backbone+zepto等小型JS类库.在项目开发之初,这类中小型的项目采用这两种库可以满足基本的需求.然而,随着 ...
- 对于Android的线程和线程池的理解
Android的消息机制,主要是指Handler的运行机制,Handler的运行需要底层的MessageQueue 和 Looper的支撑,MessageQueue中文名消息队列,它的内部存储了一组消 ...
- 把zlog封装成模块,隐藏zlog
mylog.h #ifndef _MY_LOG_H #define _MY_LOG_H int init(char *filename); void *get_category(char * cate ...
- STL string常用操作指令
s.insert(pos,args); 在pos之前插入args指定的字符.pos可以是一个下标或一个迭代器.接受下标的版本返回一个指向s的引用;接受迭代器的版本返回指向第一个插入字符的迭代器. s. ...
- 转:从BeagleBone谈AM335x硬件系统设计
链接:http://blog.chinaunix.net/uid-730738-id-3266690.html 作者:chenzhufly 从BeagleBone谈AM335x硬件系统设计 日期 ...
- Win8/8.1/10获得完整管理员权限的方法
WIN+R,运行对话框中输入gpedit.msc,开启组策略,然后一步步地在“计算机配置”-“Windows 设置”-“安全设置”-“本地策略”-“安全选项”,找到右侧的“用户账户控制:以管理员批准模 ...