收藏 分享(赏)

ArcGIS-Engine+C#-初学者实例代码.doc

上传人:weiwoduzun 文档编号:5739075 上传时间:2019-03-15 格式:DOC 页数:67 大小:812.02KB
下载 相关 举报
ArcGIS-Engine+C#-初学者实例代码.doc_第1页
第1页 / 共67页
ArcGIS-Engine+C#-初学者实例代码.doc_第2页
第2页 / 共67页
ArcGIS-Engine+C#-初学者实例代码.doc_第3页
第3页 / 共67页
ArcGIS-Engine+C#-初学者实例代码.doc_第4页
第4页 / 共67页
ArcGIS-Engine+C#-初学者实例代码.doc_第5页
第5页 / 共67页
点击查看更多>>
资源描述

1、ArcGIS Engine+C#实例开发教程 读者对象:使用 C#开发 ArcGIS Engine(以下简称 AE)的初学者。 预备知识:了解 AE 基本体系,了解 C#基本语法,了解 VS2005 的基本使用方法。 预期学习效果:进一步理解 AE 的体系结构与开发方法,掌握基本的 GIS 桌面应用程序的开发。第一讲 桌面 GIS 应用程序框架的建立本讲主要是使用 MapControl、PageLayoutControl 、ToolbarControl、TOCControl 四个控件建立起基本的桌面 GIS 应用程序框架。最终成果预览如下:1、新建项目启动 VS2005,选择“文件| 新建|项

2、目” ,在项目类型中选择 Visual C#,再选择Windows 应用程序模板,输入名称 “3sdnMap”,点击确定。在解决方案管理器中将“Form1.cs”重命名为“3sdnMap.cs” ,在设计视图中,选中窗体,将其属性中的“Text”改为“3sdnMap” 。2、添加控件选择工具箱中的“菜单和工具栏|MenuStrip” ,将其拖入窗体。选择工具箱中的“ArcGIS Windows Forms”节,将“ToolbarControl”控件拖入窗体,并将其属性中的 Dock 设置为 Top。选择工具箱中的“菜单和工具栏|StatusStrip” ,将其拖入到窗体。选择工具箱中的“容器|

3、SplitContainer”容器拖入窗体,并将其属性中的 Dock 设置为 Fill。将 TabControl 控件拖入 Panel1,将 Alignment 属性设置为 Bottom,Dock 属性设置为 Fill。点击 TabPages 属性右边的按钮,弹出 TabPage 集合编辑器,将 tabPage1 的Name 设置为 tabPageLayer, Text 设置为图层,将 tabPage2 的 Name 设置为tabPageProperty,Text 设置为属性。如下所示。选择“图层”选项卡,拖入 TOCControl 控件,设置 Dock 属性为 Fill。选择“属性”选项卡,

4、拖入 DataGridView 控件,设置 Dock 属性为 Fill。拖入 TabControl 控件到 Panel2,设置 Dock 属性为 Fill。并上述类似的方法,将两个选项卡的 Name 和 Text 分别设置为:(tabPageMap、地图) , (tabPageLayout,制版)。选择“地图”选项卡,拖入 MapControl 控件,设置 Dock 属性为 Fill。选择“制版”选项卡,拖入 PageLayoutControl 控件,设置 Dock 属性为 Fill。最后将 LicenseControl 控件拖入到窗体的任意地方。按 F5 编译运行,可以看到刚才布局好的程序界

5、面了。3、控件绑定通过以上步骤添加的控件还只是单独存在,而我们的程序需要各控件间协同工作,因此要进行控件绑定。分别右击 ToolbarControl、TOCControl 控件,将 Buddy 设置为 axMapControl1,如下图所示。这样,工具条和图层控件就与地图控件关联了。4、添加工具此时,工具条中还没有任何工具,添加的方法也很简单。右击 ToolbarControl,选择“属性|Items” ,点击 Add,选择 Commands 选项卡中的 Generic,双击Open、SaveAs、Redo、Undo 即可将相应工具添加到工具条。常见的工具有:Map Navigation 中的

6、导航工具,Map Inquiry 中的查询工具,Feature Selection中的选择工具,你可以根据需要酌情添加工具。5、编译运行按 F5 即可编译运行程序,至此桌面 GIS 应用程序框架基本框架已经搭建好了,可以通过工具条的工具打开地图文档,浏览地图了,效果如开篇所示。第二讲 菜单的添加及其实现在上一讲中,我们实现了应用程序基本框架,其中有个小错误,在此先跟大家说明下。在“属性”选项卡中,我们当时添加的是 DataGridView 控件,这个控件是用来显示数据表的,而专门用于属性的查询和设置的控件是 PropertyGrid 控件。因此请你删除“属性”选项卡中的 DataGridVie

7、w 控件,再把位于“工具箱| 所有 Windows 窗体|PropertyGrid” (如果没有,右击选择“选择项”以添加此控件)控件拖到该选项卡。在这一讲中,主要讲解菜单的添加和实现。1、添加菜单在设计视图中,单击菜单栏,会出现“请在此处键入”的提示,单击提示就可以键入菜单名称,如“文件” ,再单击“文件” ,即可输入其下拉子菜单,如下所示:Tips:每创建一个菜单,请在其属性面板中设置 Name 属性,而且不要为中文,因此 Name 值将是此菜单响应函数的函数名的一部分,带中文的函数名,总是不好吧。本讲中,我们将添加新建(New) 、打开(Open) 、添加数据(AddData) 、保存(

8、Save) 、另存为(SaveAs) 、退出(Exit)这些菜单, ()内为相应的 Name 属性值。Tips:你可以在属性面板中的 Text 属性中,把菜单名设置为中英文形式,如“打开 Open”,带下划线的 O 表示此项菜单的快捷键是字母 O,设置方法是在相应字母前加上“private ESRI.ArcGIS.Controls.IPageLayoutControl2 m_pageLayoutControl = null;private IMapDocument pMapDocument;若以上指针无效,请添加以下引用:using ESRI.ArcGIS.Carto;using ESRI.A

9、rcGIS.Controls;using ESRI.ArcGIS.esriSystem;using ESRI.ArcGIS.Display;using ESRI.ArcGIS.Geometry;using ESRI.ArcGIS.SystemUI;在设计视图中的属性面板中,选择 Form1,即主窗体,单击事件按钮(闪电形状的那个按钮) ,打到“Load ”事件并双击,添加此事件。在 Form1_Load 函数中初始化这些指针:/取得 MapControl 和 PageLayoutControl 的引用m_mapControl = (IMapControl3)this.axMapControl1

10、.Object;m_pageLayoutControl = (IPageLayoutControl2)this.axPageLayoutControl1.Object;依次双击每个菜单项,添加菜单响应函数。实现代码如下:/ / 新建地图命令/ / / private void New_Click(object sender, EventArgs e)/本命令涉及到 MapControl 和 PageLayoutControl 同步问题,将在下一讲中实现/ / 打开地图文档 Mxd 命令/ private void Open_Click(object sender, EventArgs e)/本

11、命令涉及到 MapControl 和 PageLayoutControl 同步问题,将在下一讲中实现/ / 添加数据命令/ private void AddData_Click(object sender, EventArgs e)int currentLayerCount = this.axMapControl1.LayerCount;ICommand pCommand = new ControlsAddDataCommandClass();pCommand.OnCreate(this.axMapControl1.Object);pCommand.OnClick(); / / 保存地图文档命

12、令/ private void Save_Click(object sender, EventArgs e)/首先确认当前地图文档是否有效if (null != m_pageLayoutControl.DocumentFilename /打开当前地图文档mapDoc.Open(m_pageLayoutControl.DocumentFilename, string.Empty);/用 PageLayout 中的文档替换当前文档中的 PageLayout 部分mapDoc.ReplaceContents(IMxdContents)m_pageLayoutControl.PageLayout);/

13、保存地图文档mapDoc.Save(mapDoc.UsesRelativePaths, false);mapDoc.Close();/ / 另存为地图文档命令/ private void SaveAs_Click(object sender, EventArgs e)/调用另存为命令ICommand command = new ControlsSaveAsDocCommandClass();command.OnCreate(m_controlsSynchronizer.ActiveControl);command.OnClick();/ / 退出程序/ private void Exit_Cl

14、ick(object sender, EventArgs e)Application.Exit();3、编译运行按 F5 编译运行程序。也许你会发现,菜单命令的实现方式都是类型的。没错,在AE9.2 中,内置了许多常用的 Command 和 Tool,如ControlsAddDataCommandClass、ControlsMapZoomInToolClass、ControlsMapPanToolClass 等等,这些内置对象在 ESRI.ArcGIS.Controls 命名空间中,你可以对象浏览器中查看。而且这些内置对象的调用方式都类似,如下所示:/定义ICommand command =

15、new ControlsSaveAsDocCommandClass();/创建command.OnCreate(m_controlsSynchronizer.ActiveControl);/调用command.OnClick();希望你可以举一反三,去实现更多的你想要的功能。第三讲 MapControl 与 PageLayoutControl 同步在 ArcMap 中,能够很方面地进行 MapView 和 Layout View 两种视图的切换,而且二者之间的数据是同步显示的。关于两种视图同步的实现方法有多种,可以使用 ObjectCopy 对象进行数据硬拷贝,而比较简单的方法莫过于二者共享一

16、份地图了,这也是最常用的方法。1、新建同步类 ControlsSynchronizer在解决方案面板中右击项目名,选择“添加|类” ,在类别中选择“Visual C#项目项” ,在模板中选择“类” ,输入类名“ControlsSynchronizer.cs ”,将以下代码覆盖自动生成的代码:using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.IO;using System.Runtime.Int

17、eropServices;using ESRI.ArcGIS.esriSystem;using ESRI.ArcGIS.Carto;using ESRI.ArcGIS.Controls;using ESRI.ArcGIS.SystemUI;namespace _sdnMap/ / This class is used to synchronize a gven PageLayoutControl and a MapControl./ When initialized, the user must pass the reference of these control to the class,

18、 bind/ the control together by calling BindControls which in turn sets a joined Map referenced/ by both control; and set all the buddy controls joined between these two controls./ When alternating between the MapControl and PageLayoutControl, you should activate the visible control / and deactivate

19、the other by calling ActivateXXX./ This calss is limited to a situation where the controls are not simultaneously visible. / public class ControlsSynchronizer#region class membersprivate IMapControl3 m_mapControl = null;private IPageLayoutControl2 m_pageLayoutControl = null;private ITool m_mapActive

20、Tool = null;private ITool m_pageLayoutActiveTool = null;private bool m_IsMapCtrlactive = true;private ArrayList m_frameworkControls = null;#endregion#region constructor/ / 默认构造函数/ public ControlsSynchronizer()/初始化 ArrayListm_frameworkControls = new ArrayList();/ / 构造函数/ public ControlsSynchronizer(I

21、MapControl3 mapControl, IPageLayoutControl2 pageLayoutControl): this()/为类成员赋值m_mapControl = mapControl;m_pageLayoutControl = pageLayoutControl;#endregion#region properties/ / 取得或设置 MapControl/ public IMapControl3 MapControlget return m_mapControl; set m_mapControl = value; / / 取得或设置 PageLayoutContro

22、l/ public IPageLayoutControl2 PageLayoutControlget return m_pageLayoutControl; set m_pageLayoutControl = value; / / 取得当前 ActiveView 的类型/ public string ActiveViewTypegetif (m_IsMapCtrlactive)return “MapControl“;elsereturn “PageLayoutControl“;/ / 取得当前活动的 Control/ public object ActiveControlgetif (m_ma

23、pControl = null | m_pageLayoutControl = null)throw new Exception(“ControlsSynchronizer:ActiveControl:rnEither MapControl or PageLayoutControl are not initialized!“);if (m_IsMapCtrlactive)return m_mapControl.Object;elsereturn m_pageLayoutControl.Object;#endregion#region Methods/ / 激活 MapControl 并解除 t

24、he PagleLayoutControl/ public void ActivateMap()tryif (m_pageLayoutControl = null | m_mapControl = null)throw new Exception(“ControlsSynchronizer:ActivateMap:rnEither MapControl or PageLayoutControl are not initialized!“);/缓存当前 PageLayout 的 CurrentToolif (m_pageLayoutControl.CurrentTool != null) m_p

25、ageLayoutActiveTool = m_pageLayoutControl.CurrentTool;/解除 PagleLayoutm_pageLayoutControl.ActiveView.Deactivate();/激活 MapControlm_mapControl.ActiveView.Activate(m_mapControl.hWnd);/将之前 MapControl 最后使用的 tool,作为活动的 tool,赋给MapControl 的 CurrentToolif (m_mapActiveTool != null) m_mapControl.CurrentTool = m

26、_mapActiveTool;m_IsMapCtrlactive = true;/为每一个的 framework controls,设置 Buddy control 为 MapControlthis.SetBuddies(m_mapControl.Object);catch (Exception ex)throw new Exception(string.Format(“ControlsSynchronizer:ActivateMap:rn0“, ex.Message);/ / 激活 PagleLayoutControl 并减活 MapCotrol/ public void ActivateP

27、ageLayout()tryif (m_pageLayoutControl = null | m_mapControl = null)throw new Exception(“ControlsSynchronizer:ActivatePageLayout:rnEither MapControl or PageLayoutControl are not initialized!“);/缓存当前 MapControl 的 CurrentToolif (m_mapControl.CurrentTool != null) m_mapActiveTool = m_mapControl.CurrentTo

28、ol;/解除 MapControlm_mapControl.ActiveView.Deactivate();/激活 PageLayoutControlm_pageLayoutControl.ActiveView.Activate(m_pageLayoutControl.hWnd);/将之前 PageLayoutControl 最后使用的 tool,作为活动的 tool,赋给PageLayoutControl 的 CurrentToolif (m_pageLayoutActiveTool != null) m_pageLayoutControl.CurrentTool = m_pageLayou

29、tActiveTool;m_IsMapCtrlactive = false;/为每一个的 framework controls,设置 Buddy control 为PageLayoutControlthis.SetBuddies(m_pageLayoutControl.Object);catch (Exception ex)throw new Exception(string.Format(“ControlsSynchronizer:ActivatePageLayout:rn0“, ex.Message);/ / 给予一个地图, 置换 PageLayoutControl 和 MapContro

30、l 的 focus map/ public void ReplaceMap(IMap newMap)if (newMap = null)throw new Exception(“ControlsSynchronizer:ReplaceMap:rnNew map for replacement is not initialized!“);if (m_pageLayoutControl = null | m_mapControl = null)throw new Exception(“ControlsSynchronizer:ReplaceMap:rnEither MapControl or Pa

31、geLayoutControl are not initialized!“);/create a new instance of IMaps collection which is needed by the PageLayout/创建一个 PageLayout 需要用到的,新的 IMaps collection 的实例IMaps maps = new Maps();/add the new map to the Maps collection/把新的地图加到 Maps collection 里头去maps.Add(newMap);bool bIsMapActive = m_IsMapCtrl

32、active;/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/调用 PageLayout 的 replace map 来置换 focus map/我们必须调用 ActivatePageLayout,因为它是那个我们可以调用“ReplaceMaps“的 Controlthis.ActivatePageLayout();m_pageLayoutContro

33、l.PageLayout.ReplaceMaps(maps);/assign the new map to the MapControl/把新的地图赋给 MapControlm_mapControl.Map = newMap;/reset the active tools/重设 active toolsm_pageLayoutActiveTool = null;m_mapActiveTool = null;/make sure that the last active control is activated/确认之前活动的 control 被激活if (bIsMapActive)this.A

34、ctivateMap();m_mapControl.ActiveView.Refresh();elsethis.ActivatePageLayout();m_pageLayoutControl.ActiveView.Refresh();/ / bind the MapControl and PageLayoutControl together by assigning a new joint focus map/ 指定共同的 Map 来把 MapControl 和 PageLayoutControl 绑在一起/ / true if the MapControl supposed to be a

35、ctivated first,如果 MapControl 被首先激活,则为 truepublic void BindControls(IMapControl3 mapControl, IPageLayoutControl2 pageLayoutControl, bool activateMapFirst)if (mapControl = null | pageLayoutControl = null)throw new Exception(“ControlsSynchronizer:BindControls:rnEither MapControl or PageLayoutControl ar

36、e not initialized!“);m_mapControl = MapControl;m_pageLayoutControl = pageLayoutControl;this.BindControls(activateMapFirst);/ / bind the MapControl and PageLayoutControl together by assigning a new joint focus map/ 指定共同的 Map 来把 MapControl 和 PageLayoutControl 绑在一起/ / true if the MapControl supposed to

37、 be activated first,如果 MapControl 被首先激活,则为 truepublic void BindControls(bool activateMapFirst)if (m_pageLayoutControl = null | m_mapControl = null)throw new Exception(“ControlsSynchronizer:BindControls:rnEither MapControl or PageLayoutControl are not initialized!“);/create a new instance of IMap/创造

38、IMap 的一个实例IMap newMap = new MapClass();newMap.Name = “Map“;/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 collectionmaps.Add(newMap);

39、/call replace map on the PageLayout in order to replace the focus map/调用 PageLayout 的 replace map 来置换 focus mapm_pageLayoutControl.PageLayout.ReplaceMaps(maps);/assign the new map to the MapControl/把新的 map 赋给 MapControlm_mapControl.Map = newMap;/reset the active tools/重设 active toolsm_pageLayoutActi

40、veTool = null;m_mapActiveTool = null;/make sure that the last active control is activated/确定最后活动的 control 被激活if (activateMapFirst)this.ActivateMap();elsethis.ActivatePageLayout();/ /by passing the applications toolbars and TOC to the synchronization class, it saves you the/management of the buddy co

41、ntrol 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 = nu

42、ll)throw new Exception(“ControlsSynchronizer:AddFrameworkControl:rnAdded 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(“Contro

43、lsSynchronizer:RemoveFrameworkControl:rnControl to 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

44、 / when the active control changes, the class iterates through the array of the framework controls/ and calles SetBuddyControl on each of the controls./ / the active controlprivate void SetBuddies(object buddy)tryif (buddy = null)throw new Exception(“ControlsSynchronizer:SetBuddies:rnTarget Buddy Co

45、ntrol 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(“ControlsSynchronizer:SetBuddies:r

46、n0“, ex.Message);#endregion2、新建 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 _sdnMapGuid(“f27d8789-f

47、bbc-4801-be78-0e3cd8fff9d5“)ClassInterface(ClassInterfaceType.None)ProgId(“_sdnMap.Maps“)public class Maps : IMaps, IDisposable/class member - using internally an ArrayList to manage the Maps collectionprivate ArrayList m_array = null;#region class constructorpublic Maps()m_array = new ArrayList();#

48、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/ / public void RemoveAt(int Index)if (Index m_array.Count | Index / Reset the Maps array/ public void Reset()m_array.Clear();/ / Get the number of Maps in the collection/ public int Countgetreturn m_array.Count;/ / Return the Map at the given index/

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报