看到有人用其他
语言写了一个下雪的程序
用as3实现下雪效果 ,就想到自己都很久都没用winform写程序了,然后就自己动手写了一个。
我先截个图展示一下:
http://hi.csdn.net/attachment/201107/24/0_1311472567aWCr.gif
下面我贴出我的代码:
- 1using System;
- 2using System.Drawing;
- 3using System.Windows.Forms;
- 4
- 5namespace snow
- 6{
- 7 public partial class Form1 : Form
- 8 {
- 9 public Form1()
- 10 {
- 11 InitializeComponent();
- 12 }
- 13 private int[,] snowArray =new int [800,3];
- 14 private int snowNum;
- 15 Random rand = new Random();
- 16 //int [][]a =new int[1][1];
- 17 private void Form1_Load(object sender, EventArgs e)
- 18 {
- 19
- 20 snowNum =rand.Next (800);
- 21 int x = rand.Next(800);
- 22 int y = rand.Next(600);
- 23 for (int i = 0; i < snowNum/10;i++ )
- 24 {
- 25 snowArray[i, 0] = x;
- 26 snowArray[i, 1] = y;
- 27 snowArray [i,2]= rand.Next (40);
- 28 x = rand.Next(800);
- 29 y = rand.Next(600);
- 30 }
- 31
- 32 }
- 33
- 34 private void Paintsnow(object sender, PaintEventArgs e)
- 35 {
- 36 Graphics paint = e.Graphics;
- 37 Bitmap bm = new Bitmap(10, 10);
- 38 bm.SetPixel(0, 0, Color.White);
- 39 bm.SetPixel(0, 1, Color.White);
- 40 bm.SetPixel(1, 0, Color.White);
- 41 bm.SetPixel(1, 1, Color.White);
- 42
- 43
- 44 for (int i = 0; i < snowNum/10; i++)
- 45 {
- 46 paint.DrawImageUnscaled(bm, snowArray[i, 0], snowArray[i, 1]);
- 47 }
- 48 }
- 49
- 50 private void timer1_Tick(object sender, EventArgs e)
- 51 {
- 52
- 53 for (int i = 0; i < snowNum/10; i++)
- 54 {
- 55 if (snowArray [i, 1] > 600)
- 56 {
- 57 snowArray[i, 1] = 0;
- 58 }
- 59 snowArray[i, 1] += snowArray[i, 2];
- 60
- 61
- 62 }
- 63 this.Invalidate();
- 64 this.FindForm().Paint+=new PaintEventHandler (Paintsnow);
- 65 }
- 66 }
- 67}
- 68
复制代码
有兴趣的人还可以增加效果。。。
源码下载: