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 ...
随机推荐
- javascript字符类型操作函数
//获取字符串的长度 String.prototype.getByteLength = function() { var bytes=0,i=0; for (; i<this.length; + ...
- dede留言板BUG解决
dede留言板刷新后空白BUG解决 DEDE留言板验证码留空或者不正确返回空白页面的解决方法 解决方法如下进入文件/plus/guestbook.php 找到代码ShowMsg("验证码不正 ...
- Lua 学习笔记(二)
七.再论lua函数 1.lua中的函数被认为是带有词法定界和第一类值 a.词法定界:被嵌套的函数可以访问外部函数的变量 b.第一类值: lua中的函数可以放在变量中 (函数指针?) ...
- Android 源码编译环境搭建(64位Ubuntu)各种依赖包安装
1.准备: 普通PC(要求能上网), PC的操作系统Ubuntu 10.04 LTS(64位的),已经下载好的Android 1.6_r1的源代码. 2.Linux的依赖package安装: 为了更快 ...
- S3C2440 驱动程序开发
转载:http://www.cnblogs.com/lishixian/articles/2999923.html
- 封装JDBC事务操作,执行存储过程测试
Oracle数据库端测试环境见:http://www.cnblogs.com/yshyee/p/4392328.html package com.mw.utils; import java.sql.C ...
- BeanUtils框架浅析
一.使用步骤: 1.添加jar包: commons-beanutils-1.8.0.jar commons-logging.jar 2.使用setProperty()方法对javabean设置属性值 ...
- protel或Altium Designer中各种栅格(grid)的意义
Snap Grid:捕获栅格.指光标移动的最小间隔.Component Grid:元件放置捕获栅格.指放置元件时,元件移动的间隔.Electrical Grid:电气栅格.电气栅格的作用是在移动或放置 ...
- 硬盘安装windows7
微软已经发表声明 Windows XP 操作系统将于2014年4月8日停止提供补丁和安全更新,提醒用户尽快升级现有的XP操作系统.Windows XP曾在2001年10月25日正式发布的,已经走过了十 ...
- leetcode: sortlist之四种方法
原题链接:https://oj.leetcode.com/problems/sort-list/ 题目:空间复杂度为常数,时间复杂度为O(nlogn)的排序链表实现 方法一:第一想法是模拟数组的快速排 ...