// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Drawing; using System.Windows.Forms; public class MainForm : Form { private const int D = 20; private const int W = 400; private const int H = 400; private static readonly Random rng = new Random(); private int x; private int y; private int dx; private int dy; private PictureBox pic; private Timer t; public MainForm() { x = rng.Next(0, W-D); y = rng.Next(0, H-D); dx = rng.Next(1, 5); dy = rng.Next(1, 5); pic = new PictureBox(); pic.Location = new Point(0, 0); pic.Size = new Size(W, H); pic.Paint += BallPaint; Controls.Add(pic); Resize += Mirror; ClientSize = new Size(W, H); t = new Timer(); t.Interval = 10; t.Tick += BallMove; t.Start(); } private void Mirror(object sender, EventArgs evt) { pic.Size = this.ClientSize; } private void BallMove(object sender, EventArgs evt) { x += dx; y += dy; if(x < 0) { x = 0; dx = -dx; dy += rng.Next(-1, 1); } if(x > pic.Width - D) { x = pic.Width - D; dx = -dx; dy += rng.Next(-1, 1); } if(y < 0) { y = 0; dx += rng.Next(-1, 1); dy = - dy; } if(y > pic.Height - D) { y = pic.Height - D; dx += rng.Next(-1, 1); dy = - dy; } pic.Invalidate(); } private void BallPaint(object sender, PaintEventArgs evt) { evt.Graphics.FillEllipse(new SolidBrush(Color.Blue), x, y, D, D); } [STAThread] public static void Main(string[] args) { Application.Run(new MainForm()); } }