Click here to Skip to main content
15,896,342 members

draw rectangle and undo

qasimqadri asked:

Open original thread
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]
Tags: Rectangle

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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