赞
踩
闲来无事,随便写下
效果图
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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。