Form1.cs
上传用户:zhkydz
上传日期:2018-03-23
资源大小:88k
文件大小:24k
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- //// ESRI
- using ESRI.ArcGIS.Carto;
- using ESRI.ArcGIS.Geometry;
- using ESRI.ArcGIS.Display;
- using ESRI.ArcGIS.Controls;
- using ESRI.ArcGIS.DataSourcesFile;
- using ESRI.ArcGIS.Geodatabase;
- using ESRI.ArcGIS.SystemUI;
- using ESRI.ArcGIS.Framework;
- using ESRI.ArcGIS.DisplayUI;
- using ESRI.ArcGIS.Utility.BaseClasses;
- using ESRI.ArcGIS.CartoUI;
- using ESRI.ArcGIS.esriSystem;
- namespace ArcGisEngineNewProject
- {
- public partial class Form1 : Form
- {
- private int x=0, y=0;
- private IToolbarMenu mapMenu = null;
- private ITOCControl mTOCControl;
- private ILayer pMoveLayer;
- private int Toindex;
- //声明变量moveLayer,用于判断移动图层操作的顺序,0为不可移动,-4选择移动图层,4为选择移动位置
- private static int moveLayer = 0;
- //更改图层的图例样式,changeLegend为4时更改,为0时不能
- private static int changeLegend = 0;
- //删除选中图层,deleteLayer为4时可删除图层,为0时不能
- private static int deleteLayer = 0;
- //点击地图要素,显示相关属性,viewFeature为4时可以查看要素属性,为0时不能
- private static int viewFeature = 0;
- //======================================================
- public Form1()
- {
- InitializeComponent();
- this.mapMenu = new ToolbarMenu();
- this.mapMenu.SetHook(this.myMapControl1);
- }
- //鹰眼功能中当地图恢复时刷新控件
- private void myMapControl1_OnExtentUpdated(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnExtentUpdatedEvent e)
- {
- //当主地图窗口更新后,更新鹰眼控件地图
- IMap pMap;
- pMap = myMapControl1.Map;
- int i;
- for (i = 0; i <= pMap.LayerCount - 1; i++)
- {
- myMapControl2.Map.AddLayer(pMap.get_Layer(i));
- }
- //得到新范围
- IEnvelope pEnv;
- pEnv = e.newEnvelope as IEnvelope;
- IGraphicsContainer pGraphicsContainer;
- IActiveView pActiveView;
- pGraphicsContainer = myMapControl2.Map as IGraphicsContainer;
- pActiveView = pGraphicsContainer as IActiveView;
- //在绘制新的矩形框前,清除Map对象中的任何图形元素
- pGraphicsContainer.DeleteAllElements();
- IRectangleElement pRectangleEle;
- pRectangleEle = new RectangleElementClass();
- IElement pEle;
- pEle = pRectangleEle as IElement;
- pEle.Geometry = pEnv;
- IRgbColor pColor;
- pColor = new RgbColorClass();
- pColor.RGB = 255;
- pColor.Transparency = 255;
- //产生一个线符号对象
- ILineSymbol pOutline;
- pOutline = new SimpleLineSymbolClass();
- //设置线符号的属性
- pOutline.Width = 1;
- pOutline.Color = pColor;
- //设置颜色属性
- pColor = new RgbColorClass();
- pColor.RGB = 255;
- pColor.Transparency = 0;
- //设置填充符号的属性
- IFillSymbol pFillSymbol;
- pFillSymbol = new SimpleFillSymbolClass();
- pFillSymbol.Color = pColor;
- pFillSymbol.Outline = pOutline;
- IFillShapeElement pFillshapeEle;
- pFillshapeEle = pEle as IFillShapeElement;
- pFillshapeEle.Symbol = pFillSymbol;
- pEle = pFillshapeEle as IElement;
- pGraphicsContainer.AddElement(pEle, 0);
- pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
- //drowlittleViewMap(e.newEnvelope as IEnvelope);
- }
- //若图层名称为空,则更名失败
- private void axTOCControl1_OnEndLabelEdit(object sender, ESRI.ArcGIS.Controls.ITOCControlEvents_OnEndLabelEditEvent e)
- {
- string newLabel = e.newLabel;
- if (newLabel.Trim() == "")
- {
- e.canEdit = false;
- }
- }
- //右键添加shp图层
- private void addToolStripMenuItem_Click(object sender, EventArgs e)
- {
- IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
- openFileDialog1.Filter = "(*.shp)|*.shp";
- openFileDialog1.InitialDirectory = @"D:";
- openFileDialog1.Multiselect = false ;
- DialogResult pDialogResult = openFileDialog1.ShowDialog();
- if (pDialogResult != DialogResult.OK)
- return;
- string pPath = openFileDialog1.FileName;
- string pFilePath = System.IO.Path.GetDirectoryName(pPath);
- string pFileName = System.IO.Path.GetFileName(pPath);
- IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(pFilePath , 0);
- IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
- IFeatureClass pFC = pFeatureWorkspace.OpenFeatureClass(pFileName);
- IFeatureLayer pFlayer = new FeatureLayerClass();
- pFlayer.FeatureClass = pFC;
- pFlayer.Name = pFC.AliasName;
- ILayer pLayer = pFlayer as ILayer;
- IMap pMap = myMapControl1.Map;
- pMap.AddLayer(pLayer);
- myMapControl1.ActiveView.Refresh();
- myMapControl2.ActiveView.Refresh();
- }
- //添加lyr图层文件
- private void 添加lyr图层ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
- openFileDialog1.Filter = "(*.lyr)|*.lyr";
- openFileDialog1.InitialDirectory = @"D:";
- openFileDialog1.Multiselect = false;
- DialogResult pDialogResult = openFileDialog1.ShowDialog();
- if (pDialogResult != DialogResult.OK)
- return;
- string pFileName = openFileDialog1.FileName;
- myMapControl1.AddLayerFromFile(pFileName);
- myMapControl1.ActiveView.Refresh();
- myMapControl2.ActiveView.Refresh();
- }
- //右键删除图层
- private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
- {
- deleteLayer = 4;
- MessageBox.Show("请选择要删除的图层!", "删除图层");
- }
- //改变图层样式
- private void changestyleToolStripMenuItem_Click(object sender, EventArgs e)
- {
- changeLegend = 4;
- MessageBox.Show ("请选择要更改样式的图层!", "样式更改");
-
- }
- //全部删除图层
- private void removeToolStripMenuItem_Click(object sender, EventArgs e)
- {
- IMap myMap = myMapControl1.Map;
- myMap.ClearLayers();
- axTOCControl1.Update();
- myMapControl1.ActiveView.Refresh();
- }
- //TOCControl单击事件:出现快捷菜单,更改图例样式,删除图层
- private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
- {
- x = e.x;
- y = e.y;
- //单击鼠标左键
- if (e.button == 1)
- {
- esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
- IBasicMap map = null;
- ILayer layer = null;
- object other = null;
- object index = null;
- int pLayerIndex = 0;
- //返回单击在TOCControl中位置
- axTOCControl1.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index);
- IMap pMap = axTOCControl1.ActiveView.FocusMap;
- //更改图层图例
- if(changeLegend ==4)
- {
- //判断是否单击图层或图例
- if (item == esriTOCControlItem.esriTOCControlItemLegendClass || item ==esriTOCControlItem .esriTOCControlItemLayer )
- {
- ILayer pTempLayer;
- for (int i = 0; i < pMap.LayerCount; i++)
- {
- pTempLayer = pMap.get_Layer(i);
- if (pTempLayer == layer)
- {
- pLayerIndex = i;
- break;
- }
- }
- //声明要更改图例的图层位置
- ISymbolSelector pSymbolSelector;
- pSymbolSelector = new SymbolSelectorClass();
- IGeoFeatureLayer pGeoFeatureLayer;
- pGeoFeatureLayer = this.myMapControl1.get_Layer(pLayerIndex) as IGeoFeatureLayer;
- //要更改图层图例的图例类型的类的定义
- IFeatureLayer pFeatureLayer;
- pFeatureLayer = this.myMapControl1.get_Layer(pLayerIndex) as IFeatureLayer;
- esriGeometryType pType;
- IFeatureClass pFeatureClass;
- pFeatureClass = pFeatureLayer.FeatureClass;
- pType = pFeatureClass.ShapeType;
- //判断图例的类型:点,线,面
- switch (pType)
- {
- //定义点的简单符号样式对话框
- case esriGeometryType.esriGeometryPoint:
- //MessageBox.Show("point");
- ISimpleMarkerSymbol pSimpleMarkerSymbol;
- pSimpleMarkerSymbol = new SimpleMarkerSymbolClass();
- pSymbolSelector.AddSymbol(pSimpleMarkerSymbol as ISymbol);
- break;
- //定义线的简单符号样式对话框
- case esriGeometryType.esriGeometryPolyline:
- //MessageBox.Show("line");
- ISimpleLineSymbol pSimpleLineSymbol;
- pSimpleLineSymbol = new SimpleLineSymbolClass();
- pSymbolSelector.AddSymbol(pSimpleLineSymbol as ISymbol);
- break;
- //定义面得简单符号样式对话框
- case esriGeometryType.esriGeometryPolygon:
- //MessageBox.Show("polygon");
- ISimpleFillSymbol pSimpleFillSymbol;
- pSimpleFillSymbol = new SimpleFillSymbolClass();
- pSymbolSelector.AddSymbol(pSimpleFillSymbol as ISymbol);
- break;
- default:
- MessageBox.Show("未知的图例类型!", "错误");
- return;
- }
- //当选择对话框的图例对象时进入,再更改所在图例样式
- if (pSymbolSelector.SelectSymbol(0))
- {
- ISymbol pSymbol;
- ISimpleRenderer pRenderer;
- pSymbol = pSymbolSelector.GetSymbolAt(0);
- pRenderer = new SimpleRendererClass();
- pRenderer.Symbol = pSymbol;
- pGeoFeatureLayer.Renderer = pRenderer as IFeatureRenderer;
- this.myMapControl1.Refresh();
- this.axTOCControl1.Update();
- pGeoFeatureLayer.GetType();
- }
- changeLegend = 0;
- }
- }
- //删除单个图层
- else if(deleteLayer ==4)
- {
- //确保有项目被选择
- if (item == esriTOCControlItem.esriTOCControlItemMap)
- {
- //ILayer l = null;
- if (MessageBox.Show("确实要删除地图 " + map.Name+" 吗?", "删除地图", MessageBoxButtons.YesNo) == DialogResult.Yes)
- {
- pMap.ClearLayers();
- myMapControl1.ActiveView.Refresh();
- mTOCControl.Update();
- }
- }
- else if (item == esriTOCControlItem.esriTOCControlItemLayer)
- {
- if (MessageBox.Show("确实要删除图层 " + layer.Name+" 吗?", "删除图层", MessageBoxButtons.YesNo) == DialogResult.Yes)
- {
- pMap.DeleteLayer(layer);
- myMapControl1.ActiveView.Refresh();
- mTOCControl.Update();
- }
- }
- else
- MessageBox.Show("未选择任何内容!","错误");
- deleteLayer = 0;
- }
- }
- else if (e.button == 2)//右键弹出菜单
- {
- this.mapMenu.PopupMenu(e.x, e.y, this.axTOCControl1.hWnd);
- }
- }
- //载入窗体,向ToolbarControl加载按钮,设置ToolbarControl和TOCControl与MapControl的关联
- private void Form1_Load(object sender, EventArgs e)
- {
- //关联控件
- axTOCControl1.SetBuddyControl(myMapControl1);
- myToolbarControl.SetBuddyControl(myMapControl1);
- //添加toolbar控件按钮
- string uID = null;
- uID = "esriControlTools.ControlsOpenDocCommand";
- myToolbarControl.AddItem(uID, -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
- uID = "esriControlTools.ControlsAddDataCommand";
- myToolbarControl.AddItem(uID, -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
-
- uID = "esriControlTools.ControlsMapZoomInTool";
- myToolbarControl.AddItem(uID, -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
- uID = "esriControlTools.ControlsMapZoomOutTool";
- myToolbarControl.AddItem(uID, -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
- uID = "esriControlTools.ControlsMapZoomInFixedCommand";
- myToolbarControl.AddItem(uID, -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
- uID = "esriControlTools.ControlsMapZoomOutFixedCommand";
- myToolbarControl.AddItem(uID, -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
- uID = "esriControlTools.ControlsMapPanTool";
- myToolbarControl.AddItem(uID, -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
- uID = "esriControlTools.ControlsMapFullExtentCommand";
- myToolbarControl.AddItem(uID, -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
- uID = "esriControlTools.ControlsMapZoomToLastExtentBackCommand";
- myToolbarControl.AddItem(uID, -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
- uID = "esriControlTools.ControlsMapZoomToLastExtentForwardCommand";
- myToolbarControl.AddItem(uID, -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
- uID = "esriControlTools.ControlsMapIdentifyTool";
- myToolbarControl.AddItem(uID, -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
- uID = "esriControlTools.ControlsSelectFeaturesTool";
- myToolbarControl.AddItem(uID, -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
- }
- //窗体卸载时调用Engine关闭函数(卸载内存中内容,以免内存出错,但不知为什么必须得调用)
- private void Form1_FormClosed(object sender, FormClosedEventArgs e)
- {
- ESRI.ArcGIS.ADF.COMSupport.AOUninitialize.Shutdown();
- }
- //快捷菜单右键移动图层
- private void movelayerToolStripMenuItem_Click(object sender, EventArgs e)
- {
- moveLayer = -4;
- MessageBox.Show("请选择要移动的图层!", "调整图层顺序");
-
- }
- //鼠标弹起事件
- private void axTOCControl1_OnMouseUp(object sender, ITOCControlEvents_OnMouseUpEvent e)
- {
- if (e.button == 1)//单击鼠标左键
- {
- if (moveLayer == -4)//调整图层顺序
- {
- mTOCControl = axTOCControl1.Object as ITOCControl;
- esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
- IBasicMap map = null;
- ILayer layer = null;
- object other = null;
- object index = null;
- mTOCControl.HitTest(x, y, ref item, ref map, ref layer, ref other, ref index);
- if (item == esriTOCControlItem.esriTOCControlItemLayer)
- {
- if (layer is IAnnotationSublayer)
- {
- return;
- }
- else
- {
- pMoveLayer = layer;
- MessageBox.Show("图层:" + layer.Name + " 被选中nn请单击图层目的位置!", "调整图层顺序");
- }
- }
- moveLayer = 4;//将图层调整进行到第2步,选择移动到得目的位置
- }
- else if (moveLayer == 4)//调整图层顺序
- {
- esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
- IBasicMap map = null;
- ILayer layer = null;
- object other = null;
- object index = null;
- mTOCControl.HitTest(x, y, ref item, ref map, ref layer, ref other, ref index);
- IMap pMap = axTOCControl1.ActiveView.FocusMap;
- if (item == esriTOCControlItem.esriTOCControlItemLayer || layer != null)
- {
- if (pMoveLayer != layer)
- {
- ILayer pTempLayer;
- for (int i = 0; i < pMap.LayerCount; i++)
- {
- pTempLayer = pMap.get_Layer(i);
- if (pTempLayer == layer)
- {
- Toindex = i;
- }
- }
- pMap.MoveLayer(pMoveLayer, Toindex);
- myMapControl1.ActiveView.Refresh();
- mTOCControl.Update();
-
- MessageBox.Show("图层顺序调整成功!","调整图层顺序");
- }
- }
- moveLayer = 0;//图层调整完后,将图层调整事件重新关闭
- }
- }
- }
- //编辑图层名称
- private void axTOCControl1_OnBeginLabelEdit(object sender, ITOCControlEvents_OnBeginLabelEditEvent e)
- {
- IBasicMap map = null;
- ILayer layer = null;
- object other = null;
- object index = null;
- esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
- //Determine what kind of item has been clicked on
- axTOCControl1.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index);
- //Only layer items can have their labels edited
- if (item != esriTOCControlItem.esriTOCControlItemLayer)
- {
- e.canEdit = false;
- }
- }
- //查看要素属性信息
- private void viewInfToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (viewFeature == 0)
- {
- viewFeature = 4;
- myToolbarControl.CurrentTool = null;
- myToolbarControl.Enabled = false;
- viewInfToolStripMenuItem.Text = "停止查看属性";
- myMapControl1.MousePointer = esriControlsMousePointer.esriPointerCustom;
- MessageBox.Show("看完后,记得右键‘停止查看属性’哦!", "提示");
- }
- else
- {
- viewFeature = 0;
- myToolbarControl.Enabled = true;
- viewInfToolStripMenuItem.Text = "查看属性信息";
- }
- }
- //控件随着窗体的大小更改而改变
- private void Form1_Resize(object sender, EventArgs e)
- {
- //随着主窗体大小的改变,其中的控件大小也随着改变
- this.myMapControl1.Width = this .Width- this.myMapControl1 .Left-10 ;
- this.myMapControl1.Height =this.Height - this.myToolbarControl.Height ;
- this.axTOCControl1.Height = this.Height - this.myMapControl2.Height-this.myToolbarControl .Height-5 ;
- this.myMapControl2.Top = this.myMapControl1.Top + this.myMapControl1.Height - this.myMapControl2.Height;
- this.myMapControl2.Width = this.axTOCControl1.Width;
- }
- //myMapControl1中点击要素,则显示要素属性
- private void myMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
- {
- if (e.button == 1)
- {
- if (viewFeature == 4)
- {
- //myMapControl1.MousePointer = esriControlsMousePointer.esriPointerArrow;
- IActiveView activeview = myMapControl1.ActiveView;
- IEnumLayer pEnumLayer;
- IIdentifyDialogProps pIdentifyDialogProps;
- IIdentifyDialog pIdentifyDialog = new IdentifyDialogClass();
- pIdentifyDialog.Map = myMapControl1.Map;
- pIdentifyDialog.Display = activeview.ScreenDisplay;
- pIdentifyDialog.ClearLayers();
- pIdentifyDialogProps = pIdentifyDialog as IIdentifyDialogProps;
- pEnumLayer = pIdentifyDialogProps.Layers;
- pEnumLayer.Reset();
- ILayer pLayer = pEnumLayer.Next();
- while (pLayer != null)
- {
- pIdentifyDialog.AddLayerIdentifyPoint(pLayer, e.x, e.y);
- pLayer = pEnumLayer.Next();
- }
- pIdentifyDialog.Show();
- }
- }
- else if (e.button == 2)
- {
- }
- }
- //退出程序
- private void exitToolStripMenuItem_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- }
- }