当前位置:   article > 正文

C# 实现代码雨_c#下雨效果

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++;
                    });
                });
            }
        }      
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/85157
推荐阅读
相关标签
  

闽ICP备14008679号