-2

我有一个带有工具条菜单的winforms程序,带有按钮:+,-。每次我单击 + 时,它都会绘制一个不断增长的椭圆,而当我单击 - 时,它就会消失。现在,我设法绘制了不断增长的椭圆,但是当我再次单击 + 时,它会绘制新的椭圆。我需要新的椭圆在我画的前一个椭圆内开始。因此,例如,当我单击 3 次时,另一个内部会有 3 个椭圆。当我点击 - 我画的最后一个椭圆会消失。我有使用 draImage() 的限制。我的问题是:当我再次单击 + 时,如何在前一个椭圆内创建一个新的椭圆?当我单击 + 时,它只是删除以前的并开始绘制新的椭圆。我的代码如下所示:

表格.cs:

public partial class TheBalls : Form
    {
        private Ball yourBall;

        public TheBalls()
        {
            InitializeComponent();
            DoubleBuffered = true;
            yourBall = new Ball(this);
        }

        Graphics flagGraphics;
        List<Ball> list = new List<Ball> { };

        private void plusButton_MouseDown(object sender, MouseEventArgs e)
        {
            yourBall = new Ball(this);
            list.Add(yourBall);
            yourBall.Growing();
        }

        private void minusButton_MouseDown(object sender, MouseEventArgs e)
        {
            if (list.Count > 0)
            {
                list[list.Count - 1].clr = Color.Transparent;
                list.RemoveAt(list.Count -1);
            }
        }

        private void TheBalls_Paint(object sender, PaintEventArgs e)
        {

            Bitmap bitmap = new Bitmap(yourBall.Center.X * 2, yourBall.Center.Y * 2);
            flagGraphics = Graphics.FromImage(bitmap);
            yourBall.Update(flagGraphics);
            e.Graphics.DrawImage(bitmap, 0, 0);
        }


    }

球类:(椭圆)

class Ball
    {
        private Control canvas;
        private Timer t = new Timer();
        private int dir = 1;

        public Point Center { get; set; }
        public Color clr;
        public int Radius { get; set; }

        public Control Canvas
        {
            get { return canvas; }
            set
            {
                canvas = value;
                if (canvas != null)
                {
                    //canvas.SizeChanged -= Canvas_SizeChanged;
                    //canvas.SizeChanged += Canvas_SizeChanged;
                    Center = new Point(canvas.ClientSize.Width / 2, canvas.ClientSize.Height/ 2);
                }
            }
        }
        private static readonly Random rand = new Random();

        public Ball(Control canvas)
        {
            clr = Color.FromArgb(rand.Next(255), rand.Next(255), rand.Next(255));
            if (canvas != null)
            {
                this.canvas = canvas;
                canvas.SizeChanged -= Canvas_SizeChanged;
                canvas.SizeChanged += Canvas_SizeChanged;
                Center = new Point(canvas.ClientSize.Width / 2, canvas.ClientSize.Height / 2);

            }

            t.Interval = 10;
            t.Tick += timer_Tick;
        }
        private void Canvas_SizeChanged(object sender, EventArgs e)
        {
            Center = new Point(canvas.ClientSize.Width / 2, canvas.ClientSize.Height / 2);
        }
        public void Growing()
        {
            dir = 1;
            t.Enabled = true;

        }

        public void timer_Tick(object sender, EventArgs e)
        {
            if (canvas == null)
            {
                //t.Stop();
                clr = Color.Transparent;
                return;
            }
            Radius += dir;

            if (Radius > Math.Min(canvas.ClientSize.Width, canvas.ClientSize.Height) / 2)
            {
                Radius = Math.Min(canvas.ClientSize.Width, canvas.ClientSize.Height) / 2;
                clr = Color.Transparent;
            }
            canvas.Invalidate();
        }

        public void Update(Graphics g)
        {
            g.FillEllipse(new SolidBrush(clr), new Rectangle(Center.X - Radius, Center.Y - Radius, Radius * 2, Radius * 2));
        }
    }
4

2 回答 2

1

像这样更改您的 TheBalls_Paint 事件:

 private void TheBalls_Paint(object sender, PaintEventArgs e)
 {
     Bitmap bitmap = new Bitmap(yourBall.Center.X * 2, yourBall.Center.Y * 2);
     flagGraphics = Graphics.FromImage(bitmap);
     foreach (var ball in list)
     {
         ball.Update(flagGraphics);
     }
     e.Graphics.DrawImage(bitmap, 0, 0);
 }
于 2013-06-13T13:35:27.690 回答
1

现在更清楚了,是的。如果我错了,请纠正我,但看起来你list在绘画时根本没有使用变量,这就是为什么你每次只画一个球。

于 2013-06-13T13:25:07.610 回答