1

My current program allows the user to click a point, then click another point (at least 20 pixels away) and draws a line between the 2 points. I've used a Polyline so that this can be done multiple times. Though the set of all the lines only appear after all the click are done.

void DrawingCanvas_MouseUp(object sender, MouseButtonEventArgs e) {
        Point position = e.GetPosition(this);

        if (leftList == null) {
            //starting a new set
            leftList.Add(position);
            lastPoint = position;
            return;
        }
        //calculate distance, i.e. end click
        double a = lastPoint.X - position.X;
        double b = lastPoint.Y - position.Y;
        double distance = Math.Sqrt(a * a + b * b);
        if (distance > 20) {
            //continue to add to list
            leftList.Add(position);
            lastPoint = position;
        } else {
            //end of the line
            paint();
            leftList = new PointCollection(); 
        }

    }

    private void paint() {
        Polyline line = new Polyline();
        line.Visibility = System.Windows.Visibility.Visible;
        line.StrokeThickness = 2;
        line.Stroke = System.Windows.Media.Brushes.Black;
        line.Points = leftList;
        myCanvas.Children.Add(line);
    }

So my question is two-fold:

A) How do I make it so that after each click the new line is immediately added.

B) How do I render a line between the last point and where the mouse cursor is currently at (i.e. just before you choose your next point)

4

2 回答 2

2

The following simple example starts drawing a new polyline when the left mouse button is pressed and the mouse is moved by the minimum point distance of 20, with the button kept pressed. It draws the last polyline segment (to the current mouse position) in either red or green, depending on its length. If the mouse button is released and the length of the new segment is >= 20, a new point is appended to the polyline. Otherwise the polyline is terminated, and a new polyline can be created.

private Polyline polyline;
private Polyline segment = new Polyline { StrokeThickness = 2 };

private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    if (polyline == null)
    {
        var canvas = (Canvas)sender;
        var point = e.GetPosition(canvas);

        // create new polyline
        polyline = new Polyline { Stroke = Brushes.Black, StrokeThickness = 2 };
        polyline.Points.Add(point);
        canvas.Children.Add(polyline);

        // initialize current polyline segment
        segment.Stroke = Brushes.Red;
        segment.Points.Add(point);
        segment.Points.Add(point);
        canvas.Children.Add(segment);
    }
}

private void Canvas_MouseMove(object sender, MouseEventArgs e)
{
    if (polyline != null)
    {
        // update current polyline segment
        var canvas = (Canvas)sender;
        segment.Points[1] = e.GetPosition(canvas);

        var distance = (segment.Points[0] - segment.Points[1]).Length;
        segment.Stroke = distance >= 20 ? Brushes.Green : Brushes.Red;
    }
}

private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    if (polyline != null)
    {
        var canvas = (Canvas)sender;
        segment.Points[1] = e.GetPosition(canvas);

        var distance = (segment.Points[0] - segment.Points[1]).Length;

        if (distance >= 20)
        {
            polyline.Points.Add(segment.Points[1]);
            segment.Points[0] = segment.Points[1];
        }
        else
        {
            if (polyline.Points.Count < 2)
            {
                canvas.Children.Remove(polyline);
            }

            polyline = null;
            segment.Points.Clear();
            canvas.Children.Remove(segment);
        }
    }
}
于 2013-04-10T09:00:10.307 回答
0

please maintain a collection of points on every click. in collection you can add one class which will have two properties like StartPoint and EndPoint.

when the mouse is clicked first time just add one class object to collection having start point only. and when you click the mouse next time, ad end point to the last object of the class and meanwhile create a new object and assign this point as its start point and add it to collection, after that call the paint function.

于 2013-04-10T05:55:14.160 回答