C# 实现撒花特效

导读:本篇文章讲解 C# 实现撒花特效,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

这两天为了用户体验加了一个撒花的效果,记录如下:

基本逻辑:准备一些小的图片,备用!然后新建一个窗体,设置为透明模式,然后加一个定时器,一个list列表(用于存储多个图片的位置,下降速度,旋转角度),在定时器中,随机选取图片,计算(随机)下降速度,旋转角度,计算当前的位置  存到list中。然后将list中的所有图片绘制到一个bitmap上,然后更新到界面上即可!

 

代码如下,有需要的可以参考:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace 撒花特效
{
    public partial class Form1 : Form
    {
        private static readonly Random rand = new Random();
        private readonly List<SnowFlake> SnowFlakes = new List<SnowFlake>();
        private int Tick = 0;
        private class SnowFlake
        {
            public float Rotation;//旋转角度
            public float RotVelocity;//旋转速率
            public float Scale;//图像缩放比例
            public float X;//X坐标值
            public float XVelocity;//X改变速率
            public float Y;//Y坐标值
            public float YVelocity;//Y改变速率
            public int type = 0;//图像类型
            //0 普通图像,1 不旋转图像,2 只下降的图像
            public Image image;//源图像
        }

        System.Media.SoundPlayer sndPlayer;
        private Boolean on = true;
        Image screenImage;

        public Form1()
        {
            InitializeComponent();
            SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);
        }

        private void SetBackground(Image img)
        {
            try
            {
                Bitmap bitmap = (Bitmap)img;
                if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
                {
                    throw new ApplicationException();
                }
                IntPtr hObject = IntPtr.Zero;
                IntPtr zero = IntPtr.Zero;
                IntPtr hDC = Win32.GetDC(IntPtr.Zero);
                IntPtr ptr2 = Win32.CreateCompatibleDC(hDC);
                try
                {
                    hObject = bitmap.GetHbitmap(Color.FromArgb(0));
                    zero = Win32.SelectObject(ptr2, hObject);
                    Win32.Size size2 = new Win32.Size(bitmap.Width, bitmap.Height);
                    Win32.Size psize = size2;
                    Win32.Point point3 = new Win32.Point(0, 0);
                    Win32.Point pprSrc = point3;
                    point3 = new Win32.Point(base.Left, base.Top);
                    Win32.Point pptDst = point3;
                    Win32.BLENDFUNCTION pblend = new Win32.BLENDFUNCTION();
                    pblend.BlendOp = 0;
                    pblend.BlendFlags = 0;
                    pblend.SourceConstantAlpha = 0xff;
                    pblend.AlphaFormat = 1;
                    Win32.UpdateLayeredWindow(this.Handle, hDC, ref pptDst, ref psize, ptr2, ref pprSrc, 0, ref pblend, 2);
                }
                catch (Exception exception1)
                {
                    Exception exception = exception1;
                    throw exception;
                }
                finally
                {
                    Win32.ReleaseDC(IntPtr.Zero, hDC);
                    if (hObject != IntPtr.Zero)
                    {
                        Win32.SelectObject(ptr2, zero);
                        Win32.DeleteObject(hObject);
                    }
                    Win32.DeleteDC(ptr2);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        protected override System.Windows.Forms.CreateParams CreateParams
        {
            get
            {
                System.Windows.Forms.CreateParams createParams = base.CreateParams;
                createParams.ExStyle |= 0x80000;
                return createParams;
            }
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            Tick++;

            if (Tick % 2 == 0)
            {
                if (Tick < 200)
                {
                    SnowFlake s = new SnowFlake();
                    Random rd = new Random();
                    int ran;//随机图像加载
                    s.X = rand.Next(-20, this.Width + 20);//在水平方向随机产生一个位置
                    s.Y = 0;//从顶部出现
                    s.XVelocity = (float)(rand.NextDouble() – 0.5f) * 2f;//随机产生水平漂移速度
                    s.YVelocity = rand.Next(3) + 5 ; //(float)(rand.NextDouble() * 5.5) + 1f;//随机产生垂直变换速度
                    s.Rotation = rand.Next(0, 359);//随机产生旋转初始角度
                    s.RotVelocity = rand.Next(-2, 2);//随机产生旋转变换角速率
                    if (Tick % 1000 == 0)
                    {
                        ran = 6;
                        s.type = 2;
                    }
                    else
                    {
                        ran = rd.Next(100) % 9 + 1;
                        if (ran == 5)//不支持旋转
                            s.type = 1;
                    }
                    s.image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + “\\” + ran + “.png”);//加载图像
                    if (s.RotVelocity == 0)
                    {
                        s.RotVelocity = 2;
                    }
                    s.Scale = (float)(rand.NextDouble() / 2) + 0.75f;//随机产生放缩比例
                    SnowFlakes.Add(s);
                }
            }

//这里做了控制,图片全部消失后,关闭窗口

            if (Tick > 5 && SnowFlakes.Count == 0)
            {
                timer1.Stop();
                this.Close();
            }

            Graphics g = Graphics.FromImage(screenImage);
            g.Clear(Color.Transparent);
            g.SmoothingMode = SmoothingMode.HighSpeed;

            for (int i = 0; i < SnowFlakes.Count; i++)
            {
                SnowFlake s1 = SnowFlakes[i];
                if (s1.type != 2)
                    s1.X += s1.XVelocity;//水平漂移

                s1.Y += s1.YVelocity;//垂直漂移

                if (s1.type == 0)
                    s1.Rotation += s1.RotVelocity;//旋转
                else
                    s1.Rotation = 0;

                s1.XVelocity += ((float)rand.NextDouble() – 0.5f) * 0.7f;//刷新水平速率
                s1.XVelocity = Math.Max(s1.XVelocity, -2f);
                s1.XVelocity = Math.Min(s1.XVelocity, +2f);

                if (s1.Y > this.Height)//若元素下降到底部,销毁此元素
                {
                    SnowFlakes.RemoveAt(i);
                }
                else
                {
                    g.ResetTransform();
                    g.TranslateTransform(-16, -16, MatrixOrder.Append); //pan
                    g.ScaleTransform(s1.Scale, s1.Scale, MatrixOrder.Append); //scale
                    g.RotateTransform(s1.Rotation, MatrixOrder.Append); //rotate
                    g.TranslateTransform(s1.X, s1.Y, MatrixOrder.Append); //pan
                    g.DrawImage(s1.image, 0, 0); //draw
                }
            }
            g.Dispose();
            SetBackground(screenImage);
        }

      

        private void Form1_Load_1(object sender, EventArgs e)
        {
            screenImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            //  //播放声音
              sndPlayer = new System.Media.SoundPlayer(Application.StartupPath + @”/9347.wav”);
              sndPlayer.PlayLooping();
            timer1.Tick += timer_Tick;
        }
    }
}

源码已经上传:https://download.csdn.net/download/yunxiaobaobei/12691236

参考文章:http://blog.sina.com.cn/s/blog_9be6dec101011q5q.html#cmt_51A845CD-7F000001-6ADEC17E-87C-8A0

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/51839.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!