现在的位置: 首页 > 综合 > 正文

Map中实现 矩形框跟随鼠标移动

2013年06月10日 ⁄ 综合 ⁄ 共 3227字 ⁄ 字号 评论关闭

Map中实现 矩形框跟随鼠标移动

1按钮实现选择是自定义画矩形框还是指定大小的矩形框:

//在矩形中分两种:1 地图上画矩形 2 指定宽高
switch (DrawingInfo.Draw_rectWay)
{
    case RectWay.RectDraw:
        {
            _mapControl.Action = SuperAction.CreateRectangle;
        }
        break;
    case RectWay.RectCustm:
        {
            //画自定义矩形
            double w = DrawingInfo.Draw_rectWidth;
            double h = DrawingInfo.Draw_rectHeight;
            pnt_before = m_map.MapToPixel(m_map.Center);
            Rectangle2D rect = new Rectangle2D(m_map.Center, new Size2D(w, h));
            _mapControl.Action = SuperAction.Null;
            _mapControl.Cursor = Cursors.Cross;
            DrawCustomerRect(rect);
            isMoveRect = true;
        }
        break;
    default:
        break;
}

 

2 画指定大小的矩形框:

private void DrawCustomerRect(Rectangle2D rect)
{
    rect_move = rect;
    _mapControl.Cursor = Cursors.Cross;
    double x = rect.Left;
    double y = rect.Top;
    double dblWidth = rect.Width;
    double dblHeight = rect.Height;
    Point2Ds points = new Point2Ds();
    Point2D pntLeftTop = new Point2D(x, y);
    Point2D pntLeftBottom = new Point2D(x, y - dblHeight);
    Point2D pntRightBottom = new Point2D(x + dblWidth, y - dblHeight);
    Point2D pntRightTop = new Point2D(x + dblWidth, y);
    points.Add(pntLeftTop);
    points.Add(pntLeftBottom);
    points.Add(pntRightBottom);
    points.Add(pntRightTop);
    points.Add(pntLeftTop);
    GeoLine rectLine = new GeoLine();
    rectLine.AddPart(points);
    GeoStyle rectangleStyle = new GeoStyle();
    rectangleStyle.LineColor = Color.FromArgb(255, 0, 0);
    rectangleStyle.LineWidth = 0.5;
    rectLine.Style = rectangleStyle;
    int moveRectIndex = m_map.TrackingLayer.IndexOf("CustRect");
    if (moveRectIndex >= 0)
    {
        m_map.TrackingLayer.Remove(moveRectIndex);
    }
    m_map.TrackingLayer.Add(rectLine, "CustRect");
    m_map.RefreshTrackingLayer();
}
/// <summary>
/// point2D的偏移点
/// </summary>
/// <param name="point"></param>
/// <param name="dx"></param>
/// <param name="dy"></param>
/// <returns></returns>
private Point2D OffsetPoint(Point2D point, double dx, double dy)
{
    point.Offset(dx, dy);
    return point;
}

 

3,鼠标移动事件:

private void _mapControl_MouseMove(object sender, MouseEventArgs e)
{
    if (isMoveRect)
    {
        Point2D pnt2DBeforeMove = m_map.PixelToMap(pnt_before);
        Point2D pnt2DAfterMove = m_map.PixelToMap(e.Location);
        double dx = pnt2DAfterMove.X - pnt2DBeforeMove.X;
        double dy = pnt2DAfterMove.Y - pnt2DBeforeMove.Y;
        rect_move = new Rectangle2D(OffsetPoint(new Point2D(rect_move.Left, rect_move.Top), dx, dy),
                                    OffsetPoint(new Point2D(rect_move.Right, rect_move.Bottom), dx, dy));
        DrawCustomerRect(rect_move);
        pnt_before = e.Location;//这句很重要
    }
}

 

4, mapcontrol动作光标改变事件:

 this._mapControl.ActionCursorChanging += _mapControl_ActionCursorChanging;
void _mapControl_ActionCursorChanging(object sender, ActionCursorChangingEventArgs e)
{
    if (isMoveRect)
    {
        e.FollowingCursor = Cursors.Cross;
    }
}

 

5 变量的声明。这一步其实该放第一位,忘了。所以补上:

        private bool isMoveRect = false;
        private Point pnt_before;
        private Rectangle2D rect_move;

mapcontrol属性:

this._mapControl.IsWaitCursorEnabled = false;
this._mapControl.TrackMode = TrackMode.Track;

 

抱歉!评论已关闭.