ArcGIS_Engine+C_实例开发教程

上传人:无*** 文档编号:168070269 上传时间:2022-11-07 格式:DOC 页数:87 大小:773KB
收藏 版权申诉 举报 下载
ArcGIS_Engine+C_实例开发教程_第1页
第1页 / 共87页
ArcGIS_Engine+C_实例开发教程_第2页
第2页 / 共87页
ArcGIS_Engine+C_实例开发教程_第3页
第3页 / 共87页
资源描述:

《ArcGIS_Engine+C_实例开发教程》由会员分享,可在线阅读,更多相关《ArcGIS_Engine+C_实例开发教程(87页珍藏版)》请在装配图网上搜索。

1、第一讲:桌面GIS应用程序框架的建立本讲主要是使用MapControl、PageLayoutControl、ToolbarControl、TOCControl四个控件建立起基本的桌面GIS应用程序框架。最终成果预览如下:1、新建项目启动VS2005,选择“文件|新建|项目”,在项目类型中选择Visual C#,再选择Windows应用程序模板,输入名称“3sdnMap”,点击确定。在解决方案管理器中将“Form1.cs”重命名为“3sdnMap.cs”,在设计视图中,选中窗体,将其属性中的“Text”改为“3sdnMap”。2、添加控件选择工具箱中的“菜单和工具栏|MenuStrip”,将其拖

2、入窗体。选择工具箱中的“ArcGIS Windows Forms”节,将“ToolbarControl”控件拖入窗体,并将其属性中的Dock设置为Top。选择工具箱中的“菜单和工具栏|StatusStrip”,将其拖入到窗体。选择工具箱中的“容器|SplitContainer”容器拖入窗体,并将其属性中的Dock设置为Fill。将TabControl控件拖入Panel1,将Alignment属性设置为Bottom,Dock属性设置为Fill。点击TabPages属性右边的按钮,弹出TabPage集合编辑器,将tabPage1的Name设置为tabPageLayer,Text设置为图层,将tab

3、Page2的Name设置为tabPageProperty,Text设置为属性。如下所示。选择“图层”选项卡,拖入TOCControl控件,设置Dock属性为Fill。选择“属性”选项卡,拖入DataGridView控件,设置Dock属性为Fill。拖入TabControl控件到Panel2,设置Dock属性为Fill。并上述类似的方法,将两个选项卡的Name和Text分别设置为:(tabPageMap、地图),(tabPageLayout,制版)。选择“地图”选项卡,拖入MapControl控件,设置Dock属性为Fill。选择“制版”选项卡,拖入PageLayoutControl控件,设置D

4、ock属性为Fill。最后将LicenseControl控件拖入到窗体的任意地方。按F5编译运行,可以看到刚才布局好的程序界面了。3、控件绑定通过以上步骤添加的控件还只是单独存在,而我们的程序需要各控件间协同工作,因此要进行控件绑定。分别右击ToolbarControl、TOCControl控件,将Buddy设置为axMapControl1,如下图所示。这样,工具条和图层控件就与地图控件关联了。4、添加工具 此时,工具条中还没有任何工具,添加的方法也很简单。右击ToolbarControl,选择“属性|Items”,点击Add,选择Commands选项卡中的Generic,双击Open、Sav

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

6、件是 PropertyGrid 控件。因此请你删除“属性”选项卡中的 DataGridView 控件,再把位于“工具箱 | 所有 Windows 窗体 |PropertyGrid ”(如果没有,右击选择“选择项”以添加此控件)控件拖到该选项卡。 在这一讲中,主要讲解菜单的添加和实现。 1、 添加菜单 在设计视图中,单击菜单栏,会出现“请在此处键入”的提示,单击提示就可以键入菜单名称,如“文件”,再单击“文件”,即可输入其下拉子菜单,如下所示: imgfile:/C:/Documents%20and%20Settings/Administrator/Local%20Settings/Tempor

7、ary%20Internet%20Files/Content.IE5/WVUJQZMD/20090512083746513%5B1%5D.jpg/imgTips : 每创建一个菜单,请在其属性面板中设置 Name 属性,而且不要为中文,因此 Name 值将是此菜单响应函数的函数名的一部分,带中文的函数名,总是不好吧。 本讲中,我们将添加新建( New )、打开( Open )、添加数据( AddData )、保存( Save )、另存为( SaveAs )、退出( Exit )这些菜单,()内为相应的 Name 属性值。 Tips: 你可以在属性面板中的 Text 属性中,把菜单名设置为中英文

8、形式,如“打开 O pen ”,带下划线的 O 表示此项菜单的快捷键是字母 O ,设置方法是在相应字母前加上“ & ”字符,如“打开 &Open ”。但这种快捷键只在打开此下拉菜单时才有效,即当你单击“文件”菜单弹出下拉菜单时,按下字母 O 就可以定位到“打开”菜单。 还有一种在程序运行时都有效的全局快捷键,可以在属性面板中的“ ShortCutKeys ”中设置。 你还可以在属性面板中的 Image 属性中设置你喜欢的菜单图标。单击 Image 那一行右边的按钮,弹出如下菜单。选择“项目资源文件”,再单击导入就可以选择你的图标了。 最终效果如下所示。 注意,在解决方案面板中,选中刚才添加的所

9、有图标,在其属性面板中将生成操作设置为“嵌入的资源”,这一点很重要! 2、 实现相关菜单 首先定义指针(写在 public partial class Form1 : Form 下面即可): private ESRI.ArcGIS.Controls.IMapControl3 m_mapControl = null; private ESRI.ArcGIS.Controls.IPageLayoutControl2 m_pageLayoutControl = null; private IMapDocument pMapDocument; 若以上指针无效,请添加以下引用: using ESRI.A

10、rcGIS.Carto; using ESRI.ArcGIS.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

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

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

13、this.axMapControl1.Object); pCommand.OnClick(); / / 保存地图文档命令 / / / private void Save_Click(object sender, EventArgs e) / 首先确认当前地图文档是否有效 if (null != m_pageLayoutControl.DocumentFilename & m_mapControl.CheckMxFile(m_pageLayoutControl.DocumentFilename) / 创建一个新的地图文档实例 IMapDocument mapDoc = new MapDocume

14、ntClass(); / 打开当前地图文档 mapDoc.Open(m_pageLayoutControl.DocumentFilename, string.Empty); / 用 PageLayout 中的文档替换当前文档中的 PageLayout 部分 mapDoc.ReplaceContents(IMxdContents)m_pageLayoutControl.PageLayout); / 保存地图文档 mapDoc.Save(mapDoc.UsesRelativePaths, false); mapDoc.Close(); / / 另存为地图文档命令 / / / private voi

15、d SaveAs_Click(object sender, EventArgs e) / 调用另存为命令 ICommand command = new ControlsSaveAsDocCommandClass(); command.OnCreate(m_controlsSynchronizer.ActiveControl); command.OnClick(); / / 退出程序 / / / private void Exit_Click(object sender, EventArgs e) Application.Exit(); 3、 编译运行 按 F5 编译运行程序。也许你会发现,菜单

16、命令的实现方式都是类型的。没错,在 AE9.2 中,内置了许多常用的 Command 和 Tool ,如 ControlsAddDataCommandClass 、 ControlsMapZoomInToolClass 、 ControlsMapPanToolClass 等等,这些内置对象在 ESRI.ArcGIS.Controls 命名空间中,你可以对象浏览器中查看。而且这些内置对象的调用方式都类似,如下所示: / 定义 ICommand command = new ControlsSaveAsDocCommandClass(); / 创建 command.OnCreate(m_contro

17、lsSynchronizer.ActiveControl); / 调用 command.OnClick(); 第三讲 MapControl与PageLayoutControl同步 (一)在ArcMap中,能够很方面地进行MapView和Layout View两种视图的切换,而且二者之间的数据是同步显示的。关于两种视图同步的实现方法有多种,可以使用ObjectCopy对象进行数据硬拷贝,而比较简单的方法莫过于二者共享一份地图了,这也是最常用的方法。1、新建同步类ControlsSynchronizer在解决方案面板中右击项目名,选择“添加|类”,在类别中选择“Visual C#项目项”,在模板中

18、选择“类”,输入类名“ControlsSynchronizer.cs”,将以下代码覆盖自动生成的代码:using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.IO;using System.Runtime.InteropServices; using ESRI.ArcGIS.esriSystem;using ESRI.ArcGIS.Carto;using ESRI.ArcGIS.Controls;u

19、sing 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, bind / the control together by calling BindControls which in turn sets a joined Map referen

20、ced / 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 the other by calling ActivateXXX. / This calss is limited to a situation where the contro

21、ls are not simultaneously visible. / public class ControlsSynchronizer #region class members private IMapControl3 m_mapControl = null; private IPageLayoutControl2 m_pageLayoutControl = null; private ITool m_mapActiveTool = null; private ITool m_pageLayoutActiveTool = null; private bool m_IsMapCtrlac

22、tive = true; private ArrayList m_frameworkControls = null; #endregion #region constructor / / 默认构造函数 / public ControlsSynchronizer() /初始化ArrayList m_frameworkControls = new ArrayList(); / / 构造函数 / / / public ControlsSynchronizer(IMapControl3 mapControl, IPageLayoutControl2 pageLayoutControl) : this(

23、) /为类成员赋值 m_mapControl = mapControl; m_pageLayoutControl = pageLayoutControl; #endregion #region properties / / 取得或设置MapControl / public IMapControl3 MapControl get return m_mapControl; set m_mapControl = value; / / 取得或设置PageLayoutControl / public IPageLayoutControl2 PageLayoutControl get return m_p

24、ageLayoutControl; set m_pageLayoutControl = value; / / 取得当前ActiveView的类型 / public string ActiveViewType get if (m_IsMapCtrlactive) return MapControl; else return PageLayoutControl; / / 取得当前活动的Control / public object ActiveControl get if (m_mapControl = null | m_pageLayoutControl = null) throw new Ex

25、ception(ControlsSynchronizer:ActiveControl:rnEither MapControl or PageLayoutControl are not initialized!); if (m_IsMapCtrlactive) return m_mapControl.Object; else return m_pageLayoutControl.Object; #endregion #region Methods / / 激活MapControl并解除the PagleLayoutControl / public void ActivateMap() try i

26、f (m_pageLayoutControl = null | m_mapControl = null) throw new Exception(ControlsSynchronizer:ActivateMap:rnEither MapControl or PageLayoutControl are not initialized!); /缓存当前PageLayout的CurrentTool if (m_pageLayoutControl.CurrentTool != null) m_pageLayoutActiveTool = m_pageLayoutControl.CurrentTool;

27、 /解除PagleLayout m_pageLayoutControl.ActiveView.Deactivate(); /激活MapControl m_mapControl.ActiveView.Activate(m_mapControl.hWnd); /将之前MapControl最后使用的tool,作为活动的tool,赋给MapControl的CurrentTool if (m_mapActiveTool != null) m_mapControl.CurrentTool = m_mapActiveTool; m_IsMapCtrlactive = true; /为每一个的framewor

28、k controls,设置Buddy control为MapControl this.SetBuddies(m_mapControl.Object); catch (Exception ex) throw new Exception(string.Format(ControlsSynchronizer:ActivateMap:rn0, ex.Message); / / 激活PagleLayoutControl并减活MapCotrol / public void ActivatePageLayout() try if (m_pageLayoutControl = null | m_mapCont

29、rol = null) throw new Exception(ControlsSynchronizer:ActivatePageLayout:rnEither MapControl or PageLayoutControl are not initialized!); /缓存当前MapControl的CurrentTool if (m_mapControl.CurrentTool != null) m_mapActiveTool = m_mapControl.CurrentTool; /解除MapControl m_mapControl.ActiveView.Deactivate(); /激

30、活PageLayoutControl m_pageLayoutControl.ActiveView.Activate(m_pageLayoutControl.hWnd); /将之前PageLayoutControl最后使用的tool,作为活动的tool,赋给PageLayoutControl的CurrentTool if (m_pageLayoutActiveTool != null) m_pageLayoutControl.CurrentTool = m_pageLayoutActiveTool; m_IsMapCtrlactive = false; /为每一个的framework cont

31、rols,设置Buddy control为PageLayoutControl this.SetBuddies(m_pageLayoutControl.Object); catch (Exception ex) throw new Exception(string.Format(ControlsSynchronizer:ActivatePageLayout:rn0, ex.Message); / / 给予一个地图, 置换PageLayoutControl和MapControl的focus map / / public void ReplaceMap(IMap newMap) if (newMap

32、 = 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 PageLayoutControl are not initialized!); /create a new instanc

33、e 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_IsMapCtrlactive; /call replace map on the PageLayout in order to repl

34、ace the focus map /we must call ActivatePageLayout, since it is the control we call ReplaceMaps /调用PageLayout的replace map来置换focus map /我们必须调用ActivatePageLayout,因为它是那个我们可以调用ReplaceMaps的Control this.ActivatePageLayout(); m_pageLayoutControl.PageLayout.ReplaceMaps(maps); /assign the new map to the MapC

35、ontrol /把新的地图赋给MapControl m_mapControl.Map = newMap; /reset the active tools /重设active tools m_pageLayoutActiveTool = null; m_mapActiveTool = null; /make sure that the last active control is activated /确认之前活动的control被激活 if (bIsMapActive) this.ActivateMap(); m_mapControl.ActiveView.Refresh(); else th

36、is.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 activated first,如果MapControl被首先激活,则为true public void

37、 BindControls(IMapControl3 mapControl, IPageLayoutControl2 pageLayoutControl, bool activateMapFirst) if (mapControl = null | pageLayoutControl = null) throw new Exception(ControlsSynchronizer:BindControls:rnEither MapControl or PageLayoutControl are not initialized!); m_mapControl = MapControl; m_pa

38、geLayoutControl = 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 be activated first,如果MapControl被首先激活,则为true public v

39、oid 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 /创造IMap的一个实例 IMap newMap = new MapClass(); newMap.Name =

40、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 collection maps.Add(newMap); /call replace map on the PageLayout in order to replace

41、 the focus map /调用PageLayout的replace map来置换focus map m_pageLayoutControl.PageLayout.ReplaceMaps(maps); /assign the new map to the MapControl /把新的map赋给MapControl m_mapControl.Map = newMap; /reset the active tools /重设active tools m_pageLayoutActiveTool = null; m_mapActiveTool = null; /make sure that t

42、he last active control is activated /确定最后活动的control被激活 if (activateMapFirst) this.ActivateMap(); else this.ActivatePageLayout(); / /by passing the applications toolbars and TOC to the synchronization class, it saves you the /management of the buddy control each time the active control changes. This

43、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 = null) throw new Exception(ControlsSynchronizer:A

44、ddFrameworkControl: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(ControlsSynchronizer:RemoveFrameworkControl:rnCon

45、trol 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 index) throw new Exception(ControlsSync

46、hronizer:RemoveFrameworkControlAt:rnIndex is out of range!); m_frameworkControls.RemoveAt(index); / / 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 control private void SetBuddies(o

47、bject buddy) try if (buddy = null) throw new Exception(ControlsSynchronizer:SetBuddies:rnTarget Buddy Control 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:rn0, ex.Message); #endregion 第三讲 MapContr

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!