您的位置:首页 > 其它

较为完整的鹰眼实现,跟大家共享一下

2011-07-19 22:35 246 查看
主要实现了一下几点:
1.加载mxd文件同步;OnMapReplaced事件
2.加载图层文件同步;OnItemAdded事件
3.TOC中图层勾选状态与鹰眼中图层可见状态同步;ContentsChanged事件
4.鹰眼视图中右键框选主视图中显示区域;
5.鹰眼视图中左键单击确定矩形框中心;
6.TOC右键菜单删除图层;


usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
usingESRI.ArcGIS.Carto;
usingSystem.Diagnostics;
usingESRI.ArcGIS.Geometry;
usingESRI.ArcGIS.Display;
usingESRI.ArcGIS.Controls;

namespaceDEMO1
{
publicpartialclassForm1:Form
{
publicForm1()
{
InitializeComponent();
}

IActiveViewEvents_EventpAE;

privatevoidaxMapControl1_OnMapReplaced(objectsender,ESRI.ArcGIS.Controls.IMapControlEvents2_OnMapReplacedEvente)
{
axMapControl2.ClearLayers();//每次主视图地图更换时,清空鹰眼中原有图层
IMappMap=e.newMapasIMap;//参数e是object类型,要进行接口转换

intn=pMap.LayerCount;
for(inti=n-1;i>=0;i--)
{
axMapControl2.AddLayer(pMap.get_Layer(i));
}
this.pAE=e.newMapasIActiveViewEvents_Event;
pAE.ItemAdded+=newIActiveViewEvents_ItemAddedEventHandler(this.OnItemAdded);
pAE.ContentsChanged+=newIActiveViewEvents_ContentsChangedEventHandler(pAE_ContentsChanged);
}

voidpAE_ContentsChanged()
{
IBasicMapmap=null;
ILayerlayer=null;
Objectother=null;
Objectindex=null;
esriTOCControlItemitem=esriTOCControlItem.esriTOCControlItemNone;

axTOCControl1.GetSelectedItem(refitem,refmap,reflayer,refother,refindex);

ILayerplayer;
for(inti=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;
}
}
}

privatevoidForm1_Load(objectsender,EventArgse)
{
//用代码设置伙伴控件更保险
axTOCControl1.SetBuddyControl(axMapControl1);
axToolbarControl1.SetBuddyControl(axMapControl1);
//禁用鹰眼MapControl的滚轮放大缩小功能
axMapControl2.AutoMouseWheel=false;

IMappMap;
pMap=axMapControl1.Map;//主视图
pAE=pMapasIActiveViewEvents_Event;
pAE.ItemAdded+=newIActiveViewEvents_ItemAddedEventHandler(this.OnItemAdded);
pAE.ContentsChanged+=newIActiveViewEvents_ContentsChangedEventHandler(pAE_ContentsChanged);
}

privatevoidOnItemAdded(objectitem)
{
ILayerpLayer;
pLayer=itemasILayer;
axMapControl2.AddLayer(pLayer,0);//鹰眼视图
axMapControl2.Refresh();
}

privatevoidaxMapControl1_OnExtentUpdated(objectsender,ESRI.ArcGIS.Controls.IMapControlEvents2_OnExtentUpdatedEvente)
{
try
{
ESRI.ArcGIS.Geometry.IEnvelopeenvelope=(ESRI.ArcGIS.Geometry.IEnvelope)e.newEnvelope;
ESRI.ArcGIS.Carto.IGraphicsContainergc=axMapControl2.MapasESRI.ArcGIS.Carto.IGraphicsContainer;
ESRI.ArcGIS.Carto.IActiveViewac=gcasESRI.ArcGIS.Carto.IActiveView;
//在绘制前,清除axMapControl2中的内容
gc.DeleteAllElements();
ESRI.ArcGIS.Carto.IElementelment=newESRI.ArcGIS.Carto.RectangleElementClass();
elment.Geometry=envelope;
//设置鹰眼中的红线
//产生一个线符号对象
ESRI.ArcGIS.Display.ILineSymboloutLineSymbol=newESRI.ArcGIS.Display.SimpleLineSymbolClass();
outLineSymbol.Width=2;
outLineSymbol.Color=GetColor(255,0,0,255);
//设置颜色属性
//设置填充符号的属性
ESRI.ArcGIS.Display.IFillSymbolfillsymbol=newESRI.ArcGIS.Display.SimpleFillSymbolClass();
fillsymbol.Color=GetColor(9,0,0,0);
fillsymbol.Outline=outLineSymbol;
ESRI.ArcGIS.Carto.IFillShapeElementfillshapeelement=elmentasESRI.ArcGIS.Carto.IFillShapeElement;
fillshapeelement.Symbol=fillsymbol;
gc.AddElement((ESRI.ArcGIS.Carto.IElement)fillshapeelement,0);
ac.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGraphics,null,null);
}
catch(ExceptioneCatch)
{
MessageBox.Show(eCatch.ToString()+"\n鹰眼中绘制红色矩形框出错");
}
}

privateESRI.ArcGIS.Display.IRgbColorGetColor(intr,intg,intb,intt)
{
ESRI.ArcGIS.Display.IRgbColorrgb=newESRI.ArcGIS.Display.RgbColorClass();
rgb.Red=r;
rgb.Green=g;
rgb.Blue=b;
rgb.Transparency=(byte)t;
returnrgb;
}

//右键画矩形选定主视图显示范围
privatevoidaxMapControl2_OnMouseDown(objectsender,ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvente)
{
if(e.button==2)
{
try
{
IPointpPoint=newPointClass();
pPoint.X=e.mapX;
pPoint.Y=e.mapY;

IEnvelopepEnvelop=axMapControl2.TrackRectangle();
axMapControl1.Extent=pEnvelop;
}
catch
{

}
}
}

privatevoidaxMapControl2_OnMouseMove(objectsender,ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseMoveEvente)
{
if(e.button==1)
{
try
{
IPointpPoint=newPointClass();
pPoint.X=e.mapX;
pPoint.Y=e.mapY;

IEnvelopepEnvelop=axMapControl1.ExtentasIEnvelope;
pEnvelop.CenterAt(pPoint);

IGraphicsContainerpGraphicContainer=axMapControl2.MapasIGraphicsContainer;
IActiveViewpAV=pGraphicContainerasIActiveView;
pGraphicContainer.DeleteAllElements();

ESRI.ArcGIS.Carto.IElementelment=newESRI.ArcGIS.Carto.RectangleElementClass();
elment.Geometry=pEnvelop;
//设置鹰眼中的红线
//产生一个线符号对象
ESRI.ArcGIS.Display.ILineSymboloutLineSymbol=newESRI.ArcGIS.Display.SimpleLineSymbolClass();
outLineSymbol.Width=2;
outLineSymbol.Color=GetColor(255,0,0,255);
//设置颜色属性
//设置填充符号的属性
ESRI.ArcGIS.Display.IFillSymbolfillsymbol=newESRI.ArcGIS.Display.SimpleFillSymbolClass();
fillsymbol.Color=GetColor(9,0,0,0);
fillsymbol.Outline=outLineSymbol;
ESRI.ArcGIS.Carto.IFillShapeElementfillshapeelement=elmentasESRI.ArcGIS.Carto.IFillShapeElement;
fillshapeelement.Symbol=fillsymbol;
pGraphicContainer.AddElement((ESRI.ArcGIS.Carto.IElement)fillshapeelement,0);
pAV.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGraphics,null,null);
}
catch
{

}
}
}

privatevoidaxMapControl2_OnMouseUp(objectsender,ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseUpEvente)
{
if(e.button==1)
{
try
{
IPointpPoint=newPointClass();
pPoint.X=e.mapX;
pPoint.Y=e.mapY;

IEnvelopepEnvelop=axMapControl1.ExtentasIEnvelope;
pEnvelop.CenterAt(pPoint);
axMapControl1.Extent=pEnvelop;
}
catch
{

}
}
}

privatevoid删除图层ToolStripMenuItem_Click(objectsender,EventArgse)
{
if(SelectedLayer!=null)
{
axMapControl1.Map.DeleteLayer(SelectedLayer);
axMapControl2.Map.DeleteLayer(SelectedLayer);
SelectedLayer=null;
}
}

ILayerSelectedLayer;
privatevoidaxTOCControl1_OnMouseDown(objectsender,ITOCControlEvents_OnMouseDownEvente)
{
if(e.button==2)
{
IBasicMapmap=null;
ILayerlayer=null;
Objectother=null;
Objectindex=null;
esriTOCControlItemitem=esriTOCControlItem.esriTOCControlItemNone;

axTOCControl1.HitTest(e.x,e.y,refitem,refmap,reflayer,refother,refindex);

if(item==esriTOCControlItem.esriTOCControlItemLayer)
{
SelectedLayer=layer;
TOCMenuStrip1.Show(axTOCControl1,e.x,e.y);
}
}
}
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐