绘制矩形并撤消 [英] draw rectangle and undo

查看:82
本文介绍了绘制矩形并撤消的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我下面的代码,它在鼠标事件上绘制矩形和eclipse ..我尝试搜索撤消alots.but无法撤消mycode ..我想要我点击按钮然后最后一个矩形从drawing.pls dnt给我link.modify我的代码因为我对图形的体验不是很多。单击按钮撤消执行,最后绘制矩形或eclipse将删除..单击按钮,以前的rec或eclipse

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

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]

推荐答案

由于您将对象添加到列表(或两个),这里是我要做的它。

1)创建一个抽象的MyShape类,并从中派生MyRectangle和MyOval。可能每个应该提供代码来绘制自己,给定一个合适的Graphics对象

2)用一个MyShape列表替换我的两个列表。

3)添加一个ReDo MyShape列表。

4)用鼠标创建矩形或椭圆形时,将其作为相应的MyShape对象添加到MyShapes列表中,然后清除ReDo列表。然后按原样无效。

5)将Paint处理程序更改为从单个MyShapes列表中绘制。

7)要撤消,请从MyShapes末尾删除一个项目列表,并将其添加到重做列表。然后无效。

8)要重做,请从重做列表中删除一个项目,然后将其放回MyShapes列表。
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.


这篇关于绘制矩形并撤消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆