基于C#的ArcEngine二次开发教程(08):视图漫游与鹰眼功能的代码实现
作者:互联网
目录
1 准备工作
控件准备:在界面上再拖拽一个MapControl控件,命名为axMapControl2
整体思路:先把图层加载到axMapControl1中,在获取其当前图层,将其显示的axMapControl2中
ps:VS的自动添加名称空间引用
2 代码实现
2.1 打开mxd文档
鹰眼功能只能在打开mxd文档时有效,因此需要使用IMapControl的LoadMxFile函数
代码实现:
private void 鹰眼ToolStripMenuItem_Click(object sender, EventArgs e)
{
axMapControl1.LoadMxFile(@"E:\C#\Example200\AE\AE\data.mxd");
//加载mxd文档
}
2.2 视图平移功能
用到的ITool接口的实现类ContorlsMapPanTool
代码实现
private void 视图漫游ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (axMapControl1.CurrentTool == null)
{
ICommand icc;
ITool tool = new ControlsMapPanToolClass();//视图平移函数
axMapControl1.CurrentTool = tool;
icc = tool as ICommand;
icc.OnCreate(axMapControl1.Object);
icc.OnClick();
}
else
{
axMapControl1.CurrentTool = null;
}
}
2.3 添加鹰眼视图
重写axMapControl1的OnMapReplaced事件:
private void axMapControl1_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e)
{
IMap imap = axMapControl1.Map;
for (int i = 0; i < imap.LayerCount; i++)
{
IObjectCopy obj = new ObjectCopyClass();
object copyLayer = axMapControl1.get_Layer(imap.LayerCount - 1 - i);
object copyLayer2 = obj.Copy(copyLayer);
axMapControl2.Map.AddLayer(copyLayer2 as ILayer);
}
axMapControl2.Extent = axMapControl1.FullExtent;
}
由于ArcGIS不能在两个MapControl之间共享数据,因此需要将axMapControl1中的图层拷贝到axMapControl2中
接口IObjectCopy定义在名称空间using ESRI.ArcGIS.Carto;中,成员及实现类如下:
2.4 为鹰眼视图添加定位矩形框
实现鹰眼中的定位矩形框功能:
private void axMapControl1_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e)
{
IEnvelope pEnv;
pEnv = e.newEnvelope as IEnvelope;
IGraphicsContainer graphicscontainer;
IActiveView activewer;
graphicscontainer = axMapControl2.Map as IGraphicsContainer;
activewer = graphicscontainer as IActiveView;
graphicscontainer.DeleteAllElements();
IElement plement;
plement = new RectangleElementClass();
plement.Geometry = pEnv;
IRgbColor rgbcol = new RgbColorClass();
rgbcol.RGB = 255;
rgbcol.Transparency = 255;
ILineSymbol poutline = new SimpleLineSymbolClass();
poutline.Width = 1;
poutline.Color = rgbcol;
IRgbColor pcolor = new RgbColorClass();
pcolor.RGB = 255;
pcolor.Transparency = 0;
IFillSymbol fillsym = new SimpleFillSymbolClass();
fillsym.Color = pcolor;
fillsym.Outline = poutline;
IFillShapeElement pfillshapeelement;
pfillshapeelement = plement as IFillShapeElement;
pfillshapeelement.Symbol = fillsym;
plement = pfillshapeelement as IElement;
graphicscontainer.AddElement(plement, 0);
activewer.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
代码注释:
接口IEnvelop定义在名称空间(ESRI.ArcGIS.Geometry),继承的接口和实现类如下:
IGraphicsContainer和IActiveView都被类Map实现,可以进行接口跳转。
RectangleElementClass同时实现了接口IFillShapeElement和接口IElement
3 总结
讲述了利用AE进行鹰眼视图和设计和视图漫游功能的代码实现
还存在以下问题:当前的鹰眼视图是可以缩放的,是否可以设置为固定比例尺不可缩放呢?请高手赐教
标签:C#,axMapControl2,axMapControl1,鹰眼,视图,二次开发,new,plement 来源: https://blog.csdn.net/m1m2m3mmm/article/details/94044758