主要实现了一下几点:
1.加载mxd文件同步;OnMapReplaced事件
2.加载图层文件同步;OnItemAdded事件
3.TOC中图层勾选状态与鹰眼中图层可见状态同步;ContentsChanged事件
4.鹰眼视图中右键框选主视图中显示区域;
5.鹰眼视图中左键单击确定矩形框中心;
6.TOC右键菜单删除图层;
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 System.Diagnostics;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Controls;
namespace DEMO1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
IActiveViewEvents_Event pAE;
private void axMapControl1_OnMapReplaced(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMapReplacedEvent e)
{
axMapControl2.ClearLayers();//每次主视图地图更换时,清空鹰眼中原有图层
IMap pMap = e.newMap as IMap;//参数e是object类型,要进行接口转换
int n = pMap.LayerCount;
for (int i = n - 1; i >= 0; i--)
{
axMapControl2.AddLayer(pMap.get_Layer(i));
}
this.pAE = e.newMap as IActiveViewEvents_Event;
pAE.ItemAdded += new IActiveViewEvents_ItemAddedEventHandler(this.OnItemAdded);
pAE.ContentsChanged += new IActiveViewEvents_ContentsChangedEventHandler(pAE_ContentsChanged);
}
void pAE_ContentsChanged()
{
IBasicMap map = null;
ILayer layer = null;
Object other = null;
Object index = null;
esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
axTOCControl1.GetSelectedItem(ref item, ref map, ref layer, ref other, ref index);
ILayer player;
for (int i = 0; i < axMapControl2.LayerCount; i++)
{
player = axMapControl2.get_Layer(i);
if (player.Name == layer.Name)
{
if (layer.Visible == true)
player.Visible = true;
else
player.Visible = false;
axMapControl2.Refresh();
break;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
//用代码设置伙伴控件更保险
axTOCControl1.SetBuddyControl(axMapControl1);
axToolbarControl1.SetBuddyControl(axMapControl1);
//禁用鹰眼MapControl的滚轮放大缩小功能
axMapControl2.AutoMouseWheel = false;
IMap pMap;
pMap = axMapControl1.Map;//主视图
pAE = pMap as IActiveViewEvents_Event;
pAE.ItemAdded += new IActiveViewEvents_ItemAddedEventHandler(this.OnItemAdded);
pAE.ContentsChanged+=new IActiveViewEvents_ContentsChangedEventHandler(pAE_ContentsChanged);
}
private void OnItemAdded(object item)
{
ILayer pLayer;
pLayer = item as ILayer;
axMapControl2.AddLayer(pLayer,0);//鹰眼视图
axMapControl2.Refresh();
}
private void axMapControl1_OnExtentUpdated(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnExtentUpdatedEvent e)
{
try
{
ESRI.ArcGIS.Geometry.IEnvelope envelope = (ESRI.ArcGIS.Geometry.IEnvelope)e.newEnvelope;
ESRI.ArcGIS.Carto.IGraphicsContainer gc = axMapControl2.Map as ESRI.ArcGIS.Carto.IGraphicsContainer;
ESRI.ArcGIS.Carto.IActiveView ac = gc as ESRI.ArcGIS.Carto.IActiveView;
//在绘制前,清除axMapControl2中的内容
gc.DeleteAllElements();
ESRI.ArcGIS.Carto.IElement elment = new ESRI.ArcGIS.Carto.RectangleElementClass();
elment.Geometry = envelope;
//设置鹰眼中的红线
//产生一个线符号对象
ESRI.ArcGIS.Display.ILineSymbol outLineSymbol = new ESRI.ArcGIS.Display.SimpleLineSymbolClass();
outLineSymbol.Width = 2;
outLineSymbol.Color = GetColor(255, 0, 0, 255);
//设置颜色属性
//设置填充符号的属性
ESRI.ArcGIS.Display.IFillSymbol fillsymbol = new ESRI.ArcGIS.Display.SimpleFillSymbolClass();
fillsymbol.Color = GetColor(9, 0, 0, 0);
fillsymbol.Outline = outLineSymbol;
ESRI.ArcGIS.Carto.IFillShapeElement fillshapeelement = elment as ESRI.ArcGIS.Carto.IFillShapeElement;
fillshapeelement.Symbol = fillsymbol;
gc.AddElement((ESRI.ArcGIS.Carto.IElement)fillshapeelement, 0);
ac.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGraphics, null, null);
}
catch(Exception eCatch)
{
MessageBox.Show(eCatch.ToString()+"\n鹰眼中绘制红色矩形框出错");
}
}
private ESRI.ArcGIS.Display.IRgbColor GetColor(int r, int g, int b, int t)
{
ESRI.ArcGIS.Display.IRgbColor rgb = new ESRI.ArcGIS.Display.RgbColorClass();
rgb.Red = r;
rgb.Green = g;
rgb.Blue = b;
rgb.Transparency = (byte)t;
return rgb;
}
//右键画矩形选定主视图显示范围
private void axMapControl2_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
{
if (e.button == 2)
{
try
{
IPoint pPoint =new PointClass();
pPoint.X = e.mapX;
pPoint.Y = e.mapY;
IEnvelope pEnvelop = axMapControl2.TrackRectangle();
axMapControl1.Extent = pEnvelop;
}
catch
{
}
}
}
private void axMapControl2_OnMouseMove(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseMoveEvent e)
{
if (e.button == 1)
{
try
{
IPoint pPoint = new PointClass();
pPoint.X = e.mapX;
pPoint.Y = e.mapY;
IEnvelope pEnvelop = axMapControl1.Extent as IEnvelope;
pEnvelop.CenterAt(pPoint);
IGraphicsContainer pGraphicContainer = axMapControl2.Map as IGraphicsContainer;
IActiveView pAV = pGraphicContainer as IActiveView;
pGraphicContainer.DeleteAllElements();
ESRI.ArcGIS.Carto.IElement elment = new ESRI.ArcGIS.Carto.RectangleElementClass();
elment.Geometry = pEnvelop;
//设置鹰眼中的红线
//产生一个线符号对象
ESRI.ArcGIS.Display.ILineSymbol outLineSymbol = new ESRI.ArcGIS.Display.SimpleLineSymbolClass();
outLineSymbol.Width = 2;
outLineSymbol.Color = GetColor(255, 0, 0, 255);
//设置颜色属性
//设置填充符号的属性
ESRI.ArcGIS.Display.IFillSymbol fillsymbol = new ESRI.ArcGIS.Display.SimpleFillSymbolClass();
fillsymbol.Color = GetColor(9, 0, 0, 0);
fillsymbol.Outline = outLineSymbol;
ESRI.ArcGIS.Carto.IFillShapeElement fillshapeelement = elment as ESRI.ArcGIS.Carto.IFillShapeElement;
fillshapeelement.Symbol = fillsymbol;
pGraphicContainer.AddElement((ESRI.ArcGIS.Carto.IElement)fillshapeelement, 0);
pAV.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGraphics, null, null);
}
catch
{
}
}
}
private void axMapControl2_OnMouseUp(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseUpEvent e)
{
if (e.button == 1)
{
try
{
IPoint pPoint = new PointClass();
pPoint.X = e.mapX;
pPoint.Y = e.mapY;
IEnvelope pEnvelop = axMapControl1.Extent as IEnvelope;
pEnvelop.CenterAt(pPoint);
axMapControl1.Extent = pEnvelop;
}
catch
{
}
}
}
private void 删除图层ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (SelectedLayer != null)
{
axMapControl1.Map.DeleteLayer(SelectedLayer);
axMapControl2.Map.DeleteLayer(SelectedLayer);
SelectedLayer = null;
}
}
ILayer SelectedLayer;
private void axTOCControl1_OnMouseDown(object sender, 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 = layer;
TOCMenuStrip1.Show(axTOCControl1, e.x, e.y);
}
}
}
}
}