![C# 桌面雪花飘飘 雪花飘飘洒洒](http://img.aihuau.com/images/31101031/31044528t01971616c17f2249d1.jpg)
usingSystem;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.Drawing.Drawing2D;usingSystem.Drawing.Imaging;usingSystem.Runtime.InteropServices;usingSystem.Windows.Forms; namespace雪花飘飘{publicpartialclassForm1:Form{privateBitmapm_Snow;privateImagescreenImage;privateTimertimer=newTimer();privatestaticreadonlyRandomrand=newRandom();privatereadonlyList<SnowFlake>SnowFlakes=newList<SnowFlake>();privateintTick=0; publicForm1(){this.components=newSystem.ComponentModel.Container();this.timer=newSystem.Windows.Forms.Timer(this.components);this.SuspendLayout();////timer//this.timer.Enabled=true;this.timer.Interval=100;this.timer.Tick+=newSystem.EventHandler(this.OnTick);////Form1//this.ControlBox=false;this.ShowInTaskbar=false;this.StartPosition=System.Windows.Forms.FormStartPosition.CenterScreen;this.TopMost=true;this.WindowState=System.Windows.Forms.FormWindowState.Maximized;this.ResumeLayout(false); SetStyle(ControlStyles.UserPaint|ControlStyles.AllPaintingInWmPaint|ControlStyles.DoubleBuffer,true); screenImage=newBitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height); } privatevoidOnTick(objectsender,EventArgsargs){Tick++;//newsnowflake//控制雪量if(Tick%5==0&&rand.NextDouble()<0.30){SnowFlakes=newSnowFlake();s.X=rand.Next(-20,this.Width+20);s.Y=0f;s.XVelocity=(float)(rand.NextDouble()-0.5f)*2f;s.YVelocity=(float)(rand.NextDouble()*3)+1f;s.Rotation=rand.Next(0,359);s.RotVelocity=rand.Next(-3,3)*2;if(s.RotVelocity==0){s.RotVelocity=3;}s.Scale=(float)(rand.NextDouble()/2)+0.75f;SnowFlakes.Add(s);}//TodrawsnowflakeGraphicsg=Graphics.FromImage(screenImage);g.Clear(Color.Transparent);g.SmoothingMode=SmoothingMode.HighSpeed; for(inti=0;i<SnowFlakes.Count;i++){SnowFlakes=SnowFlakes[i];s.X+=s.XVelocity;s.Y+=s.YVelocity;s.Rotation+=s.RotVelocity;s.XVelocity+=((float)rand.NextDouble()-0.5f)*0.7f;s.XVelocity=Math.Max(s.XVelocity,-2f);s.XVelocity=Math.Min(s.XVelocity,+2f);//如果雪下到屏幕下面后就让他消失if(s.Y>this.Height){SnowFlakes.RemoveAt(i);}//没有落到屏幕下面的时候就继续让它往下落else{g.ResetTransform();g.TranslateTransform(-16,-16,MatrixOrder.Append);//pang.ScaleTransform(s.Scale,s.Scale,MatrixOrder.Append);//scaleg.RotateTransform(s.Rotation,MatrixOrder.Append);//rotateg.TranslateTransform(s.X,s.Y,MatrixOrder.Append);//pang.DrawImage(Snow,0,0);//draw}}g.Dispose();SetBackground(screenImage);} privateclassSnowFlake{publicfloatRotation;publicfloatRotVelocity;publicfloatScale;publicfloatX;publicfloatXVelocity;publicfloatY;publicfloatYVelocity;} privatevoidSetBackground(Imageimg){try{Bitmapbitmap=(Bitmap)img;if(bitmap.PixelFormat!=PixelFormat.Format32bppArgb){thrownewApplicationException();}IntPtrhObject=IntPtr.Zero;IntPtrzero=IntPtr.Zero;IntPtrhDC=Win32.GetDC(IntPtr.Zero);IntPtrptr2=Win32.CreateCompatibleDC(hDC);try{hObject=bitmap.GetHbitmap(Color.FromArgb(0));zero=Win32.SelectObject(ptr2,hObject);Win32.Sizesize2=newWin32.Size(bitmap.Width,bitmap.Height);Win32.Sizepsize=size2;Win32.Pointpoint3=newWin32.Point(0,0);Win32.PointpprSrc=point3;point3=newWin32.Point(base.Left,base.Top);Win32.PointpptDst=point3;Win32.BLENDFUNCTIONpblend=newWin32.BLENDFUNCTION();pblend.BlendOp=0;pblend.BlendFlags=0;pblend.SourceConstantAlpha=0xff;pblend.AlphaFormat=1;Win32.UpdateLayeredWindow(this.Handle,hDC,refpptDst,refpsize,ptr2,refpprSrc,0,refpblend,2);}catch(Exceptionexception1){Exceptionexception=exception1;throwexception;}finally{Win32.ReleaseDC(IntPtr.Zero,hDC);if(hObject!=IntPtr.Zero){Win32.SelectObject(ptr2,zero);Win32.DeleteObject(hObject);}Win32.DeleteDC(ptr2);}}catch(Exceptione){MessageBox.Show(e.Message);}} protectedoverrideSystem.Windows.Forms.CreateParamsCreateParams{get{System.Windows.Forms.CreateParamscreateParams=base.CreateParams;createParams.ExStyle|=0x80000;returncreateParams;}} privateBitmapSnow{get{if(m_Snow==null){m_Snow=newBitmap(32,32);using(Graphicsg=Graphics.FromImage(m_Snow)){g.SmoothingMode=SmoothingMode.AntiAlias;g.Clear(Color.Transparent);g.TranslateTransform(16,16,MatrixOrder.Append);Colorblack=Color.FromArgb(1,1,1);Colorwhite=Color.FromArgb(255,255,255);DrawSnow(g,newSolidBrush(black),newPen(black,3f));DrawSnow(g,newSolidBrush(white),newPen(white,2f));g.Save();}}returnm_Snow;}} privatestaticvoidDrawSnow(Graphicsg,Brushb,Penp){constinta=6;constinta2=a+2;constintr=2;g.DrawLine(p,-a,-a,+a,+a);g.DrawLine(p,-a,+a,+a,-a);g.DrawLine(p,-a2,0,+a2,0);g.DrawLine(p,0,-a2,0,+a2);g.FillEllipse(b,-r,-r,r*2,r*2);} }internalclassWin32{publicconstbyteAC_SRC_ALPHA=1;publicconstbyteAC_SRC_OVER=0;publicconstintULW_ALPHA=2; [DllImport("gdi32.dll")]publicstaticexternIntPtrCreateCompatibleDC(IntPtrhDC);[DllImport("gdi32.dll")]publicstaticexternBoolDeleteDC(IntPtrhdc);[DllImport("gdi32.dll")]publicstaticexternBoolDeleteObject(IntPtrhObject);[DllImport("user32.dll")]publicstaticexternIntPtrGetDC(IntPtrhandle);[DllImport("user32.dll",ExactSpelling=true)]publicstaticexternintReleaseDC(IntPtrhandle,IntPtrhDC);[DllImport("gdi32.dll")]publicstaticexternIntPtrSelectObject(IntPtrhDC,IntPtrhObject);[DllImport("user32.dll")]publicstaticexternBoolUpdateLayeredWindow(IntPtrhandle,IntPtrhdcDst,refPointpptDst,refSizepsize,IntPtrhdcSrc,refPointpprSrc,intcrKey,refBLENDFUNCTIONpblend,intdwFlags); [StructLayout(LayoutKind.Sequential)]publicstructBLENDFUNCTION{publicbyteBlendOp;publicbyteBlendFlags;publicbyteSourceConstantAlpha;publicbyteAlphaFormat;} publicenumBool{False,True} [StructLayout(LayoutKind.Sequential)]publicstructPoint{publicintx;publicinty;publicPoint(intx,inty){this=newWin32.Point();this.x=x;this.y=y;}} [StructLayout(LayoutKind.Sequential)]publicstructSize{publicintcx;publicintcy;publicSize(intcx,intcy){this=newWin32.Size();this.cx=cx;this.cy=cy;}}} }