




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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设置为图层,将ta
3、bPage2的Name设置为tabPageProperty,Text设置为属性。如下所示。 选择“图层”选项卡,拖入TOCControl控件,设置Dock属性为Fill。选择“属性”选项卡,拖入DataGridView控件,设置Dock属性为Fill。拖入TabControl控件到Panel2,设置Dock属性为Fill。并上述类似的方法,将两个选项卡的Name和Text分别设置为:(tabPageMap、地图),(tabPageLayout,制版)。选择“地图”选项卡,拖入MapControl控件,设置Dock属性为Fill。选择“制版”选项卡,拖入PageLayoutControl控件,设
4、置Dock属性为Fill。最后将LicenseControl控件拖入到窗体的任意地方。按F5编译运行,可以看到刚才布局好的程序界面了。3、控件绑定通过以上步骤添加的控件还只是单独存在,而我们的程序需要各控件间协同工作,因此要进行控件绑定。分别右击ToolbarControl、TOCControl控件,将Buddy设置为axMapControl1,如下图所示。 这样,工具条和图层控件就与地图控件关联了。4、添加工具此时,工具条中还没有任何工具,添加的方法也很简单。右击ToolbarControl,选择“属性|Items”,点击Add,选择Commands选项卡中的Generic,双击Open、S
5、aveAs、Redo、Undo即可将相应工具添加到工具条。 常见的工具有:Map Navigation中的导航工具,Map Inquiry中的查询工具,Feature Selection中的选择工具,你可以根据需要酌情添加工具。5、编译运行按F5即可编译运行程序,至此桌面GIS应用程序框架基本框架已经搭建好了,你可以通过工具条的工具打开地图文档,浏览地图了,效果如开篇所示。下一讲中将大家带来的是菜单的添加及其实现,敬请关注3SDN.NET。第二讲 菜单的添加及其实现在上一讲中,我们实现了应用程序基本框架,其中有个小错误,在此先跟大家说明下。在“属性”选项卡中,我们当时添加的是 DataGrid
6、View 控件,这个控件是用来显示数据表的,而专门用于属性的查询和设置的控件是 PropertyGrid 控件。因此请你删除“属性”选项卡中的 DataGridView 控件,再把位于“工具箱 | 所有 Windows 窗体 |PropertyGrid ”(如果没有,右击选择“选择项”以添加此控件)控件拖到该选项卡。 在这一讲中,主要讲解菜单的添加和实现。 1、 添加菜单 在设计视图中,单击菜单栏,会出现“请在此处键入”的提示,单击提示就可以键入菜单名称,如“文件”,再单击“文件”,即可输入其下拉子菜单,如下所示: Tips : 每创建一个菜单,请在其属性面板中设置 Name 属性,而且不要为
7、中文,因此 Name 值将是此菜单响应函数的函数名的一部分,带中文的函数名,总是不好吧。 本讲中,我们将添加新建( New )、打开( Open )、添加数据( AddData )、保存( Save )、另存为( SaveAs )、退出( Exit )这些菜单,()内为相应的 Name 属性值。 Tips: 你可以在属性面板中的 Text 属性中,把菜单名设置为中英文形式,如“打开 O pen ”,带下划线的 O 表示此项菜单的快捷键是字母 O ,设置方法是在相应字母前加上“ & ”字符,如“打开 &Open ”。但这种快捷键只在打开此下拉菜单时才有效,即当你单击“文件”菜单弹出下拉菜单时,按
8、下字母 O 就可以定位到“打开”菜单。 还有一种在程序运行时都有效的全局快捷键,可以在属性面板中的“ ShortCutKeys ”中设置。 你还可以在属性面板中的 Image 属性中设置你喜欢的菜单图标。单击 Image 那一行右边的按钮,弹出如下菜单。选择“项目资源文件”,再单击导入就可以选择你的图标了。 最终效果如下所示。 注意,在解决方案面板中,选中刚才添加的所有图标,在其属性面板中将生成操作设置为“嵌入的资源”,这一点很重要! 2、 实现相关菜单 首先定义指针(写在 public partial class Form1 : Form 下面即可): 3 m_mapControl = nu
9、ll; 2 m_pageLayoutControl = null; private IMapDocument pMapDocument; 若以上指针无效,请添加以下引用: 在设计视图中的属性面板中,选择 Form1 ,即主窗体,单击事件按钮(闪电形状的那个按钮),打到“ Load ”事件并双击,添加此事件。 在 Form1_Load 函数中初始化这些指针: / 取得 MapControl 和 PageLayoutControl 的引用 依次双击每个菜单项,添加菜单响应函数。实现代码如下: / / 新建地图命令 / / / private void New_Click(object sender
10、, EventArgs e) / 本命令涉及到 MapControl 和 PageLayoutControl 同步问题,将在下一讲中实现 / / 打开地图文档 Mxd 命令 / / / private void Open_Click(object sender, EventArgs e) / 本命令涉及到 MapControl 和 PageLayoutControl 同步问题,将在下一讲中实现 / / 添加数据命令 / / / private void AddData_Click(object sender, EventArgs e) ICommand pCommand = new Contr
11、olsAddDataCommandClass(); pCommand.OnClick(); / / 保存地图文档命令 / / / private void Save_Click(object sender, EventArgs e) / 首先确认当前地图文档是否有效 if (null!= m_pageLayoutControl.DocumentFilename & m_mapControl.CheckMxFile(m_pageLayoutControl.DocumentFilename) / 创建一个新的地图文档实例 IMapDocument mapDoc = new MapDocumentC
12、lass(); / 打开当前地图文档 mapDoc.Open(m_pageLayoutControl.DocumentFilename, string.Empty); / 用 PageLayout 中的文档替换当前文档中的 PageLayout 部分 mapDoc.ReplaceContents(IMxdContents)m_pageLayoutControl.PageLayout); / 保存地图文档 mapDoc.Save(mapDoc.UsesRelativePaths, false); mapDoc.Close(); / / 另存为地图文档命令 / / / private void S
13、aveAs_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、 编译运行 / 定义 ICommand command =
14、new ControlsSaveAsDocCommandClass(); / 创建 command.OnCreate(m_controlsSynchronizer.ActiveControl); / 调用 command.OnClick(); 希望你可以举一反三,去实现更多的你想要的功能。 在下一讲中,我将给大家带来的是 MapControl 与 PageLayoutControl 两种视图同步的实现,敬请关注 3SDN.NET 。 第三讲 MapControl与PageLayoutControl同步在ArcMap中,能够很方面地进行MapView和Layout View两种视图的切换,而且二
15、者之间的数据是同步显示的。关于两种视图同步的实现方法有多种,可以使用ObjectCopy对象进行数据硬拷贝,而比较简单的方法莫过于二者共享一份地图了,这也是最常用的方法。1、新建同步类ControlsSynchronizer在解决方案面板中右击项目名,选择“添加|类”,在类别中选择“Visual C#项目项”,在模板中选择“类”,输入类名“ControlsSynchronizer.cs”,将以下代码覆盖自动生成的代码:using System;using System.Drawing;using System.Collections;using System.ComponentModel;us
16、ing System.IO;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 referenced / by bot
17、h 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 controls are not s
18、imultaneously 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_IsMapCtrlactive = true;
19、 private ArrayList m_frameworkControls = null; #endregion #region constructor / / 默认构造函数 / public ControlsSynchronizer() /初始化ArrayList m_frameworkControls = new ArrayList(); / / 构造函数 / / / public ControlsSynchronizer(IMapControl3 mapControl, IPageLayoutControl2 pageLayoutControl) : this() /为类成员赋值 m_
20、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_pageLayoutCon
21、trol; 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 Exception(Cont
22、rolsSynchronizer: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 if (m_pageLay
23、outControl = 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; /解除PagleLay
24、out/激活MapControl /将之前MapControl最后使用的tool,作为活动的tool,赋给MapControl的CurrentTool if (m_mapActiveTool != null) m_mapControl.CurrentTool = m_mapActiveTool; m_IsMapCtrlactive = true; /为每一个的framework controls,设置Buddy control为MapControl this.SetBuddies(m_mapControl.Object); catch (Exception ex) throw new Exce
25、ption(string.Format(ControlsSynchronizer:ActivateMap:rn0, ex.Message); / / 激活PagleLayoutControl并减活MapCotrol / public void ActivatePageLayout() try if (m_pageLayoutControl = null | m_mapControl = null) throw new Exception(ControlsSynchronizer:ActivatePageLayout:rnEither MapControl or PageLayoutContro
26、l are not initialized!); /缓存当前MapControl的CurrentTool if (m_mapControl.CurrentTool != null) m_mapActiveTool = m_mapControl.CurrentTool; /解除MapControl /激活PageLayoutControl /将之前PageLayoutControl最后使用的tool,作为活动的tool,赋给PageLayoutControl的CurrentTool if (m_pageLayoutActiveTool != null) m_pageLayoutControl.C
27、urrentTool = m_pageLayoutActiveTool; m_IsMapCtrlactive = false; /为每一个的framework controls,设置Buddy control为PageLayoutControl this.SetBuddies(m_pageLayoutControl.Object); catch (Exception ex) throw new Exception(string.Format(ControlsSynchronizer:ActivatePageLayout:rn0, ex.Message); / / 给予一个地图, 置换PageL
28、ayoutControl和MapControl的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:r
29、nEither MapControl or PageLayoutControl 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 bI
30、sMapActive = m_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 /调用PageLayout的replace map来置换focus map /我们必须调用ActivatePageLayout,因为它是那个我们可以调用ReplaceMaps的Control this.ActivatePageLayout(
31、); /assign the new map to the MapControl /把新的地图赋给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(); else thi
32、s.ActivatePageLayout(); / / 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 BindControls(IMapControl3 mapControl, IPag
33、eLayoutControl2 pageLayoutControl, bool activateMapFirst) if (mapControl = null | pageLayoutControl = null) throw new Exception(ControlsSynchronizer:BindControls:rnEither MapControl or PageLayoutControl are not initialized!); m_mapControl = MapControl; m_pageLayoutControl = pageLayoutControl; this.B
34、indControls(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 void BindControls(bool activateMapFirst) if
35、(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 = Map; /create a new instance of IMaps collec
36、tion 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 the focus map /调用PageLayout的replace map来置换
37、focus map /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 the last active control is activated /确定最后活动的control被激活 if (activateMapFirst) this.ActivateMap
38、(); 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 method ads the framework /control to an array; once the active control changes, the class ite
39、rates 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:AddFrameworkControl:rnAdded control is not initialized!); m_frameworkControls.Add(control); /
40、/ Remove a framework control from the managed list of controls / / public void RemoveFrameworkControl(object control) if (control = null) throw new Exception(ControlsSynchronizer:RemoveFrameworkControl:rnControl to be removed is not initialized!); m_frameworkControls.Remove(control); / / Remove a fr
41、amework 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(ControlsSynchronizer: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
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论