C# 实现代码雨

闲来无事,随便写下

效果图
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {

        #region 属性
        Bitmap Img;  //背景图片
        Graphics Graphics;
        int ticks = 0;
        //所有的线条集合
        Dictionary<int, List<MyLine>> Lines = new Dictionary<int, List<MyLine>>();
        Random _Random = new Random();
        //准备随机获取的字符集合
        char[] CharsDB = new char[] {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' };
        #endregion
        public Form3()
        {
            InitializeComponent();
           
        }
       
        private void Form3_Load(object sender, EventArgs e)
        {
            this.DoubleBuffered = true;
            this.FormBorderStyle = FormBorderStyle.None;
            this.BackColor = Color.Black;
            this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
            this.WindowState = FormWindowState.Maximized;
            InitGDI();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void InitGDI()
        {
            Img = new Bitmap(this.Width, this.Height);
            this.BackgroundImage = Img;
            Graphics = this.CreateGraphics();
            Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
            Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            timer1.Enabled = true;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            ticks += 10;
            if (ticks % 10 == 0)
            {
                ChangeLines();
                DrawLine();
            }

            if (ticks % 100 == 0)
            {
                DisposeLines();
            }
        }
        /// <summary>
        /// 改变线条的位置
        /// </summary>
        private void ChangeLines()
        {
            foreach (var item in Lines)
            {
                var Line = item.Value;
                for (int i = Line.Count - 1; i >= 0; i--)
                {
                    if (Line[i].Point.Y > this.Height)
                        Line.Remove(Line[i]);
                    else
                        Line[i].Point = new Point(Line[i].Point.X, Line[i].Point.Y+ Line[i].MoveSpeed);
                }
            }
        }
        /// <summary>
        /// 消除处理
        /// </summary>
        private void DisposeLines()
        {
            for (int i = 0; i < 4; i++)
            {
                if (!Lines.ContainsKey(i + 1))
                {
                    Lines[i + 1] = new List<MyLine>();
                }
                var font = new Font("微软雅黑",(i+1)*6,FontStyle.Bold);
                var startX = _Random.Next(0,this.Width);
                var Chars = GetChars(20);

                var height = 0f;
                var heights = new List<float>();
                foreach (var item in Chars)
                {
                    var size = Graphics.MeasureString(item+"",font);
                    height += size.Height;
                    heights.Add(height);
                }
                Lines[i + 1].Add(new MyLine
                {
                    Content = Chars,
                    Point = new Point(startX, -(int)height),
                    Level = i + 1,
                    Font = font,
                    MoveSpeed = _Random.Next(2, 20),
                    Heights = heights.ToArray()
                });
            }
        }
        /// <summary>
        /// 随机获取字母
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        private Char[] GetChars(int num)
        {           
            char[] result = new char[num];
            
            for (int i = 0; i < num; i++)
            {
                result[i] = CharsDB[_Random.Next(0, CharsDB.Length-1)];
            }
            return result;
        }
        /// <summary>
        /// 界面绘制
        /// </summary>
        private void DrawLine()
        {
            Img = new Bitmap(this.Width, this.Height);
            this.BackgroundImage = Img;
            Graphics = this.CreateGraphics();
            Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
            Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            Color shadowColor = Color.FromArgb(115, 215, 200);//光晕的颜色
            foreach (var item in Lines)
            {
                var line = item.Value;
                line.ForEach(t =>
                {
                    var index = 0;
                    t.Content = GetChars(20);
                    t.Content.ToList().ForEach(x =>
                    {                       
                        Graphics.DrawString(x + "", t.Font, new SolidBrush(Color.FromArgb(255 - (255 - (index + 1) * 8),255,100)), new Point() {X=t.Point.X,Y=(int)(t.Point.Y-t.Heights[index]) });
                        index++;
                    });
                });
            }
        }      
    }
}

参考链接:https://live.csdn.net/v/182162?spm=1001.2014.3001.5501

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yangzm996

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值