Map中實現 矩形框跟隨滑鼠移動

來源:互聯網
上載者:User
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;

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.