GIS二次开发实验报告学号:姓名:班级:专业:目录1系统特色 (1)2DotNetBar介绍 (3)3文件打开实现 (4)4多标签切换实现 (7)5 属性表实现 (8)6柱状图渲染实现 (10)7 状态条实现与比例尺调整实现 (15)8 最短路径分析实 (16)9 多边形选择 (23)10 鹰眼设计实现 (24)11 实验总结 (25)1系统特色系统特色:1、人性化界面,大标签UI。
2、D otNetBar控件同ArcGis控件结合,提升系统的整体美观程度。
3、一体化仿Office主题设计,色调和谐。
4、多标签模式,支持同时打开多个文档。
5、属性设置方便直观。
6、图层窗、鹰眼窗、属性表窗支持任意拖动调整位置与停靠。
2DotNetBar介绍DotNetBar是一组用于.NET Framework环境下的一组组件集,利用该组件集能够打造绚丽并且实用的应用程序界面,给开发人员提供极大的便利。
由于DotNetBar2内容极为丰富,因此先在工具箱中“添加选项卡”(DotNetBar2),然后在此选项卡下“选择项”,浏览DevComponents.DotNetBar2.dll文件,显示如下:3 文件打开实现● 单击“文件”按钮,会弹出“打开”和“关闭”选项,代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 private void OpenMXD_Click(object sender, EventArgs e)//“打开MXD 文件”按钮{OpenFileDialog ofDialog = new OpenFileDialog();ofDialog.Title = "输入ArcMap 文档名称";ofDialog.Filter = "ArcView MXD 文件|*.mxd";ofDialog.Multiselect = false ;if (ofDialog.ShowDialog() == DialogResult.OK)openFile(ofDialog.FileName, false );}private void Openshape_Click(object sender, EventArgs e)//“打开shape 文件”按钮{IWorkspaceFactory shpFactory = new ShapefileWorkspaceFactoryClass();OpenFileDialog ofDialog = new OpenFileDialog();ofDialog.Title = "输入ArcMap 文档名称";ofDialog.Filter = "ArcView Shape 文件|*.shp";ofDialog.Multiselect = false ;if (ofDialog.ShowDialog() == DialogResult.OK){string fileName = ofDialog.FileName;string filePath = fileName.Substring(0, stIndexOf('\\'));IWorkspace shpWorkspace = shpFactory.OpenFromFile(filePath, 0);IFeatureLayer ftlayer = new FeatureLayerClass();IEnumDataset ds = shpWorkspace.get_Datasets(esriDatasetType.esriDTFeatureClass); IDataset featureClass = ds.Next();ftlayer.FeatureClass = (IFeatureClass)featureClass; = fileName;ILayer layer = ftlayer as ILayer;IMap mmap = sizelist[(int )superTabControl.SelectedTab.Tag].Map;mmap.AddLayer(layer);IActiveView act = mmap as IActiveView;act.Refresh();}}● 执行“打开文档”命令,代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public void openFile(string fileName, bool isRecentFile)//打开文档{ribbonTabItem2.Enabled = false ;ribbonTabItem1.Enabled = true ;m_ipPathFinder = null ;if (!isRecentFile)//如果不是从“最近打开”处打开的则记录该文档名writeRecentFile(fileName);if (switch1)//当不是空文档时{ switch2 = false ;//新建一个文档标签为去地址文件名的标签SuperTabItem tabItem =superTabControl.CreateTab(System.IO.Path.GetFileNameWithoutExtension(fileName)); AxMapControl newaxmap = new AxMapControl();sizelist.Add(newaxmap);newaxmap.SendToBack();newaxmap.Dock = DockStyle.Fill;18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 tabItem.AttachedControl.Controls.Add(newaxmap);superTabControl.SelectNextTab();superTabControl.SelectedTab.Tag = n++;newaxmap.LoadMxFile(fileName);axTOCClayer.SetBuddyControl(newaxmap);superTabControl.SelectNextTab();newaxmap.OnMouseMove +=new IMapControlEvents2_Ax_OnMouseMoveEventHandler(axMapControlOnMouseMove);switch2 = true ;newaxmap.OnMouseDown +=new IMapControlEvents2_Ax_OnMouseDownEventHandler(axMapControlOnMouseDown);}else //否则直接载入{superTabControl.SelectedTab.Text =System.IO.Path.GetFileNameWithoutExtension(fileName);sizelist[(int )superTabControl.SelectedTab.Tag].LoadMxFile(fileName);switch1 = true ;}anotherrefresh();//刷新标签switch2 = true ;bar4.Show();//鹰眼框,图层框可用bar5.Show();bar5.AutoHide = true ;axMapEagleEye.Extent = axMapEagleEye.FullExtent;axMapEagleEye.ActiveView.Refresh();//读取刚载入的地图的图层和字段信息getLayerInfo(sizelist[(int )superTabControl.SelectedTab.Tag]);getAttributeInfo(sizelist[(int )superTabControl.SelectedTab.Tag]);}本系统支持快速打开最近曾经打开过的文档,代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 public void writeRecentFile(string filename)//写入“最近文档”的记录文件{try{StreamWriter s = new StreamWriter("\\Menu.ini", true );s.WriteLine(filename);s.Flush();s.Close();}catch { }}public void readRecentFile()//读取最近打开的文档{itemContainer4.SubItems.Clear();try {StreamReader sr = new StreamReader("\\Menu.ini");while (sr.Peek() >= 0){ButtonItem recentFile = new ButtonItem();recentFile.Tag = sr.ReadLine();recentFile.Text = "&" + System.IO.Path.GetFileNameWithoutExtension((string )recentFile.Tag); recentFile.Click += new EventHandler(openRecentFile);itemContainer4.SubItems.Add(recentFile);}}catch { }}public void openRecentFile(object sender, EventArgs e)//打开最近打开的文档30 31 32 33 {ButtonItem tempButton = (ButtonItem)sender;openFile((string )tempButton.Tag, true );}● 清除“最近打开栏”中的记录,代码如下:1 2 3 4 5 6 7 8 private void clearRecentFile_Click(object sender, EventArgs e)//清除“最近打开”{itemContainer4.SubItems.Clear();System.IO.FileStream stream = File.Open("\\Menu.ini", FileMode.OpenOrCreate, FileAccess.Write); stream.Seek(0, SeekOrigin.Begin);stream.SetLength(0);stream.Close();}● 当当前标签没有加载地图时,初始化一个空文档,代码如下:12 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 private void FormWithNoDocument()//初始化一个空文档{ribbonTabItem1.Enabled = false ;//空文档下不能进行图层选择操作ribbonTabItem2.Enabled = false ;bar5.Hide();//鹰眼框,图层框,属性表框都隐藏superTabControl.CreateTab("空文档");//创建标签为“空文档”的标签superTabControl.SelectedTab.Tag = n++;bar4.Hide();bar3.Hide();AxMapControl newaxmap = new AxMapControl();//动态创建一个axmapcontrolnewaxmap.BeginInit();newaxmap.SendToBack();newaxmap.Dock = DockStyle.Fill;sizelist.Add(newaxmap);superTabControl.SelectedTab.AttachedControl.Controls.Add(newaxmap);//将其加入该标签newaxmap.OnExtentUpdated +=new IMapControlEvents2_Ax_OnExtentUpdatedEventHandler(axMapControlOnExtentUpdated);newaxmap.OnMouseMove +=new IMapControlEvents2_Ax_OnMouseMoveEventHandler(axMapControlOnMouseMove);newaxmap.OnMouseDown +=new IMapControlEvents2_Ax_OnMouseDownEventHandler(axMapControlOnMouseDown);newaxmap.EndInit();axTOCClayer.SetBuddyControl(newaxmap);//设置为关联组件BTNattribute1.SubItems.Clear();//初始化所有图层、字段选择框BTNlayer1.SubItems.Clear();buttonItem4.SubItems.Clear();buttonItem7.SubItems.Clear();}单击文件按钮,弹出菜单4 多标签切换实现● 相应“关闭标签”事件,代码如下:1 2 3 4 5 6 7 8 9 private void superTabControl1_TabItemClose(object sender, SuperTabStripTabItemCloseEventArgs e) {if (superTabControl.Tabs.Count == 1)//如果只剩下唯一一个t 标签(一定是“空文档”标签){switch2 = false ; switch1 = false ;FormWithNoDocument();}}● 相应“标签切换”事件,代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34//当不是空文档时切换的文档标签切换事件private void superTabControl_SelectedTabChanged(object sender, SuperTabStripSelectedTabChangedEventArgs e){if (switch2){m_ipPathFinder = null ;axMapEagleEye.LoadMxFile(sizelist[(int )superTabControl.SelectedTab.Tag].DocumentFilename);//加载该文档 axMapEagleEye.Extent = axMapEagleEye.FullExtent;axMapEagleEye.ActiveView.Refresh();axTOCClayer.SetBuddyControl(sizelist[(int )superTabControl.SelectedTab.Tag]);//重新设置关联组件axTOCClayer.ActiveView.Refresh();nowSelectedLayer = 0;//初始化选择的图层getLayerInfo(sizelist[(int )superTabControl.SelectedTab.Tag]);//重新读取该文档的信息 getAttributeInfo(sizelist[(int )superTabControl.SelectedTab.Tag]);} }private void anotherrefresh()//当是空文档时切换的文档标签切换事件{m_ipPathFinder = null ;axMapEagleEye.LoadMxFile(sizelist[(int )superTabControl.SelectedTab.Tag].DocumentFilename);axMapEagleEye.Extent = axMapEagleEye.FullExtent;axMapEagleEye.ActiveView.Refresh();axTOCClayer.SetBuddyControl(sizelist[(int )superTabControl.SelectedTab.Tag]);axTOCClayer.ActiveView.Refresh();nowSelectedLayer = 0;getLayerInfo(sizelist[(int )superTabControl.SelectedTab.Tag]);getAttributeInfo(sizelist[(int )superTabControl.SelectedTab.Tag]);}5 属性表实现● 相应右键菜单“查看属性表” ,代码如下:1 2 3 4 5 6 7 8 9 private void 查看属性表ToolStripMenuItem_Click(object sender, EventArgs e){if (!barisshow){shuxingbiao(); barisshow = true ;//表示属性表已打开查看属性表ToolStripMenuItem.Enabled = false ;}}● 相应ArcGis 图层列表单击事件,代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21ILayer m_Layer;//记录图层,提供给数据窗private void axTOCControlOnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)//右键菜单 {AxTOCControl axt1 = (AxTOCControl)sender;esriTOCControlItem Item = esriTOCControlItem.esriTOCControlItemNone;IBasicMap pBasicMap = null ;ILayer pLayer = null ;object other = null ;object index = null ;//根据点击位置实现赋值axt1.HitTest(e.x, e.y, ref Item, ref pBasicMap, ref pLayer, ref other, ref index); m_Layer = pLayer; if (Item == esriTOCControlItem.esriTOCControlItemLayer|| Item == esriTOCControlItem.esriTOCControlItemLegendClass)//如果点击的地方是图层名或者图例 {if (e.button == 2)//显示右键菜单,并定义其相对控件的位置,正好在鼠标出显示contextMenuStrip1.Show(axt1, new System.Drawing.Point(e.x, e.y));else if (barisshow)//若已打开属性表框则左键点击直接显示其属性shuxingbiao();}}● 显示属性表,代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 private void shuxingbiao()//显示所选图层的属性表{IFeatureLayer pFeatureLayer = m_Layer as IFeatureLayer;IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;IFeatureCursor pFeatureCursor = pFeatureClass.Search(null , false );IFeature pFeature = pFeatureCursor.NextFeature();IFields pFields = pFeatureClass.Fields;int n1 = 0, n2 = 0;DataTable pTable = new DataTable();for (int i = 0; i < pFields.FieldCount; i++)//获取所有列{DataColumn pColumn = new DataColumn(pFields.get_Field(i).Name);if (pFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry)n1 = i;15161718192021222324252627282930313233pTable.Columns.Add(pColumn);}while (pFeature != null){DataRow pRow = pTable.NewRow();for (int i = 0; i < pFields.FieldCount; i++)//添加每一列的值 {pRow[i] = pFeature.get_Value(i);}pRow[n1] = getshapetype(m_Layer);pTable.Rows.Add(pRow);pFeature = pFeatureCursor.NextFeature();n2++;}dockContainerItem1.Control.Show();dataGridViewX1.DataSource = pTable;bar3.Show();barisshow = true;}右键显示属性表,属性表在下方显示6 柱状图渲染实现● 初始化图层信息,并将其添加到“选择图层”按钮的子项中,代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33int pointLayer = 0;//记录点图层编号 private void getLayerInfo(AxMapControl axmaptemp)//读取图层信息 { BTNlayer1.SubItems.Clear();//清理菜单项的遗留 buttonItem4.SubItems.Clear(); BTNlayer2.SubItems.Clear(); for (int i = 0; i < yerCount; i++) { ButtonItem newButtonItem = new ButtonItem();//新建菜单项 newButtonItem.Text = axmaptemp.get_Layer(i).Name;//将图层名赋予新建菜单项 newButtonItem.Tag = i;//设置该菜单项与图层对应的编号 newButtonItem.AutoCollapseOnClick = true ; newButtonItem.Click += new EventHandler(readLayerInfo);//绑定事件 BTNlayer1.SubItems.Add(newButtonItem);//加为该按钮的子项菜单 ButtonItem newButtonItem2 = new ButtonItem(); newButtonItem2.Text = axmaptemp.get_Layer(i).Name; newButtonItem2.Tag = i; newButtonItem2.AutoCollapseOnClick = true ; newButtonItem2.Click += new EventHandler(readLayerInfo); buttonItem4.SubItems.Add(newButtonItem2); //若图层为点图层,则显示为BTNlayer2按钮的子项,因为要求路径点为点图层 if (getshapetype(axmaptemp.get_Layer(i)) == "点") { pointLayer = i; ButtonItem newButtonItem3 = new ButtonItem(); newButtonItem3.Text = axmaptemp.get_Layer(i).Name; newButtonItem3.Tag = i; newButtonItem3.AutoCollapseOnClick = true ; newButtonItem3.Click += new EventHandler(readLayerInfo); BTNlayer2.SubItems.Add(newButtonItem3); } } }● 确定要显示哪些图层的函数,代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 //确定字段的值是不是数字,因为有些功能限制某些字段必为值 public static int ParseFieldType(esriFieldType fieldType) { switch (fieldType) { case esriFieldType.esriFieldTypeInteger: return 2; case esriFieldType.esriFieldTypeDouble: return 1; case esriFieldType.esriFieldTypeSmallInteger: return 2; default : return 0; } }● 读取所选择的图层,代码如下:1 2 3 4 5 6 private void readLayerInfo(object sender, EventArgs e)//读取所选择的图层 {ButtonItem tempButton = (ButtonItem)sender; nowSelectedLayer = (int )tempButton.Tag;getAttributeInfo(sizelist[(int )superTabControl.SelectedTab.Tag]); }● 根据所选择的图层,初始化字段信息,并将其添加到“选择字段”按钮的子项中,代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 //颜色选择器记录列表,记录所有被添加的颜色选择器 List<ColorPickerDropDown> allColor = new List<ColorPickerDropDown>(); int checkboxnum = 0;//记录复选框的编号,为了与颜色选择器对应 List<CheckBoxItem> allcheckbox = new List<CheckBoxItem>(); private void getAttributeInfo(AxMapControl axmaptemp)//获取字段信息 { allColor.Clear();//清空颜色选择器记录列表 allcheckbox.Clear();//清空复选框记录列表 checkboxnum = 0; BTNattribute1.SubItems.Clear();//清空属性列表 buttonItem7.SubItems.Clear(); //获取属性字段 IFeatureLayer pFeatureLayer = axmaptemp.get_Layer(nowSelectedLayer) as IFeatureLayer; IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass; IFeatureCursor pFeatureCursor = pFeatureClass.Search(null , false ); IFeature pFeature = pFeatureCursor.NextFeature(); IFields pFields = pFeatureClass.Fields; for (int k = 0; k < pFields.FieldCount; k++) //获取所有列 { if (ParseFieldType(pFields.get_Field(k).Type) != 0)//只选择值为数字的字段 { CheckBoxItem newcheckbox = new CheckBoxItem();//新建复选框 newcheckbox.Text = pFields.get_Field(k).Name;//赋名字 newcheckbox.AutoCollapseOnClick = false ; ButtonItem newbuttonIn = new ButtonItem(); newbuttonIn.Text = newcheckbox.Text; newcheckbox.Tag = checkboxnum; allcheckbox.Add(newcheckbox); ColorPickerDropDown newColorPicker = new ColorPickerDropDown(); allColor.Add(newColorPicker); newbuttonIn.Tag = checkboxnum; newbuttonIn.Click += new EventHandler(selectedAttribute); newColorPicker.Tag = checkboxnum++; newColorPicker.Symbol = buttonX1.Symbol;//设置颜色选择器的图标 newColorPicker.ShowSubItems = false ; newColorPicker.AutoCollapseOnClick = false ; newColorPicker.AutoExpandOnClick = true ; newColorPicker.SelectedColorChanged += new EventHandler(colorPickerDropDownSelectedColorChanged); newColorPicker.AutoDisposeImages = true ; newColorPicker.SymbolSize = 12; newColorPicker.PopupSide = ePopupSide.Left;//设置颜色选择器向左弹出子菜单 DevComponents.DotNetBar.ItemContainer newItemContainer = new ItemContainer(); newItemContainer.SubItems.Add(newColorPicker); newItemContainer.SubItems.Add(newcheckbox); BTNattribute1.SubItems.Add(newItemContainer); buttonItem7.SubItems.Add(newbuttonIn); }49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 if (m_ipPathFinder == null )//打开几何网络工作空间,为网络分析做准备 { m_ipPathFinder = new ClsPathFinder(); IMap ipMap = axmaptemp.Map; ILayer ipLayer = ipMap.get_Layer(pointLayer); IFeatureLayer ipFeatureLayer = ipLayer as IFeatureLayer; IFeatureDataset ipFDB = ipFeatureLayer.FeatureClass.FeatureDataset; m_ipPathFinder.SetOrGetMap = ipMap; if (m_ipPathFinder.OpenFeatureDatasetNetwork(ipFDB)) ribbonTabItem2.Enabled = true ; else //弹出错误提示 { balloonTip1.SetBalloonText(ribbonControl1, "打开network 出错,不支持网络分析!"); balloonTip1.ShowBalloon(ribbonControl1); } } } }● 读取所选择的字段及对应字段的颜色,代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 List<string > strCollected = new List<string >();//要进行柱状图渲染的字段 int nowSelectedLayer = 0; private void readAttributeInfo(object sender, EventArgs e)//根据所选择的图层读取该涂层的字段信息 { colorCollection.Clear();//初始化被选择颜色集合 strCollected.Clear();//初始化字段选择集合 for (int i = 0; i < allcheckbox.Count; i++) { if (allcheckbox[i].Checked)//若该字段被选中 { strCollected.Add(allcheckbox[i].Text);//将字段加入字段集合 IRgbColor tempColor = new RgbColor();//临时颜色变量 if (allColor[i].SelectedColor != Color.Empty)//如果设置了颜色,则设为该色 { tempColor.Red = allColor[i].SelectedColor.R; tempColor.Green = allColor[i].SelectedColor.G; tempColor.Blue = allColor[i].SelectedColor.B; } else //否则设为随机深颜色 tempColor = GetRandomDarkColor(); colorCollection.Add(tempColor);//被选择颜色集合加入此色 } } }● 进行柱状图渲染,代码如下:1 2 3 4 5 6 7 8 9 10 11 12 List<IRgbColor> colorCollection = new List<IRgbColor>();//颜色集合 public void BarChartRenderer(ILayer pLayer, List<string > FieldNames)//构造柱状图 { IChartRenderer pChartRenderer = new ChartRendererClass(); IRendererFields pRendererFields = pChartRenderer as IRendererFields; for (int i = 0; i < FieldNames.Count; i++) { pRendererFields.AddField(FieldNames[i], FieldNames[i]);//读取所有被选中的字段 } IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer; IGeoFeatureLayer pGeoFeatureLayer = pFeatureLayer as IGeoFeatureLayer; IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;1314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 int FieldsNum = FieldNames.Count;int[] FieldIndecies = new int[FieldsNum]; for (int i = 0; i < FieldsNum; i++){FieldIndecies[i] = pFeatureClass.FindField(FieldNames[i]);//将字段对应起来if (FieldIndecies[i] == -1){MessageBox.Show("选择的字段名称有错误!");return;}}bool FirstValue = true;double MaxValue = -1;//初始化一个最大值for (int i = 0; i < FieldsNum; i++){IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);IFeature pFeature = pFeatureCursor.NextFeature();while (pFeature != null){double FieldValue = Convert.ToDouble(pFeature.get_Value(FieldIndecies[i]));if (FirstValue)//筛选最大值{MaxValue = FieldValue;FirstValue = false;}else if (FieldValue > MaxValue)MaxValue = FieldValue;pFeature = pFeatureCursor.NextFeature();}}IBarChartSymbol pBarChartSymbol = new BarChartSymbolClass();// 定义并设置渲染类型 pBarChartSymbol.Width = barwidth;//设置条带宽度IChartSymbol pChartSymbol = pBarChartSymbol as IChartSymbol;IMarkerSymbol pMarkerSymbol = pBarChartSymbol as IMarkerSymbol;pChartSymbol.MaxValue = MaxValue;// 设置pChartSymbol的最大值pMarkerSymbol.Size = maxHeighth;// 设置bar的最大高度ISymbolArray pSymbolArray = pBarChartSymbol as ISymbolArray;//给一个bar设置符号 ISimpleFillSymbol pSimpleFillSymbol;IRgbColor pColor;for (int i = 0; i < FieldsNum; i++)//读取各个字段的颜色{pColor = colorCollection[i];pSimpleFillSymbol = new SimpleFillSymbolClass();pSimpleFillSymbol.Color = pColor;pSymbolArray.AddSymbol(pSimpleFillSymbol as ISymbol);}pColor = GetRandomLightColor();//设置背景符号pSimpleFillSymbol = new SimpleFillSymbolClass();pSimpleFillSymbol.Color = pColor;pChartRenderer.BaseSymbol = pSimpleFillSymbol as ISymbol;eOverposter = false;pChartRenderer.ChartSymbol = pChartSymbol;pChartRenderer.CreateLegend();pGeoFeatureLayer.Renderer = pChartRenderer as IFeatureRenderer;sizelist[(int)superTabControl.SelectedTab.Tag].ActiveView.Refresh();}若未定义颜色,则自动生成颜色,代码如下:4 5 6 7 8 91011121314151617181920212223242526272829303132 int nRed, nGreen, nBlue; int redLow = 180;int greenLow = 180;int blueLow = 180;nRed = random.Next(redLow);nGreen = random.Next(greenLow);nBlue = random.Next(blueLow);IRgbColor color = new RgbColor();color.Red = nRed;color.Green = nGreen;color.Blue = nBlue;return color;}public IRgbColor GetRandomLightColor()//生成随机浅色{int nRed, nGreen, nBlue;int high = 255;int Low = 200;nRed = random.Next(Low, high);nGreen = random.Next(Low, high);nBlue = random.Next(Low, high);IRgbColor color = new RgbColor();color.Red = nRed;color.Green = nGreen;color.Blue = nBlue;return color;}柱状图渲染菜单如图所示选择字段并设置颜色生成的柱状图如图所示7 状态条实现与比例尺调整实现● 绑定主地图窗口的鼠标移动代码,代码如下:1 2 3 4 5private void axMapControlOnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e) { if (switch1)//若不是空文档则显示坐标 labelItem3.Text = "X=" + e.mapX.ToString() + " Y=" + e.mapY.ToString(); }● 绑定滑动条值改变代码,代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16int nowvalue = 0;//当前缩放比例 private void SliderRating_ValueChanged(object sender, EventArgs e)//根据滑动条控制缩放比例 { intchange = SliderRating.Value - nowvalue; nowvalue = SliderRating.Value; IActiveView nowactive = sizelist[(int )superTabControl.SelectedTab.Tag].ActiveView; IEnvelope envolop = nowactive.Extent; if (change > 0) for (int n = 0; n < change; n++) envolop.Expand(0.9, 0.9, true ); else for (int n = 0; n < -change; n++) envolop.Expand(1.1, 1.1, true ); sizelist[(int )superTabControl.SelectedTab.Tag].Extent = envolop; nowactive.Refresh(); }● 绑定“恢复最小比例尺”按钮代码,代码如下:1 2 3 4 5private void BTNfullExtent_Click(object sender, EventArgs e)//全屏显示按钮 { sizelist[(int )superTabControl.SelectedTab.Tag].Extent = sizelist[(int )superTabControl.SelectedTab.Tag].FullExtent; }显示鼠标所指的图上坐标,坐标如图所示8 最短路径分析实绑定主地图窗口的鼠标按下代码,代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52private void axMapControlOnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e) { AxMapControl axmaptem = (AxMapControl)sender; else if (BTNstartDraw.Checked == true )//如果“绘制”按钮被按下 { IGeometry g = null ; IEnvelope pEnv; IActiveView pActiveView = axmaptem.ActiveView; IMap pMap = axmaptem.Map; pEnv = axmaptem.TrackRectangle(); if (pEnv.IsEmpty == true )//建立一个10*10的矩形选区 { ESRI.ArcGIS.esriSystem.tagRECT r; r.bottom = e.y + 5; r.top = e.y - 5; r.left = e.x - 5; r.right = e.x + 5; pActiveView.ScreenDisplay.DisplayTransformation.TransformRect(pEnv, ref r, 4); pEnv.SpatialReference = pActiveView.FocusMap.SpatialReference; } g = pEnv as IGeometry; ISelectionEnvironment pSelectionEnv = new SelectionEnvironment();//新建选择环境 IRgbColor pColor = new RgbColor(); pColor.Red = 255;//被选中的点为红色 pSelectionEnv.DefaultColor = pColor; //选择要素,true 表示在选择框里找到一个就停止搜索 axmaptem.Map.SelectByShape(g, pSelectionEnv, true ); axmaptem.Refresh(); IEnumFeatureSetup pEnumFeatureSetup = axmaptem.Map.FeatureSelection as IEnumFeatureSetup; pEnumFeatureSetup.AllFields = true ; IEnumFeature pEnumFeature = pEnumFeatureSetup as IEnumFeature; if (pEnumFeature == null ) return ; IFeature pFeature = pEnumFeature.Next(); //如果选择的要素是点要素 if (pFeature != null && pFeature.Shape.GeometryType == esriGeometryType.esriGeometryPoint) { selectedFeature.Add(pFeature);//加入被选中点集合 axmaptem.Map.ClearSelection();//清除选区 foreach (IFeature featuretemp in selectedFeature)//重新绘制点要素集中所有的点 axmaptem.Map.SelectFeature(axmaptem.get_Layer(pointLayer), featuretemp); } else return ; if (m_ipPoints == null )//新建点集 m_ipPoints = new MultipointClass(); IPoint poi = (IPoint)pFeature.Shape; object o = Type.Missing; m_ipPoints.AddPoint(poi, o, o);//添加该点 m_ipPathFinder.StopPoints = m_ipPoints; } }● 选择权字段,代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42string PofAttribute = "";//最短路径的权字段 private void selectedAttribute(object sender, EventArgs e)//选择最短路径的权字段 { ButtonItem tempbutton = (ButtonItem)sender; PofAttribute = tempbutton.Text; } int LineSymbolWidth = 5; private void BTNshortestWay_Click(object sender, EventArgs e)//构建最短路径 { if (PofAttribute == "") { balloonTip1.SetBalloonText(bar2, "请选择权字段"); balloonTip1.ShowBalloon(bar2); return ; } if (m_ipPathFinder.SetOrGetMap == null ) return ; m_ipPathFinder.SolvePath(PofAttribute);//先解析路径 IPolyline ipPolyResult = m_ipPathFinder.PathPolyLine();//最后返回最短路径 ISimpleLineSymbol pLineSymbol = new SimpleLineSymbolClass();//设置Symbol 属性 if (colorPickerDropDown1.SelectedColor == Color.Empty)//设置颜色 pLineSymbol.Color = GetRandomDarkColor(); else { IRgbColor tempcolor = new RgbColor(); tempcolor.Red = colorPickerDropDown1.SelectedColor.R; tempcolor.Green = colorPickerDropDown1.SelectedColor.G; tempcolor.Blue = colorPickerDropDown1.SelectedColor.B; pLineSymbol.Color = tempcolor; } pLineSymbol.Width = LineSymbolWidth; IElement m_Element = new LineElement(); m_Element.Geometry = ipPolyResult; ILineElement plinelement = (ILineElement)m_Element; plinelement.Symbol = pLineSymbol; IGraphicsContainer m_Container = sizelist[(int )superTabControl.SelectedTab.Tag].Map as IGraphicsContainer; m_Container.AddElement(m_Element, 0); m_ipPoints = null ; m_ipPathFinder.StopPoints = null ; sizelist[(int )superTabControl.SelectedTab.Tag].ActiveView.Refresh(); }● 实现“最短路径”类,代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 public class ClsPathFinder { private IGeometricNetwork m_ipGeometricNetwork; private IMap m_ipMap; private IPointCollection m_ipPoints; private IPointToEID m_ipPointToEID; private double m_dblPathCost = 0; private IEnumNetEID m_ipEnumNetEID_Junctions; private IEnumNetEID m_ipEnumNetEID_Edges; private IPolyline m_ipPolyline; public IMap SetOrGetMap //返回和设置当前地图 {14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 set { m_ipMap = value; } get { return m_ipMap; } } //打开几何数据集的网络工作空间 public bool OpenFeatureDatasetNetwork(IFeatureDataset FeatureDataset) { CloseWorkspace(); if (!InitializeNetworkAndMap(FeatureDataset)) return false ; return true ; } public IPointCollection StopPoints //输入点的集合 { set { m_ipPoints = value; } get { return m_ipPoints; } } public double PathCost //路径成本 { get { return m_dblPathCost; } } public IPolyline PathPolyLine()//返回路径的几何体 { IEIDInfo ipEIDInfo; IGeometry ipGeometry; if (m_ipPolyline != null ) return m_ipPolyline; m_ipPolyline = new PolylineClass(); IGeometryCollection ipNewGeometryColl = m_ipPolyline as IGeometryCollection; ISpatialReference ipSpatialReference = m_ipMap.SpatialReference; IEIDHelper ipEIDHelper = new EIDHelperClass(); ipEIDHelper.GeometricNetwork = m_ipGeometricNetwork; ipEIDHelper.OutputSpatialReference = ipSpatialReference; ipEIDHelper.ReturnGeometries = true ; try { IEnumEIDInfo ipEnumEIDInfo = ipEIDHelper.CreateEnumEIDInfo(m_ipEnumNetEID_Edges); int count = ipEnumEIDInfo.Count; ipEnumEIDInfo.Reset(); for (int i = 0; i < count; i++) { ipEIDInfo = ipEnumEIDInfo.Next(); ipGeometry = ipEIDInfo.Geometry; ipNewGeometryColl.AddGeometryCollection(ipGeometry as IGeometryCollection); } } catch { }7778798081828384858687888990919293949596979899 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 return m_ipPolyline;}public void SolvePath(string WeightName)//解决路径{try{int intEdgeUserClassID;int intEdgeUserID;int intEdgeUserSubID;int intEdgeID;IPoint ipFoundEdgePoint;double dblEdgePercent;ITraceFlowSolverGEN ipTraceFlowSolver =new TraceFlowSolverClass() as ITraceFlowSolverGEN;INetSolver ipNetSolver = ipTraceFlowSolver as INetSolver;INetwork ipNetwork = m_work;ipNetSolver.SourceNetwork = ipNetwork;INetElements ipNetElements = ipNetwork as INetElements;int intCount = m_ipPoints.PointCount;IEdgeFlag[] pEdgeFlagList = new EdgeFlagClass[intCount];//定义一个边线旗数组for (int i = 0; i < intCount; i++){INetFlag ipNetFlag = new EdgeFlagClass() as INetFlag;IPoint ipEdgePoint = m_ipPoints.get_Point(i);m_ipPointToEID.GetNearestEdge//查找输入点的最近的边线(ipEdgePoint, out intEdgeID, out ipFoundEdgePoint, out dblEdgePercent);ipNetElements.QueryIDs(intEdgeID, esriElementType.esriETEdge,out intEdgeUserClassID, out intEdgeUserID, out intEdgeUserSubID);erClassID = intEdgeUserClassID;erID = intEdgeUserID;erSubID = intEdgeUserSubID;IEdgeFlag pTemp = (IEdgeFlag)(ipNetFlag as IEdgeFlag);pEdgeFlagList[i] = pTemp;}ipTraceFlowSolver.PutEdgeOrigins(ref pEdgeFlagList);INetSchema ipNetSchema = ipNetwork as INetSchema;INetWeight ipNetWeight = ipNetSchema.get_WeightByName(WeightName);INetSolverWeights ipNetSolverWeights = ipTraceFlowSolver as INetSolverWeights;ipNetSolverWeights.FromToEdgeWeight = ipNetWeight;//开始边线的权重ipNetSolverWeights.ToFromEdgeWeight = ipNetWeight;//终止边线的权重object[] vaRes = new object[intCount - 1];ipTraceFlowSolver.FindPath(esriFlowMethod.esriFMConnected, esriShortestPathObjFn.esriSPObjFnMinSum, out m_ipEnumNetEID_Junctions, out m_ipEnumNetEID_Edges, intCount - 1, ref vaRes); m_dblPathCost = 0;for (int i = 0; i < vaRes.Length; i++)//计算元素成本{double m_Va = (double)vaRes[i];m_dblPathCost = m_dblPathCost + m_Va;}m_ipPolyline = null;}catch { }}//初始化几何网络和地图private bool InitializeNetworkAndMap(IFeatureDataset FeatureDataset){IFeatureClassContainer ipFeatureClassContainer;IFeatureClass ipFeatureClass;IGeoDataset ipGeoDataset;。