Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my below code which draw rectangle and eclipse on mouse event..i try search undo alots.but fail to undo mycode..i want dat wen i click on button then last rectangle remove from drawing.pls dnt give me link.modify my code coz am not very much experience on graphics.simply click button undo perform,last draw rectangle or eclipse will remove..clicking button rrmoving previous rec or eclipse
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace ControlShapePaint
{
    public partial class DrawShapes : UserControl
    {
        private readonly List<Rectangle> rects = new List<Rectangle>();
        private readonly List<Rectangle> oval = new List<Rectangle>();
        public bool isOval;
        public bool check = false;
        private Point tempStartPoint;
        private Point tempStartPointoval;
        private Rectangle tempRect;
        private Rectangle tempOval;
        public DrawShapes()
        {
            InitializeComponent();
            this.DoubleBuffered = true;
        }

        private void toolStripButtonRectangle_Click(object sender, EventArgs e)
        {
            isOval = true;
            check = true;
        }

        private void toolStripButtonEllipse_Click(object sender, EventArgs e)
        {
            isOval = false;
            check = true;
        }

        private void DrawShapes_MouseDown(object sender, MouseEventArgs e)
        {
            if (check == true)
            {
                if (isOval == true)
                {
                    tempStartPoint = e.Location;
                }
                else if (isOval == false)
                {
                    tempStartPointoval = e.Location;
                }

                Invalidate();
            }

        }

        private void DrawShapes_MouseMove(object sender, MouseEventArgs e)
        {
            if (check == true)
            {
                if (e.Button != MouseButtons.Left)
                    return;

                if (isOval == true)
                {
                    Point tempEndPoint = e.Location;
                    tempRect =

                           new Rectangle(
                               Math.Min(tempStartPoint.X, tempEndPoint.X),
                               Math.Min(tempStartPoint.Y, tempEndPoint.Y),
                               Math.Abs(tempStartPoint.X - tempEndPoint.X),
                               Math.Abs(tempStartPoint.Y - tempEndPoint.Y));
                }
                else if (isOval == false)
                {
                    Point tempEndPoint1 = e.Location;
                    tempOval =
                    new Rectangle(
                        Math.Min(tempStartPointoval.X, tempEndPoint1.X),
                        Math.Min(tempStartPointoval.Y, tempEndPoint1.Y),
                        Math.Abs(tempStartPointoval.X - tempEndPoint1.X),
                        Math.Abs(tempStartPointoval.Y - tempEndPoint1.Y));
                }

                Invalidate();
            }
        }

        private void DrawShapes_MouseUp(object sender, MouseEventArgs e)
        {
            if (check == true)
            {
                // Must be within constraint, prevents tiny invisible rectangles from being added
                if (isOval == true)
                {
                    if (tempRect.Width >= 10 && tempRect.Height >= 10)
                        rects.Add(tempRect);
                }
                else if (isOval == false)
                {
                    if (tempOval.Width >= 10 && tempOval.Height >= 10)
                        oval.Add(tempOval);
                }
            }
        }

        private void DrawShapes_Paint(object sender, PaintEventArgs e)
        {
            using (Pen pen = new Pen(Color.Black, 2))
            {
                // Redraws all existing rectangles onto the form
                foreach (Rectangle rect in rects)
                    e.Graphics.DrawRectangle(pen, rect);

                // Must be within constraint, prevents tiny invisible rectangles from being added
                if (tempRect.Width >= 10 && tempRect.Height >= 10)
                    e.Graphics.DrawRectangle(pen, tempRect);

            }
            using (Pen pen1 = new Pen(Color.Black, 2))
            {
                // Redraws all existing rectangles onto the form
                foreach (Rectangle rectOval in oval)
                    e.Graphics.DrawEllipse(pen1, rectOval);

                // Must be within constraint, prevents tiny invisible rectangles from being added
                if (tempOval.Width >= 10 && tempOval.Height >= 10)
                    e.Graphics.DrawEllipse(pen1, tempOval);
            }


        }





    }
}
[Edit]Code block added[/Edit]
Posted
Updated 2-Mar-13 5:50am
v2
Comments
[no name] 2-Mar-13 11:55am    
Since you want someone to do this for you, have you considered going to a rent-a-coder site and hiring someone? What don't you just create an OnPaint handler and draw whatever you want (or not) in that?

1 solution

Since you add your objects to a list (or two) here is how I would do it.
1) Create an abstract MyShape class, and derive a MyRectangle and MyOval from it. Probably, each should provide code to draw themselves, given an appropriate Graphics object
2) Replace my two lists with a single MyShape list.
3) Add a ReDo MyShape list.
4) When you create a rectangle or oval with the mouse, add it as an appropriate MyShape object to the MyShapes list, and clear the ReDo list. Then Invalidate as you are.
5) Change the Paint handler to draw from the single MyShapes list.
7) To Undo, remove an item from the end of the MyShapes list, and add it to the Redo list. Then Invalidate.
8) To Redo, remove an item from the Redo list, and put it back on the MyShapes list.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900