c#桌面飘雪小程序,兼容win7

这是一个使用C#编写的桌面飘雪小程序,经过修改后能够兼容Windows 7系统。程序通过GDI+库实现雪花飘落效果,并提供了定时更新雪花位置的机制。

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印


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


开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值