Draw Line
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Draw Line

Hybrid View

  1. #1
    Join Date
    Jan 2009
    Posts
    1

    Question Draw Line

    I want to draw a line.
    I use this code


    Code:
    private void OnMouseDown(object Sender, MouseEventArgs Args)
    {
                    StartPoint = Args.GetPosition(DrawingCanvas);
    }
    
    private void OnMouseMove(object Sender, MouseEventArgs Args)
    {
     if (Args.LeftButton == MouseButtonState.Pressed)
                    {
                        EndPoint = Args.GetPosition(DrawingCanvas);
                        DrawLine(StartPoint, EndPoint, DrawingCanvas);
                    }
    }
    
    private void DrawLine(Point From, Point To, Canvas TargetCanvas)
            {
                CurrentLine = new Line();
                CurrentLine.StrokeEndLineCap = PenLineCap.Round;
                CurrentLine.StrokeStartLineCap = PenLineCap.Round;
                CurrentLine.Stroke = Brushes.Red;
                CurrentLine.StrokeThickness = 2.0;
                CurrentLine.X1 = From.X;
                CurrentLine.Y1 = From.Y;
                CurrentLine.X2 = To.X;
                CurrentLine.Y2 = To.Y;
                Canvas.SetLeft(TargetCanvas, From.X);
                Canvas.SetTop(TargetCanvas, From.Y);
                TargetCanvas.Children.Add(CurrentLine);
            }
    But the previous line is visible.
    How can I avoid that??

    Thanks & Regards

    Prajeesh Prabhakar
    Last edited by gstercken; April 20th, 2009 at 07:05 AM. Reason: Added code tags

  2. #2
    Join Date
    Sep 2002
    Location
    14 39'19.65"N / 121 1'44.34"E
    Posts
    9,815

    Re: Draw Line

    Quote Originally Posted by Prajeesh07 View Post
    But the previous line is visible.
    How can I avoid that??
    Well, this is sort of obvious, since it's exactly what your code does: It creates a new Line object on every MouseMove event. I guess what you intended is to create the new line in your MouseDown handler, and change only its end point when the mouse is moved?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center