111,028
社区成员
发帖
与我相关
我的任务
分享
// RainDrops.cs
// compile with: csc /t:winexe RainDrops.cs
using System;
using System.Drawing;
using System.Windows.Forms;
public class Form1 : Form
{
Bitmap rainDrops;
Timer timer = new Timer();
int top = 0;
public Form1()
{
this.Size = new Size(300, 300);
Rectangle rect = this.ClientRectangle;
rainDrops = new Bitmap(rect.Width, rect.Height * 2);
using (Graphics g = Graphics.FromImage(rainDrops))
{
g.Clear(Color.LightBlue);
Random random = new Random();
for (int i = 0; i < 1000; i++)
{
int x = random.Next() % rect.Width;
int y = random.Next() % rect.Height;
int dot = random.Next() % 5;
g.FillEllipse(Brushes.White, x, y, dot, dot);
g.FillEllipse(Brushes.White, x, y+rect.Height, dot, dot);
}
}
timer.Interval = 200;
timer.Enabled = true;
timer.Tick += delegate { Invalidate(); };
}
protected override void OnPaint(PaintEventArgs e)
{
int height = this.ClientRectangle.Height;
top += 10;
if (top > height) top = 0;
e.Graphics.DrawImageUnscaled(rainDrops, 0, top - height);
}
protected override void OnPaintBackground(PaintEventArgs e)
{
}
static void Main()
{
Application.Run(new Form1());
}
}