大家好,又见面了,我是你们的朋友全栈君。
1、添加程序集
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
*若出现“命名空间ESRI.ArcGIS中不存在类型或命名空间”的错误
解决方法如下:
右击解决方案中的引用,打开添加引用
选择缺少的程序集,点击确定
2、添加定义声明变量,添加SynchronizeEagleEye方法、DrawRectangle方法、GetRgbColor方法代码
//定义声明变量
IEnvelope pEnv;
private void SynchronizeEagleEye()
{
if (EagleEyeMapControl.LayerCount > 0)
{
EagleEyeMapControl.ClearLayers();
}
//设置鹰眼和主地图的坐标系统一致
EagleEyeMapControl.SpatialReference = mainMapControl.SpatialReference;
for (int i = mainMapControl.LayerCount - 1; i >= 0; i--)
{
//使鹰眼视图与数据视图的图层上下顺序保持一致
ILayer pLayer = mainMapControl.get_Layer(i);
if (pLayer is IGroupLayer || pLayer is ICompositeLayer)
{
ICompositeLayer pCompositeLayer = (ICompositeLayer)pLayer;
for (int j = pCompositeLayer.Count - 1; j >= 0; j--)
{
ILayer pSubLayer = pCompositeLayer.get_Layer(j);
IFeatureLayer pFeatureLayer = pSubLayer as IFeatureLayer;
if (pFeatureLayer != null)
{
//由于鹰眼地图较小,所以过滤点图层不添加
if (pFeatureLayer.FeatureClass.ShapeType != esriGeometryType.esriGeometryPoint
&& pFeatureLayer.FeatureClass.ShapeType != esriGeometryType.esriGeometryMultipoint)
{
EagleEyeMapControl.AddLayer(pLayer);
}
}
}
}
else
{
IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
if (pFeatureLayer != null)
{
if (pFeatureLayer.FeatureClass.ShapeType != esriGeometryType.esriGeometryPoint
&& pFeatureLayer.FeatureClass.ShapeType != esriGeometryType.esriGeometryMultipoint)
{
EagleEyeMapControl.AddLayer(pLayer);
}
}
}
//设置鹰眼地图全图显示
EagleEyeMapControl.Extent = mainMapControl.FullExtent;
pEnv = mainMapControl.Extent as IEnvelope;
DrawRectangle(pEnv);
EagleEyeMapControl.ActiveView.Refresh();
}
}
//在鹰眼地图上面画矩形框
private void DrawRectangle(IEnvelope pEnvelope)
{
//在绘制前,清除鹰眼中之前绘制的矩形框
IGraphicsContainer pGraphicsContainer = EagleEyeMapControl.Map as IGraphicsContainer;
IActiveView pActiveView = pGraphicsContainer as IActiveView;
pGraphicsContainer.DeleteAllElements();
//得到当前视图范围
IRectangleElement pRectangleElement = new RectangleElementClass();
IElement pElement = pRectangleElement as IElement;
pElement.Geometry = pEnvelope;
//设置矩形框(实质为中间透明度面)
IRgbColor pColor = new RgbColorClass();
pColor = GetRgbColor(255, 0, 0);
pColor.Transparency = 255;
ILineSymbol pOutLine = new SimpleLineSymbolClass();
pOutLine.Width = 2;
pOutLine.Color = pColor;
IFillSymbol pFillSymbol = new SimpleFillSymbolClass();
pColor = new RgbColorClass();
pColor.Transparency = 0;
pFillSymbol.Color = pColor;
pFillSymbol.Outline = pOutLine;
//向鹰眼中添加矩形框
IFillShapeElement pFillShapeElement = pElement as IFillShapeElement;
pFillShapeElement.Symbol = pFillSymbol;
pGraphicsContainer.AddElement((IElement)pFillShapeElement, 0);
//刷新
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
//定义用于获取颜色的GetRgbColor方法
private IRgbColor GetRgbColor(int intR, int intG, int intB)
{
IRgbColor pRgbColor = null;
if (intR < 0 || intR > 255 || intG < 0 || intG > 255 || intB < 0 || intB > 255)
{
return pRgbColor;
}
pRgbColor = new RgbColorClass();
pRgbColor.Red = intR;
pRgbColor.Green = intG;
pRgbColor.Blue = intB;
return pRgbColor;
}
*出现“无法嵌入互操作类型”的错误
解决方法如下:
修改相应程序集的“嵌入互操作类型”属性值,将原来的True改为False
3、设置MainMapControl控件的OnMapReplaced属性的功能,在OnMapReplaced中添加SynchronizeEagleEye方法
private void MainMapControl_OnMapReplaced(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMapReplacedEvent e)
{
SynchronizeEagleEye();
}
4、设置MainMapControl控件的OnExtentUpdated属性的功能,在OnExtentUpdated中添加DrawRectangle方法
private void MainMapControl_OnExtentUpdated(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnExtentUpdatedEvent e)
{
//得到当前视图范围
pEnv = (IEnvelope)e.newEnvelope;
DrawRectangle(pEnv);
}
5、设置EagleEyeMapControl控件的OnMouseDown属性的功能,添加OnMouseDown的左键和右键功能代码
//定义声明变量
IPoint pMoveRectPoint;
bool bCanDrag;
private void EagleEyeMapControl_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
if (EagleEyeMapControl.Map.LayerCount > 0)
{
//按下鼠标左键移动矩形框
if (e.button == 1)
{
//如果指针落在鹰眼的矩形框中,标记可移动
if (e.mapX > pEnv.XMin && e.mapY > pEnv.YMin && e.mapX < pEnv.XMax && e.mapY < pEnv.YMax)
{
bCanDrag = true;
}
pMoveRectPoint = new PointClass();
pMoveRectPoint.PutCoords(e.mapX, e.mapY); //记录点击的第一个点的坐标
}
//按下鼠标右键绘制矩形框
else if (e.button == 2)
{
IEnvelope pEnvelope = EagleEyeMapControl.TrackRectangle();
IPoint pTempPoint = new PointClass();
pTempPoint.PutCoords(pEnvelope.XMin + pEnvelope.Width / 2, pEnvelope.YMin + pEnvelope.Height / 2);
mainMapControl.Extent = pEnvelope;
//矩形框的高宽和数据试图的高宽不一定成正比,这里做一个中心调整
mainMapControl.CenterAt(pTempPoint);
}
}
}
6、设置EagleEyeMapControl控件的OnMouseMove属性的功能,添加OnMouseMove改变鼠标样式的功能代码
private void EagleEyeMapControl_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
{
if (e.mapX > pEnv.XMin && e.mapY > pEnv.YMin && e.mapX < pEnv.XMax && e.mapY < pEnv.YMax)
{
//如果鼠标移动到矩形框中,鼠标换成小手,表示可以拖动
EagleEyeMapControl.MousePointer = esriControlsMousePointer.esriPointerHand;
if (e.button == 2) //如果在内部按下鼠标右键,将鼠标演示设置为默认样式
{
EagleEyeMapControl.MousePointer = esriControlsMousePointer.esriPointerDefault;
}
}
else
{
//在其他位置将鼠标设为默认的样式
EagleEyeMapControl.MousePointer = esriControlsMousePointer.esriPointerDefault;
}
if (bCanDrag)
{
double Dx, Dy; //记录鼠标移动的距离
Dx = e.mapX - pMoveRectPoint.X;
Dy = e.mapY - pMoveRectPoint.Y;
pEnv.Offset(Dx, Dy); //根据偏移量更改 pEnv 位置
pMoveRectPoint.PutCoords(e.mapX, e.mapY);
DrawRectangle(pEnv);
mainMapControl.Extent = pEnv;
}
}
7、设置EagleEyeMapControl控件的OnMouseUp属性的功能,添加OnMouseUp范围变化的功能代码
private void EagleEyeMapControl_OnMouseUp(object sender, IMapControlEvents2_OnMouseUpEvent e)
{
if (e.button == 1 && pMoveRectPoint != null)
{
if (e.mapX == pMoveRectPoint.X && e.mapY == pMoveRectPoint.Y)
{
mainMapControl.CenterAt(pMoveRectPoint);
}
bCanDrag = false;
}
}
8、设置MainMapControl控件的OnAfterScreenDraw属性的功能,添加OnAfterScreenDraw的功能代码及CopyToPageLayout代码
private void CopyToPageLayout()
{
IObjectCopy pObjectCopy = new ObjectCopyClass();
object copyFromMap = mainMapControl.Map;
object copiedMap = pObjectCopy.Copy(copyFromMap);//复制地图到copiedMap中
object copyToMap = axPageLayoutControl1.ActiveView.FocusMap;
pObjectCopy.Overwrite(copiedMap, ref copyToMap); //复制地图
axPageLayoutControl1.ActiveView.Refresh();
}
private void mainMapControl_OnAfterScreenDraw(object sender, IMapControlEvents2_OnAfterScreenDrawEvent e)
{
IActiveView pActiveView = (IActiveView)axPageLayoutControl1.ActiveView.FocusMap;
IDisplayTransformation displayTransformation = pActiveView.ScreenDisplay.DisplayTransformation;
displayTransformation.VisibleBounds = mainMapControl.Extent;
axPageLayoutControl1.ActiveView.Refresh();
CopyToPageLayout();
}
9、鹰眼成果展示
鼠标移动鹰眼视图内变成手指样式;可在鹰眼视图内左键拖动矩形框、右键画矩形框来改变显示区域
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/163445.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...