draw line with mouse

Topics: WPF
Aug 9, 2011 at 10:49 AM

I draw line with version Winform ok but with WPF i don't find.

My code

 protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)

{

                        this.Cursor = Cursors.Cross;
                        ControlPaint.DrawReversibleLine(this.PointToScreen(p1), this.PointToScreen(p2), Color.Black);
                        p2 = new Point(FromLatLngToLocal(pe).X, FromLatLngToLocal(pe).Y);
                        ControlPaint.DrawReversibleLine(this.PointToScreen(p1), this.PointToScreen(p2), Color.Black);

}

protected override void OnMouseUp(MouseEventArgs e)

{

                    Point p1 = new Point(FromLatLngToLocal(ps).X, FromLatLngToLocal(ps).Y);
                    Point p2 = new Point(FromLatLngToLocal(pe).X, FromLatLngToLocal(pe).Y);
                    ControlPaint.DrawReversibleLine(this.PointToScreen(p1), this.PointToScreen(p2), Color.Black);
                    layerStation.Routes.Add(MapRouteFromGeometry(line, Guid.NewGuid().ToString()));

}

Help me with WPF

Aug 9, 2011 at 2:03 PM
Edited Aug 9, 2011 at 2:31 PM
		public void MouseMove(object sender, MouseEventArgs e)
		{
			//---cut---//
                list.Add(_firstpoint);
                list.Add(p.Position);
                _rullerroute = DrawPolyline(list, "", _map);
                var distance = ((MapRoute)_rullerroute.Tag).Distance * 1000;
                ((RullerLable)(l.Shape)).Caption.Text = string.Format("Distance {0:F2} m", distance);
			//---cut---//

		}

        private GMapMarker DrawPolyline(List<PointLatLng> points, string name, GMapControl map)
        {
            MapRoute r = new MapRoute(points, name);
            GMapMarker route = new GMapMarker(points[0]);
            route.Route.AddRange(r.Points);
            route.RegenerateRouteShape(map);
            route.ZIndex = -1;
            map.Markers.Add(route);
            route.Tag = r;
            return route;
        }
Aug 10, 2011 at 2:56 AM

your code

map.Markers.Add(route);
when we use mousemove marker will add a lot of routes into our map. That map has multiple lines, while we just draw a line

Aug 10, 2011 at 8:55 AM

I solved problem by follow code

 private void MainMap_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Point p = e.GetPosition(MainMap);
            switch (_button)
            {
                case ButtonStatus.LINE:
                    if (dragInProgress)
                    {
                        //Đổi tên marker lại
                        ((MapRoute)(FindMarker("temp").Tag)).Name=Guid.NewGuid().ToString();
                        pe = MainMap.FromLocalToLatLng((int)p.X, (int)p.Y);                                     
                       
                        //Nếu còn vẽ thì lấy điểm cuối của line trước thành điểm đầu của line sau
                        if (bDrawing)
                        {
                            ps = pe;
                        }
                      
                    }
                    //Vẽ điểm đầu tiên
                    else
                    {
                        ps = MainMap.FromLocalToLatLng((int)p.X, (int)p.Y);
                        dragInProgress = true;                    
                    }

    }

 private void MainMap_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            //Kết thúc vẽ
            bDrawing = false;
            dragInProgress = false;
        }

 private void MainMap_MouseMove(object sender, MouseEventArgs e)
        {
            bool mouseOver = false;
            Point p = e.GetPosition(MainMap);
            if (dragInProgress)
            {
                switch (_button)
                {
                    case ButtonStatus.LINE:
                        PointLatLng p1 = MainMap.FromLocalToLatLng((int)p.X, (int)p.Y);
                        List<PointLatLng> l = new List<PointLatLng>();
                        l.Add(ps);
                        l.Add(p1);
                        MainMap.Markers.Remove(FindMarker("temp"));
                        GMapMarker _rullerroute = DrawPolyline(l, "temp", MainMap);

                        break;

               }

           }

     }

Aug 10, 2011 at 2:44 PM

A lot of cut))) You can do something 

private _rullerroute;
public void MouseMove(object sender, MouseEventArgs e)
{
	..........
if (_rullerroute != null) _rullerroute.Map.Markers.Remove(_rullerroute);
..........
}