展开全部
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;
namespace WindowsApplication6
{
public partial class Form1 : Form
{
private class SnowFlake
{
public float Rotation;
public float RotVelocity;
public float Scale;
public float X;
public float XVelocity;
public float Y;
public float YVelocity;
}
private Bitmap m_Snow;
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0xF010;
public const int HTCAPTION = 0x0002;
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
int weigth;
int heigth;
private static readonly Random Random = new Random();
Random rb = new Random();
private readonly List SnowFlakes = new List();
private int Tick = 0;
private void Form1_Load(object sender, EventArgs e)
{
weigth = Screen.PrimaryScreen.Bounds.Width;
heigth = Screen.PrimaryScreen.Bounds.Width;
IntPtr Wnd = FindWindow("Progman", "Program Manager");
SetParent(this.Handle, Wnd);
}
private Bitmap Snow
{
get
{
if (m_Snow == null)
{
m_Snow = new Bitmap(32, 32);
using (Graphics g = Graphics.FromImage(m_Snow))
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.Clear(Color.Transparent);
g.TranslateTransform(16, 16, MatrixOrder.Append);
Color black = Color.FromArgb(1, 1, 1);
Color white = Color.FromArgb(255, 255, 255);
DrawSnow(g, new SolidBrush(black), new Pen(black, 3f));
DrawSnow(g, new SolidBrush(white), new Pen(white, 2f));
g.Save();
}
}
return m_Snow;
}
}
public Form1()
{
InitializeComponent();
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);
}
private void OnTick(object sender, EventArgs args)
{
Tick++;
//衍生新的雪花
if (Tick % 5 == 0 && Random.NextDouble() < 0.30)
{
SnowFlake s = new SnowFlake();
s.X = Random.Next(-50, Width + 50);
s.Y = Random.Next(-20, -7);
s.XVelocity = (float)(Random.NextDouble() - 0.5f) * 2f;
s.YVelocity = (float)(Random.NextDouble() * 3) + 1f;
s.Rotation = Random.Next(0, 359);
s.RotVelocity = Random.Next(-3, 3) * 2;
if (s.RotVelocity == 0)
{
s.RotVelocity = 3;
}
s.Scale = (float)(Random.NextDouble() / 2) + 0.75f;
SnowFlakes.Add(s);
}
//需要移除的雪花。62616964757a686964616fe78988e69d8331333262343065
List del = new List();
foreach (SnowFlake s in SnowFlakes)
{
s.X += s.XVelocity;
s.Y += s.YVelocity;
s.Rotation += s.RotVelocity;
s.XVelocity += ((float)Random.NextDouble() - 0.5f) * 0.7f;
s.XVelocity = Math.Max(s.XVelocity, -2f);
s.XVelocity = Math.Min(s.XVelocity, +2f);
if (s.YVelocity > Height + 10)
{
del.Add(s);
}
}
//删除
foreach (SnowFlake s in del)
{
SnowFlakes.Remove(s);
}
//刷新
Refresh();
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
foreach (SnowFlake s in SnowFlakes)
{
g.ResetTransform();
g.TranslateTransform(-16, -16, MatrixOrder.Append);
g.ScaleTransform(s.Scale, s.Scale, MatrixOrder.Append);
g.RotateTransform(s.Rotation, MatrixOrder.Append);
g.TranslateTransform(s.X, s.Y, MatrixOrder.Append);
g.DrawImage(Snow, 0, 0);
}
}
private static void DrawSnow(Graphics g, Brush b, Pen p)
{
const int a = 6;
const int a2 = a + 2;
const int r = 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);
}
}
}
已赞过
已踩过<
你对这个回答的评价是?
评论
收起