实验报告GIS应用软件开发专业:测绘工程班级:10 级测绘三班姓名:学号:****:**撰写时间:2013 年5 月20 日星期一目录一、程序运行演示 (2)二、问题及解决方案 (6)三、源程序代码 (7)四、心得体会 (14)一、程序运行演示1. 新建项目文件,并实现file 中的基本功能,显示地图基本要素。
如图 1图12. 在工具栏加载按钮,按钮功能是对当前视图缩小 3 倍;在工具栏加载工具,工具功能在地图上标注当前日期。
如图 2图2按钮实现缩小 3 倍功能操作,如图 3图33.点击地图节点,弹出如图4,图5 菜单并实现菜单中的内容:图4图54. 点击图层节点,弹出如图 6 菜单并需要实现菜单中的内容图65. 点击Legend Class ,弹出符号选择对话框,该对话框供用户按需要选择符号并进行更改。
如图7图76.打开图层属性表,用户选择某条记录,可弹出如图8,9 所示的右键菜单,并实现右键菜单中的内容图8图9二、问题及解决方案问题1.VS2008 与2010 之间要进行格式转换,按照所导向的步骤进行,否则程序将无法正常运行,出现错误问题2. 构建好基本框架后,地图文档中要素显示出现问题。
此问题要添加licence 控件,否则无法显示地图要素。
问题3. 使用Toolbar 和Toc功能时无法进行操作此问题应在属性设置时建立关联功能。
问题4. 程序Program文件中要添加相应功能键语句,否则将无法实现功能。
问题5. 引用接口,方法,例如ADF,要添加相应的引用,否则程序出现错误。
问题6. 在实现两个自定义按钮与工具时,遇到的问题:先直接引用现有项未实现,发现缺少一些东西,后自己直接创建的Basetool 和Basecomman类d ;在代码中报错,重新生成解决方案后顺利解决这个问题。
问题7. 在实现地图和图层节点的右键功能时,遇到琐碎的问题,在这个过程中直接引用老师所给的几个类,但是报错,是因为命名空间未做修改;缺少引用,需要根据错误提示添加相应的引用三、源程序代码using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;using System.Runtime.InteropServices;using ESRI.ArcGIS.esriSystem;using ESRI.ArcGIS.Carto;using ESRI.ArcGIS.Controls;using ESRI.ArcGIS.ADF;using ESRI.ArcGIS.SystemUI;using ESRI.ArcGIS.Display;namespace WindowsFormsApplication2{public partial class MainForm : Form{#region class private membersprivate IMapControl3 m_mapControl = null;private string m_mapDocumentName = string.Empty;#endregionIMapDocument m_MapDocument = new MapDocument();#region class constructorprivate ITOCControl2 m_tocControl;private IToolbarMenu m_menuMap;private IToolbarMenu m_menuLayer;private ZoomIn3XCMD1 test = new ZoomIn3XCMD1();public MainForm(){InitializeComponent();}private void newDocumentToolStripMenuItem_Click(object sender, EventArgs e){ICommand command = new CreateNewDocument();command.OnCreate(m_mapControl.Object);command.OnClick();}private void openDocumentToolStripMenuItem_Click(object sender, EventArgs e){openFileDialog1.Title = "Save Map Document As";openFileDialog1.Filter = "Map Documents (*.mxd)|*.mxd";openFileDialog1.ShowDialog();string sFilePath = openFileDialog1.FileName;if (axMapControl1.CheckMxFile(sFilePath)){axMapControl1.MousePointer = esriControlsMousePointer.esriPointerHourglass;axMapControl1.LoadMxFile(sFilePath, 0, Type.Missing);axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;}else{MessageBox.Show(sFilePath + "is not a valid ArcMap document");return;}}private void MainForm_Load(object sender, EventArgs e){//get the MapControlm_mapControl = (IMapControl3)axMapControl1.Object;//disable the Save menu (since there is no document yet)this.menuSaveDoc.Enabled = false;// 定义地图右键菜单,图层右键菜单m_tocControl = (ITOCControl2)axTOCControl1.Object;m_mapControl = (IMapControl3)axMapControl1.Object;// 添加菜单到地图节点m_menuMap = new ToolbarMenu();m_menuMap.AddItem("esriControls.ControlsAdddataCommand", -1, 0, false, esriCommandStyles.esriCommandStyleTextOnly);m_menuMap.AddItem(new LayerVisibility(), 1, 1, false, esriCommandStyles.esriCommandStyleTextOnly);m_menuMap.AddItem(new LayerVisibility(), 2, 2, false, esriCommandStyles.esriCommandStyleTextOnly);// 添加菜单到图层节点m_menuLayer = new ToolbarMenu();m_menuLayer.AddItem(new OpenAttributeTableCmd(), -1, 0, false, esriCommandStyles.esriCommandStyleIconAndText);// 右键添加属性表m_menuLayer.AddItem(new RemoveLayer(), 1, 1, false, esriCommandStyles.esriCommandStyleTextOnly);m_menuLayer.AddItem(new RemoveLayer(), 1, 2, true, esriCommandStyles.esriCommandStyleTextOnly);m_menuLayer.AddItem(new ScaleThresholds(), 2, 3, false, esriCommandStyles.esriCommandStyleTextOnly);m_menuLayer.AddItem(new ScaleThresholds(), 3, 4, false, esriCommandStyles.esriCommandStyleTextOnly);m_menuLayer.AddItem(new LayerSelectable(), 1, 5, true, esriCommandStyles.esriCommandStyleTextOnly);m_menuLayer.AddItem(new LayerSelectable(), 2, 6, false, esriCommandStyles.esriCommandStyleTextOnly);m_menuLayer.AddItem(new ZoomToLayer(), -1, 7, true, esriCommandStyles.esriCommandStyleTextOnly);//Set the hook of each menum_menuLayer.SetHook(m_mapControl);m_menuMap.SetHook(m_mapControl);axToolbarControl1.AddItem(test, -1, -1, true, 0, esriCommandStyles.esriCommandStyleIconAndText);axToolbarControl1.AddItem(new AddDateTool(), -1, -1, false, 0,esriCommandStyles.esriCommandStyleIconAndText);}private void saveDocumentToolStripMenuItem_Click(object sender, EventArgs e){//execute Save Document commandif (m_mapControl.CheckMxFile(m_mapDocumentName)){//create a new instance of a MapDocumentIMapDocument mapDoc = new MapDocument();mapDoc.Open(m_mapDocumentName, string.Empty);//Make sure that the MapDocument is not readonlyif (mapDoc.get_IsReadOnly(m_mapDocumentName)){MessageBox.Show("Map document is read only!");mapDoc.Close();return;}//Replace its contents with the current mapmapDoc.ReplaceContents((IMxdContents)m_mapControl.Map);//save the MapDocument in order to persist itmapDoc.Save(esRelativePaths, false);//close the MapDocumentmapDoc.Close();}}#endregionprivate void saveAsToolStripMenuItem_Click(object sender, EventArgs e){//execute SaveAs Document commandICommand command = new ControlsSaveAsDocCommand();command.OnCreate(m_mapControl);command.OnClick();}private void exitToolStripMenuItem_Click(object sender, EventArgs e){Application.Exit();}private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e){statusBarXY.Text =string.Format("{0}, {1} {2}", e.mapX.ToString("#######.##"),e.mapY.ToString("#######.##"), axMapControl1.MapUnits.ToString().Substring(4));}private void axMapControl1_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e){test.m_activeView = this.axMapControl1.ActiveView;// 添加两个工具时添加//get the current document name from the MapControlm_mapDocumentName = m_mapControl.DocumentFilename;//if there is no MapDocument, diable the Save menu and clear the statusbarif (m_mapDocumentName == string.Empty){menuSaveDoc.Enabled = false;statusBarXY.Text = string.Empty;}else{//enable the Save manu and write the doc name to the statusbarmenuSaveDoc.Enabled = true;statusBarXY.Text = Path.GetFileName(m_mapDocumentName);}}private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e){if (e.button == 1){//IBasicMap map = new Map();IMap Map = new Map();IBasicMap map = (IBasicMap)Map;ILayer layer = new FeatureLayer();object other = new object();object index = new object();esriTOCControlItem item = new esriTOCControlItem();//Determine what kind of item has been clicked onaxTOCControl1.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index);//QI to IFeatureLayer and IGeoFeatuerLayer interfaceif (layer == null) return;IFeatureLayer featureLayer = layer as IFeatureLayer;if (featureLayer == null) return;IGeoFeatureLayer geoFeatureLayer = (IGeoFeatureLayer)featureLayer;ILegendClass legendClass = new LegendClass();ISymbol symbol = null;if (other is ILegendGroup && (int)index != -1){legendClass = ((ILegendGroup)other).get_Class((int)index);symbol = legendClass.Symbol;}if (symbol == null) return;symbol = GetSymbolByControl(symbol);//symbol = GetSymbolBySymbolSelector(symbol);if (symbol == null) return;legendClass.Symbol = symbol;this.Activate();//Fire contents changed event that the TOCControl listens toaxMapControl1.ActiveView.ContentsChanged();//Refresh the displayaxMapControl1.Refresh(esriViewDrawPhase.esriViewGeography, null, null);axTOCControl1.Update();}if (e.button == 2){esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;IBasicMap map = null; ILayer layer = null;object other = null; object index = null;//Determine what kind of item is selectedm_tocControl.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index);//Ensure the item gets selectedif (item == esriTOCControlItem.esriTOCControlItemMap)m_tocControl.SelectItem(map, null);elsem_tocControl.SelectItem(layer, null);// Set the layer into the CustomProperty (this is used by the custom layer commands)m_mapControl.CustomProperty = layer;//Popup the correct context menuif (item == esriTOCControlItem.esriTOCControlItemMap)m_menuMap.PopupMenu(e.x, e.y, m_tocControl.hWnd);if (item == esriTOCControlItem.esriTOCControlItemLayer)m_menuLayer.PopupMenu(e.x, e.y, m_tocControl.hWnd);}}private ISymbol GetSymbolByControl(ISymbol symbolType){ISymbol symbol = null;IStyleGalleryItem styleGalleryItem = null;esriSymbologyStyleClass styleClass = esriSymbologyStyleClass.esriStyleClassMarkerSymbols;if (symbolType is IMarkerSymbol){styleClass = esriSymbologyStyleClass.esriStyleClassMarkerSymbols;}if (symbolType is ILineSymbol){styleClass = esriSymbologyStyleClass.esriStyleClassLineSymbols;}if (symbolType is IFillSymbol){styleClass = esriSymbologyStyleClass.esriStyleClassFillSymbols;}GetSymbolByControlForm symbolForm = new GetSymbolByControlForm(styleClass);symbolForm.ShowDialog();styleGalleryItem = symbolForm.m_styleGalleryItem;if (styleGalleryItem == null) return null;symbol = styleGalleryItem.Item as ISymbol;symbolForm.Dispose();this.Activate();return symbol;}}}四、心得体会本次实验过程中,值得肯定的是,已对AO开发一般流程基本掌握:新建项目、搭建框架、实现各个框架中的各个功能、添加引用与类。