本文介绍了撤消问题&重做选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我创建了一个项目,通过用户移动鼠标绘制椭圆

和2个按钮撤消&重做



撤消工作o.k.

但我有重做选项的问题



如果我绘制3个圆圈

然后点击两次在撤消按钮上我删除了2个圆圈并留下了第一个圆圈

这很棒

但是当我点击重做按钮一次我想要返回第二个圆圈时我画画

但是我得到了2圈(第2和第3张也是最后一张)



这里是我的代码



Hey
i have created a project that draw an Ellipse via movement of the mouse by the user
and 2 buttons Undo & Redo

The undo works o.k.
but i have a problem with the Redo option

if i draw 3 circles
and then click twice on the undo button i delete 2 circles and left with the first circle
which is great
but when i click the redo button once i want to return the second circle that i draw
but i get the insted the 2 circle (2th and 3th and final)

here is my code

namespace Final_Project___Undo_and_Redo
{
    public partial class Form1 : Form
    {
        private Bitmap PicInMemory;
        private Graphics PaintOnMyPic;
        private int FirstX, FirstY;
        private Graphics PaintOnPanel;
        List<bitmap> UndoList;
        List<bitmap> RedoList;
        private bool UndoMade = false;
        private bool RedoMade = false;

        public Form1()
        {
            InitializeComponent();
            PicInMemory = new Bitmap(panel1.Width, panel1.Height);
            PaintOnPanel = panel1.CreateGraphics();
            PaintOnMyPic = Graphics.FromImage(PicInMemory);
            PaintOnMyPic.Clear(Color.White);
            UndoList = new List<bitmap>();
            RedoList = new List<bitmap>();
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            FirstX = e.X;
            FirstY = e.Y;
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            Pen MyPen = new Pen(Color.Aqua);
            if (e.Button == MouseButtons.Left)
            {
                    PaintOnPanel = panel1.CreateGraphics();
                    PaintOnPanel.DrawImage(PicInMemory, 0, 0);
                    PaintOnPanel.DrawEllipse(MyPen, FirstX, FirstY, e.X - FirstX, e.Y - FirstY);
            }
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            UndoList.Add(new Bitmap(PicInMemory));
            Pen MyPen = new Pen(Color.Aqua);
            PaintOnMyPic = Graphics.FromImage(PicInMemory);
            PaintOnMyPic.DrawEllipse(MyPen, FirstX, FirstY, e.X - FirstX, e.Y - FirstY);
            PaintOnPanel = panel1.CreateGraphics();
            PaintOnPanel.DrawImage(PicInMemory, 0, 0);
            RedoList.Add(new Bitmap(PicInMemory));
        }

        private void BtnUndo_Click(object sender, EventArgs e)
        {
            if (UndoList.Count == 0)
            {
                BtnUndo.Enabled = false;
                return;
            }
            PicInMemory = UndoList[UndoList.Count - 1];
            PaintOnPanel = panel1.CreateGraphics();
            PaintOnPanel.DrawImage(PicInMemory, 0, 0);
            UndoList.RemoveAt(UndoList.Count - 1);
            UndoMade = true;
        }

        private void BtnRedo_Click(object sender, EventArgs e)
        {
            if (RedoList.Count == 0 || UndoMade == false)
            {
                BtnRedo.Enabled = false;
                return;
            }
            PicInMemory = RedoList[RedoList.Count - 1];
            PaintOnPanel = panel1.CreateGraphics();
            PaintOnPanel.DrawImage(PicInMemory, 0, 0);
            UndoMade = false;
        }
    }
}



感谢您的帮助


Thanks for the help

推荐答案

var selectedItem = RedoList.First();
PicInMemory = selectedItem;
RedoList.RemoveAt(0)


int undo_times=0;
private void BtnUndo_Click(object sender, EventArgs e)
{
    if (UndoList.Count == 0)
    {
        BtnUndo.Enabled = false;
        return;
    }
    PicInMemory = UndoList[UndoList.Count - 1];
    PaintOnPanel = panel1.CreateGraphics();
    PaintOnPanel.DrawImage(PicInMemory, 0, 0);
    UndoList.RemoveAt(UndoList.Count - 1);
    undo_times+=1;
}

private void BtnRedo_Click(object sender, EventArgs e)
{
    if (RedoList.Count == 0 || undo_times == 0)
    {
        BtnRedo.Enabled = false;
        return;
    }
    PicInMemory = RedoList[RedoList.Count - undo_times];
    PaintOnPanel = panel1.CreateGraphics();
    PaintOnPanel.DrawImage(PicInMemory, 0, 0);
    undo_times-=1;
}



我最诚挚的祝福。


with my best wishes.


这篇关于撤消问题&amp;重做选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 00:01