------------------------------------------------------------------武汉理工大学 GIS 专业
实验二:右键删除图层
1)Form1.cs 代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Controls;
namespace 右键删除图层
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
axTOCControl1.SetBuddyControl(axMapControl1);
axToolbarControl1.SetBuddyControl(axMapControl1);//Form1中绑定可以不设置,但
必须在代码中添加这两段话,才能起同步作用。
}
private void axMapControl1_OnMapReplaced(object sender,
ESRI.ArcGIS.Controls.IMapControlEvents2_OnMapReplacedEvent e)
{
}
IMap pMap;
pMap = axMapControl1.Map;
for (int i = 0; i < pMap.LayerCount; i++)
{
axMapControl2.Map.AddLayer(pMap.get_Layer(i));
}
private void axMapControl2_OnMouseMove(object sender,
ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseMoveEvent e)
{
if (e.button != 1)
return;
IPoint pPoint = new PointClass();
pPoint.X = e.mapX;
pPoint.Y = e.mapY;
axMapControl1.CenterAt(pPoint);
axMapControl2.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,
null, null);
}
private void axMapControl1_OnExtentUpdated(object sender,
ESRI.ArcGIS.Controls.IMapControlEvents2_OnExtentUpdatedEvent e)
{
IGraphicsContainer pGC = axMapControl2.Map as IGraphicsContainer;
IActiveView pAV = pGC as IActiveView;
pGC.DeleteAllElements();
IRectangleElement pRE = new RectangleElementClass();
IElement pEle = pRE as IElement;
IEnvelope pEnvelope = e.newEnvelope as IEnvelope;
pEle.Geometry = pEnvelope;
IRgbColor pColor = new RgbColorClass();
pColor.Red = 200;
pColor.Green = 0;
pColor.Blue = 0;
pColor.Transparency = 255;
ILineSymbol pLineSymbol = new SimpleLineSymbolClass();
pLineSymbol.Width = 2;
pLineSymbol.Color = pColor;
IFillSymbol pFillSymbol = new SimpleFillSymbolClass();
pColor.Transparency = 0;
pFillSymbol.Color = pColor;
pFillSymbol.Outline = pLineSymbol;
IFillShapeElement pFillShapeElement = pRE as IFillShapeElement;
pFillShapeElement.Symbol = pFillSymbol;
pGC.AddElement(pEle, 0);
axMapControl2.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,
null, null);
}
private void axMapControl1_OnMouseDown(object sender,
ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
{
if (e.button == 1)
{
IPoint pPoint = new PointClass();
pPoint.X = e.mapX;
pPoint.Y = e.mapY;
IEnvelope pEnvelope = axMapControl1.Extent as IEnvelope;
pEnvelope.CenterAt(pPoint);
axMapControl1.Extent = pEnvelope;
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
}
else if (e.button == 2)
{
IEnvelope pEnvelope = axMapControl2.TrackRectangle();
axMapControl1.Extent = pEnvelope;
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
}
}
ILayer SelectedLayer_TOC = null;
private void axTOCControl1_OnMouseDown(object sender,
ESRI.ArcGIS.Controls.ITOCControlEvents_OnMouseDownEvent e)
{
if (e.button == 2)
{
IBasicMap map = null;
ILayer layer = null;
Object other = null;
Object index = null;
esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
axTOCControl1.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index);
if (item == esriTOCControlItem.esriTOCControlItemLayer)
{
SelectedLayer_TOC = layer;
TOCMenuStrip1.Show(axTOCControl1, e.x, e.y);
}
}
}
private void RemoveLayerMenuItem_Click(object sender, EventArgs e)
{
if (SelectedLayer_TOC != null)
{
axMapControl1.Map.DeleteLayer(SelectedLayer_TOC);
axMapControl2.Map.DeleteLayer(SelectedLayer_TOC);
SelectedLayer_TOC = null;
}
}
}
}
2)Program.cs 代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace 右键删除图层
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}