||
所要实现的功能:
(1)鼠标点击图层,拖动图层调整显示顺序;
(2)鼠标右键图层,弹出右键菜单,实现“打开属性表”、“删除图层”、“缩放至图层”等功能。
1、定义TOC中鼠标down和up的事件变量
ITOCControlEvents_OnMouseDownEvent ted; public ITOCControlEvents_OnMouseDownEvent Ted { get { return ted; } set { ted = value; } } ITOCControlEvents_OnMouseUpEvent teu; public ITOCControlEvents_OnMouseUpEvent Teu { get { return teu; } set { teu = value; } }
2、定义TOC中鼠标操作的方法
ILayer pMovelayer; int toIndex; public void AdjLayMd() { esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone; if (ted.button == 1) { IBasicMap map = null; ILayer layer = null; object other = null; object index = null; axTOCControl1.HitTest(ted.x, ted.y, ref item, ref map, ref layer, ref other, ref index); if (item == esriTOCControlItem.esriTOCControlItemLayer) { if (layer is IAnnotationSublayer) return; else { pMovelayer = layer; } } } //鼠标右键按下 else if (ted.button == 2) { if (axMapControl1.LayerCount > 0) //主视图中有地理数据 { return; } } } public void AdjLayMu() { if (teu.button == 1) { esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone; IBasicMap map = null; ILayer layer = null; object other = null; object index = null; axTOCControl1.HitTest(teu.x, teu.y, ref item, ref map, ref layer, ref other, ref index); IMap pMap = axMapControl1.ActiveView.FocusMap; if (item == esriTOCControlItem.esriTOCControlItemLayer || layer != null) { if (pMovelayer != null)//!=null?? { ILayer pTempLayer; for (int i = 0; i < pMap.LayerCount; i++) { pTempLayer = pMap.get_Layer(i); if (pTempLayer == layer) toIndex = i;//获取鼠标点击位置的图层索引号 } pMap.MoveLayer(pMovelayer, toIndex); axMapControl1.ActiveView.Refresh(); axTOCControl1.Update(); } } } }
3、TOC事件代码中调用该方法
private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e) { ted = e; AdjLayMd(); } private void axTOCControl1_OnMouseUp(object sender, ITOCControlEvents_OnMouseUpEvent e) { teu = e; AdjLayMu(); }
TOC中右键菜单的设计步骤:
4、在TOC中添加一个contxtmenustrip控件,设计几个菜单,如打开属性表、删除图层等。
在TOC的onmousedown中添加代码,调出这个控件。
ILayer SelectedLayer_TOC; private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e) { ted = e; AdjLayMd(); if (e.button == 2) { ESRI.ArcGIS.Controls.esriTOCControlItem Item = ESRI.ArcGIS.Controls.esriTOCControlItem.esriTOCControlItemNone; IBasicMap pBasicMap = null; ILayer pLayer = null; object other = null; object index = null; axTOCControl1.HitTest(e.x, e.y, ref Item, ref pBasicMap, ref pLayer, ref other, ref index); //实现赋值 if (Item == esriTOCControlItem.esriTOCControlItemLayer)//点击的是图层的话,就显示右键菜单 { SelectedLayer_TOC = pLayer; contextMenuStrip1.Show(axTOCControl1, new System.Drawing.Point(e.x, e.y)); //显示右键菜单,并定义其相对控件的位置,正好在鼠标出显示 } } } 5、删除图层的代码 private void 删除图层ToolStripMenuItem_Click(object sender, EventArgs e) { if (SelectedLayer_TOC != null) { axMapControl1.Map.DeleteLayer(SelectedLayer_TOC); axMapControl2.Map.DeleteLayer(SelectedLayer_TOC); SelectedLayer_TOC = null; } }
6、添加一个新的windows窗体GeoMapAttribute,在上面添加个datagridview控件,用来显示图层属性,Modifiers为public。form1.cs中打开属性表的代码如下:
private void 打开属性表ToolStripMenuItem_Click(object sender, EventArgs e) { GeoMapAttribute newMapAttribute = new GeoMapAttribute(SelectedLayer_TOC); newMapAttribute.Show(); }
7、GeoMapAttribute.cs 中构造函数
public GeoMapAttribute(ILayer pLyr) { ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop); InitializeComponent(); pLayer = pLyr; }
8、定义几个变量
private ILayer pLayer;//打开属性表的图层 private IFeatureLayer pFeatureLayer; private IFeatureClass pFeatureClass; private ILayerFields pLayerFields;
9、在新的windows窗体GeoMapAttribute的load事件中添加如下代码,打开属性表。
private void GeoMapAttribute_Load(object sender, EventArgs e) { try { pFeatureLayer = pLayer as IFeatureLayer; pFeatureClass = pFeatureLayer.FeatureClass; pLayerFields = pFeatureLayer as ILayerFields; DataSet ds = new DataSet("dsTest"); DataTable dt = new DataTable(pFeatureLayer.Name); DataColumn dc = null; for (int i = 0; i < pLayerFields.FieldCount; i++) { dc = new DataColumn(pLayerFields.get_Field(i).Name); dt.Columns.Add(dc); dc = null; } IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false); IFeature pFeature = pFeatureCursor.NextFeature(); while (pFeature != null) { DataRow dr = dt.NewRow(); for (int j = 0; j < pLayerFields.FieldCount; j++) { if (pLayerFields.FindField(pFeatureClass.ShapeFieldName) == j) { dr[j] = pFeatureClass.ShapeType.ToString(); } else { dr[j] = pFeature.get_Value(j); } } dt.Rows.Add(dr); pFeature = pFeatureCursor.NextFeature(); } dataGridView1.DataSource = dt; } catch (Exception exc) { MessageBox.Show("读取属性表失败:" + exc.Message); this.Dispose(); } }
10、缩放至图层
private void 缩放至本图层ToolStripMenuItem_Click(object sender, EventArgs e)
{
IGeoDataset pGeoDataset = SelectedLayer_TOC as IGeoDataset;
IEnvelope pEnvelope = pGeoDataset.Extent;
axMapControl1.Extent = pEnvelope;
}
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-9-20 17:01
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社