locked
Cancel MouseDragged event on ESC key press RRS feed

  • Question

  • I am having a scenario where i need to cancel a drawing operation in between if user press ESC key (Similar to MS word drawing and ESC key press). I am doing the drawing operation in mouse dragged event and also there is a mouse released event to finalize the drawing.

    I am able to capture the keypress event in between on ESC key press but dont know how to cancel the current mouse dragged operation.

    I am doing this in C#.NET 4.5 winforms
    • Moved by CoolDadTx Wednesday, September 21, 2016 3:27 PM Winforms related
    Wednesday, September 21, 2016 11:19 AM

Answers

  • Hi BKoshy,

     

    Thank you for your post.

     

    According to your description ,I think you can use a Queue to save the points  which you drag, and redraw these points with the form’s color in the keypress event .

    Code below is for your reference.

            private void Form1_Load(object sender, EventArgs e)
            {
                this.KeyPreview = true;
            }
            private bool isPaint = false;
            Point startPoint = new Point();
            Point endPoint = new Point();
            private Queue<Point> pointQueue = new Queue<Point>();
         
            private void Form1_MouseDown(object sender, MouseEventArgs e)
            {
                pointQueue.Enqueue(new Point(0,0));
                //set the isPaint ture when MouseDown and record the start Point
                this.isPaint = true;
                this.startPoint.X = e.X;
                this.startPoint.Y = e.Y;
                pointQueue.Enqueue(startPoint);     
            }    
            private void Form1_MouseMove(object sender, MouseEventArgs e)
            {   
                Graphics g = this.CreateGraphics();
                Pen P = new Pen(Color.Red, 3);
                if (!isPaint)
                    return;
                //writeline 
                this.endPoint.X = e.X;
                this.endPoint.Y = e.Y;
                g.DrawLine(P, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
                //update the start Point
                startPoint.X = e.X;
                startPoint.Y = e.Y;
                pointQueue.Enqueue(startPoint);
            }
     
            private void Form1_MouseUp(object sender, MouseEventArgs e)
            {
                //set the isPaint false when MouseUp 
                isPaint = false;
                pointQueue.Enqueue(startPoint);
            }
     
            private void Form1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar == (char)Keys.Escape)
                {
                    isPaint = false;
                    if (pointQueue.Count != 0)
                    {
                        Graphics g1 = this.CreateGraphics();
                        Pen p1 = new Pen(this.BackColor, 3);
                        Point start = (Point)pointQueue.Dequeue();
                        int count = pointQueue.Count;
                        while ( pointQueue.Count != 0)
                        {
                            Point end = (Point)pointQueue.Dequeue();
                            if (end == new Point(0, 0))
                                return;
                            g1.DrawLine(p1, start, end);
                            start = end;
                        }    
                    }               
                }
            }

    I hope this can be helpful.

     

    Best Regards,

    Misty Zi

    Thursday, September 22, 2016 11:12 AM
  • To add; you can use the GraphicsPath class to store your points and perform drawing requirements.

    https://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.graphicspath(v=vs.110).aspx

    Friday, September 23, 2016 1:01 AM

All replies

  • Hi BKoshy,

     

    Thank you for your post.

     

    According to your description ,I think you can use a Queue to save the points  which you drag, and redraw these points with the form’s color in the keypress event .

    Code below is for your reference.

            private void Form1_Load(object sender, EventArgs e)
            {
                this.KeyPreview = true;
            }
            private bool isPaint = false;
            Point startPoint = new Point();
            Point endPoint = new Point();
            private Queue<Point> pointQueue = new Queue<Point>();
         
            private void Form1_MouseDown(object sender, MouseEventArgs e)
            {
                pointQueue.Enqueue(new Point(0,0));
                //set the isPaint ture when MouseDown and record the start Point
                this.isPaint = true;
                this.startPoint.X = e.X;
                this.startPoint.Y = e.Y;
                pointQueue.Enqueue(startPoint);     
            }    
            private void Form1_MouseMove(object sender, MouseEventArgs e)
            {   
                Graphics g = this.CreateGraphics();
                Pen P = new Pen(Color.Red, 3);
                if (!isPaint)
                    return;
                //writeline 
                this.endPoint.X = e.X;
                this.endPoint.Y = e.Y;
                g.DrawLine(P, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
                //update the start Point
                startPoint.X = e.X;
                startPoint.Y = e.Y;
                pointQueue.Enqueue(startPoint);
            }
     
            private void Form1_MouseUp(object sender, MouseEventArgs e)
            {
                //set the isPaint false when MouseUp 
                isPaint = false;
                pointQueue.Enqueue(startPoint);
            }
     
            private void Form1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar == (char)Keys.Escape)
                {
                    isPaint = false;
                    if (pointQueue.Count != 0)
                    {
                        Graphics g1 = this.CreateGraphics();
                        Pen p1 = new Pen(this.BackColor, 3);
                        Point start = (Point)pointQueue.Dequeue();
                        int count = pointQueue.Count;
                        while ( pointQueue.Count != 0)
                        {
                            Point end = (Point)pointQueue.Dequeue();
                            if (end == new Point(0, 0))
                                return;
                            g1.DrawLine(p1, start, end);
                            start = end;
                        }    
                    }               
                }
            }

    I hope this can be helpful.

     

    Best Regards,

    Misty Zi

    Thursday, September 22, 2016 11:12 AM
  • To add; you can use the GraphicsPath class to store your points and perform drawing requirements.

    https://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.graphicspath(v=vs.110).aspx

    Friday, September 23, 2016 1:01 AM