|

c#桌面飘雪小程序,之前在网上查到过代码,但有很多错误,并且在win7下无法运行。
将其改成c#代码,并测试,效果如上图,可兼容XP和win7系统均可运行。 其主要代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; using System.Runtime.InteropServices; namespace WindowsXueJi04 { public partial class Form1 : Form { [DllImport("gdi32.dll")] static extern int SetPixel(IntPtr hSdc, int x, int y, int crColor); [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")] public static extern IntPtr CreateDC(string lpszDriver, string lpszDevice, string lpszOutput, int lpInitData); [DllImport("gdi32.dll")] public static extern int DeleteDC(IntPtr hdc); static int SnowNumber = 600; public SnowNode[] SnowNodes = new SnowNode[SnowNumber]; int CrWind = 0; int ScreenWidth; //屏幕宽度 int ScreenHeight; Bitmap Bmp; Color SnowD; Color SnowS; Color Snowc; public Form1() { InitializeComponent(); ScreenWidth = Screen.PrimaryScreen.Bounds.Width; ScreenHeight = Screen.PrimaryScreen.Bounds.Height; SnowD = ColorTranslator.FromWin32(0xFFFFFF); SnowS = ColorTranslator.FromWin32(0xFFDDDD); Snowc = ColorTranslator.FromWin32(0xFEFfFE); Bmp = new Bitmap(ScreenWidth, ScreenHeight); this.timer1.Interval = 1200; this.timer2.Interval = 2; this.timer1.Enabled = true; InitSnowNodes(); this.timer2.Enabled = true; } public void InitSnowNodes()//初始化雪点 { Graphics G = Graphics.FromImage(Bmp); G.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(ScreenWidth, ScreenHeight)); G.Dispose();//释放资源 Random rand = new Random(); for (int i = 0; i < SnowNumber; i++) { SnowNodes[i] = new SnowNode(rand.Next(ScreenWidth), rand.Next(ScreenHeight)); SnowNodes[i].iColor = Bmp.GetPixel(SnowNodes[i].postion.X, SnowNodes[i].postion.Y); } IntPtr Ptr = CreateDC("DISPLAY", null, null, 0); for (int j = 0; j < SnowNumber; j++) { SetPixel(Ptr, SnowNodes[j].postion.X, SnowNodes[j].postion.Y, 0xFEFfFE); } DeleteDC(Ptr); Graphics G1 = Graphics.FromImage(Bmp);//复制桌面到BMP G1.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(ScreenWidth, ScreenHeight)); G1.Dispose(); } void MoveSnowNodes() { IntPtr Ptr = CreateDC("DISPLAY", null, null, 0); Random rand = new Random(); for (int i = 0; i < SnowNumber; i++)// { SetPixel(Ptr, SnowNodes[i].postion.X, SnowNodes[i].postion.Y, ColorTranslator.ToWin32(SnowNodes[i].iColor)); int dx = rand.Next(3) - 1 + CrWind*2; int dy = 4; SnowNodes[i].postion.X += dx; SnowNodes[i].postion.Y += dy; if ((SnowNodes[i].postion.X < 2)||(SnowNodes[i].postion.X > (ScreenWidth-2)) ||(SnowNodes[i].postion.Y > (ScreenHeight-2))) { SnowNodes[i].postion.X = rand.Next(ScreenWidth-2)+1; SnowNodes[i].postion.Y = rand.Next(3); SnowNodes[i].iColor = Bmp.GetPixel(SnowNodes[i].postion.X, SnowNodes[i].postion.Y); SetPixel(Ptr, SnowNodes[i].postion.X, SnowNodes[i].postion.Y, 0xFEFfFE); continue; } Color col = Bmp.GetPixel(SnowNodes[i].postion.X, SnowNodes[i].postion.Y); if (col.Equals(Snowc))// { SnowNodes[i].postion.X = rand.Next(ScreenWidth - 2) + 1; SnowNodes[i].postion.Y = rand.Next(3); SnowNodes[i].iColor = Bmp.GetPixel(SnowNodes[i].postion.X, SnowNodes[i].postion.Y); SetPixel(Ptr, SnowNodes[i].postion.X, SnowNodes[i].postion.Y, 0xFEFfFE); continue; } if ((col.Equals(SnowS)) || (GetContrast(col, dx, i) > 30))//判断对比度 { SetPixel(Ptr, SnowNodes[i].postion.X, SnowNodes[i].postion.Y - 1, 0xFFFFFF); SetPixel(Ptr, SnowNodes[i].postion.X - 1, SnowNodes[i].postion.Y, 0xFFDDDD); SetPixel(Ptr, SnowNodes[i].postion.X + 1, SnowNodes[i].postion.Y, 0xFFDDDD); SnowNodes[i].postion.X = rand.Next(ScreenWidth - 2) + 1; SnowNodes[i].postion.Y = rand.Next(3); SnowNodes[i].iColor = Bmp.GetPixel(SnowNodes[i].postion.X, SnowNodes[i].postion.Y); SetPixel(Ptr, SnowNodes[i].postion.X, SnowNodes[i].postion.Y, 0xFEFfFE); continue; } SnowNodes[i].iColor = col; SetPixel(Ptr, SnowNodes[i].postion.X, SnowNodes[i].postion.Y, 0xFEFfFE); } DeleteDC(Ptr); Graphics G = Graphics.FromImage(Bmp); G.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(ScreenWidth, ScreenHeight));// G.Dispose(); } private void timer1_Tick(object sender, EventArgs e) { Random rand = new Random(); CrWind = rand.Next(4)- 2; } private void timer2_Tick(object sender, EventArgs e) { this.timer2.Enabled = false; MoveSnowNodes(); this.timer2.Interval = 2; this.timer2.Enabled = true; } } }
完成项目资源,请到下地址下载:
http://download.csdn.net/detail/sky_blue22/8577315
c#桌面飘雪小程序,兼容win7
|