1.要素选择
ICommand Select = new ControlsSelectFeaturesToolClass();
Select.OnCreate(axMapControl1.Object);
Select.OnClick();
axMapControl1.CurrentTool = (ITool)Select;
2. 鹰眼
private bool bCanDrag;//鹰眼在矩形框中可移动的标志
private IPoint pMoveRectPoint; //记录在移动鹰眼地图上的矩形框时鼠标的位置
private IEnvelope pEnv; //记录数据视图的Extent
private void axMapControl2_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
if (axMapControl2.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 = axMapControl2.TrackRectangle();
IPoint pTempPoint = new PointClass();
pTempPoint.PutCoords(pEnvelope.XMin + pEnvelope.Width / 2, pEnvelope.YMin + pEnvelope.Height / 2);
axMapControl1.Extent = pEnvelope;
//矩形框的高宽和数据试图的高宽不一定成正比,这里做一个中心调整
axMapControl1.CenterAt(pTempPoint);
}
}
private void axMapControl2_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
{
if (e.mapX > pEnv.XMin && e.mapY > pEnv.YMin && e.mapX < pEnv.XMax && e.mapY < pEnv.YMax)
{
//如果鼠标移动到矩形框中,鼠标换成小手,表示可以拖动
axMapControl2.MousePointer = esriControlsMousePointer.esriPointerHand;
if (e.button == 2) //如果在内部按下鼠标右键,将鼠标演示设置为默认样式
{
axMapControl2.MousePointer = esriControlsMousePointer.esriPointerDefault;
}
}
else
{
//在其他位置将鼠标设为默认的样式
axMapControl2.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);
axMapControl1.Extent = pEnv;
}
}
private void axMapControl2_OnMouseUp(object sender, IMapControlEvents2_OnMouseUpEvent e)
{
if (e.button == 1 && pMoveRectPoint != null)
{
if (e.mapX == pMoveRectPoint.X && e.mapY == pMoveRectPoint.Y)
{
axMapControl1.CenterAt(pMoveRectPoint);
}
bCanDrag = false;
}
}