0

并尝试使用小型绘图应用程序,该应用程序仅绘制带有鼠标事件的线条。

有人可以为我提供正确的工作示例如何创建撤消功能吗?我有带有事件pictureBox1_MouseDown、pictureBox1_MouseMove、pictureBox1_MouseUp 的picturebox1。

当我移动鼠标并单击图片框时,它会按预期绘制线条。但是如何创建撤消功能,例如单击撤消按钮?

     private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {

            lx = e.X;
            ly = e.Y;

            Graphics g = pictureBox1.CreateGraphics();
            g.DrawLine(new Pen(new SolidBrush(paintcolor), 3), new Point(x, y), new Point(lx, ly));


            this.Invalidate();

        }




   private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            x = e.X;
            y = e.Y;

        }




  private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {

        }

我另外创建

class Shape
{

    public string _font { get; set; }
    public string g { get; set; }
    public int x { get; set; }
    public int y { get; set; }
    public int lx { get; set; }
    public int ly { get; set; }
    public string paintcolor { get; set; }
    public string s { get; set; }


}

然后在公共部分类 Form1 下添加:Form

private Stack<Shape> _shapes = new Stack<Shape>();

比添加到pictureBox1_MouseUp

newShape._font = _font.ToString();
            newShape.g = g.ToString();
            newShape.x = x;
            newShape.y = y;
            newShape.lx = lx;
            newShape.ly = ly;
            newShape.paintcolor = paintcolor.ToString();




                _shapes.Push(newShape);


            this.Invalidate();

和撤消按钮btnUndo

  private void btnUndo_Click(object sender, EventArgs e)
        {


            if (_shapes.Count > 0)
            {

                this.pictureBox1.Invalidate();
                _shapes.Pop();


            }
            else
            {
                MessageBox.Show("Nothing to remove.", "No Shapes",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

        }

并在 _shapes.Push(newShape); 上放置断点;

_shapes 是用值归档的

当我按下按钮btnUndo

我看到 _shapes.Pop(); 删除值后进先出,但当然我的行并没有消失。所以我很困惑,因为我觉得我需要在pictureBox1 上创建线条,而不是从鼠标移动,而是从堆栈。

4

0 回答 0