在做一个WinForm登录框时,突然想到,如果有黑客帝国中字符雨的特效做背景,那应该蛮Cool的,所以就有了如下代码,随意写的,有点乱。
public partial class CharacterRain : Component
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (RainThread != null && RainThread.IsAlive)
Stop();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (disposing && (components != null))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
components.Dispose();
}
base.Dispose(disposing);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
组件设计器生成的代码#region 组件设计器生成的代码
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
components = new System.ComponentModel.Container();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public CharacterRain()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Initialize();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
InitializeComponent();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public CharacterRain(IContainer container)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Initialize();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
container.Add(this);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
InitializeComponent();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private object BMPLock = new object();
private Bitmap BMP;
private Graphics Graph;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void Initialize()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.DrawRainEvent = new EventHandler(CharacterRainPanel_DrawRainEvent);
OnPaint = new PaintEventHandler(ShowWindow_Paint);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private PaintEventHandler OnPaint;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private char[] CHARACTERS =
{ '`', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\\', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '|', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '
{', '}', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?' };
private static readonly Random Rand = new Random();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private Control _ShowWindow = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取、设置用于显示字符雨的窗口(或控件)
/// </summary>
public Control ShowWindow
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _ShowWindow; }
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (_ShowWindow != null)
_ShowWindow.Paint -= OnPaint;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
_ShowWindow = value;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (_ShowWindow != null)
_ShowWindow.Paint += OnPaint;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void ShowWindow_Paint(object sender, PaintEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//Paint();
if (BMP != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lock (BMPLock)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//e.Graphics.Clear(_ShowWindow.BackColor);
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
e.Graphics.DrawImage(BMP, 0, 0);
}
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private string _RainCharacters = "`1234567890-=\\qwertyuiop[]asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+|QWERTYUIOP{}ASDFGHJKL:\"ZXCVBNM<>?";
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取、设置字符雨中可能出现的字符
/// </summary>
public string RainCharacters
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _RainCharacters; }
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (string.IsNullOrEmpty(value))
value = "`1234567890-=\\qwertyuiop[]asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+|QWERTYUIOP{}ASDFGHJKL:\"ZXCVBNM<>?";
_RainCharacters = value;
CHARACTERS = value.ToCharArray();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private Color _RainHeadColor = Color.Lime;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取、设置字符雨头的颜色
/// </summary>
public Color RainHeadColor
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _RainHeadColor; }
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!Running)
_RainHeadColor = value;
else
throw new Exception("运行中不可以更改颜色。");
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private Color _RainBodyColor = Color.Green;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取、设置字符雨体的颜色
/// </summary>
public Color RainBodyColor
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _RainBodyColor; }
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!Running)
_RainBodyColor = value;
else
throw new Exception("运行中不可以更改颜色。");
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private Color _RainTailColor = Color.DarkGreen;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取、设置字符雨体的颜色
/// </summary>
public Color RainTailColor
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _RainTailColor; }
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!Running)
_RainTailColor = value;
else
throw new Exception("运行中不可以更改颜色。");
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private Font _RainFont = new Font(new FontFamily("Consolas"), 12);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取、设置字符雨的字体样式
/// </summary>
public Font RainFont
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _RainFont; }
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!Running)
_RainFont = value;
else
throw new Exception("运行中不可以更改字体。");
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private int _StreamsCount = 100;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取、设置字符雨的数量
/// </summary>
public int StreamsCount
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _StreamsCount; }
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!Running)
_StreamsCount = value;
else
throw new Exception("运行中不可以更改数量。");
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private int _MaxLength = 50;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取、设置每个雨滴的最大字符数
/// </summary>
public int MaxLength
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _MaxLength; }
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!Running)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (value >= _MinLength)
_MaxLength = value;
else
throw new Exception("最大字符数不能小于最小字符数。");
}
else
throw new Exception("运行中不可以更改数量。");
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private int _MinLength = 20;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取、设置每个雨滴的最小字符数
/// </summary>
public int MinLength
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _MinLength; }
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!Running)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (value <= _MaxLength)
_MinLength = value;
else
throw new Exception("最小字符数不能大于最大字符数。");
}
else
throw new Exception("运行中不可以更改数量。");
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public bool Running
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return RainThread != null && RainThread.IsAlive; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private Thread RainThread;
private bool RUN;
private bool PAUSE;
private Brush BackBrush = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private Brush RainHeadBrush = null;
private Brush RainBodyBrush = null;
private Brush RainTailBrush = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 开始显示字符雨
/// </summary>
public void Start()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (_ShowWindow == null)
throw new Exception("没有用于显示的窗口(或控件)");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (!Running)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
RainThread = new Thread(new ParameterizedThreadStart(RainProcess));
RUN = true;
PAUSE = false;
Ready();
RainThread.Start();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void Ready()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Rain.RainFont = _RainFont;
Rain.RainHeadColor = _RainHeadColor;
Rain.RainBodyColor = _RainBodyColor;
Rain.RainTailColor = _RainTailColor;
Rain.BackColor = _ShowWindow.BackColor;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (RainHeadBrush != null)
RainHeadBrush.Dispose();
if (RainBodyBrush != null)
RainBodyBrush.Dispose();
if (RainTailBrush != null)
RainTailBrush.Dispose();
if (BackBrush != null)
BackBrush.Dispose();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
RainHeadBrush = new SolidBrush(Rain.RainHeadColor);
RainBodyBrush = new SolidBrush(Rain.RainBodyColor);
RainTailBrush = new SolidBrush(Rain.RainTailColor);
BackBrush = new SolidBrush(Rain.BackColor);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//RainItems.Clear();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
using (Graphics g = _ShowWindow.CreateGraphics())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
g.Clear(Rain.BackColor);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private EventHandler DrawRainEvent;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void CharacterRainPanel_DrawRainEvent(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//foreach (Rain.Item item in e.Items)
//{
// _ShowWindow.Invalidate(new Rectangle(item.X, item.Y, item.Width, item.Height));
// _ShowWindow.Update();
//}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
using (Graphics g = _ShowWindow.CreateGraphics())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
g.DrawImage(BMP, 0, 0);
}
//_ShowWindow.Invalidate(true);
//_ShowWindow.Refresh();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//private List<Rain.Item> RainItems = new List<Rain.Item>();
private object RainLOCK = new object();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void RainProcess(object Param)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
using (BMP = new Bitmap(_ShowWindow.Width, _ShowWindow.Height))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
using (Graph = Graphics.FromImage(BMP))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
List<Rain> Rains = new List<Rain>();
for (int i = 0; i < _StreamsCount; i++)
Rains.Add(new Rain(_MinLength + Rand.Next(_MaxLength - _MinLength)));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
while (RUN)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (PAUSE)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Thread.Sleep(300);
continue;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DoRain(Rains);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Thread.Sleep(50);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (Rain r in Rains)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
r.Uninit();
}
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void DoRain(List<Rain> Rains)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
for (int i = 0; i < Rains.Count; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Rain rain = Rains[i];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (!rain.Inited)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!rain.Init(_ShowWindow.Width, Rand.Next(_ShowWindow.Height / 3)))
continue;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Rain.Item item = rain.GetItem(CHARACTERS[Rand.Next(CHARACTERS.Length)]);
Rain.Item prior = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (item == null)
continue;
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lock (BMPLock)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (item.Y <= BMP.Height)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (item.Type == Rain.Item.RainItemType.Head && rain.Prior.Type != Rain.Item.RainItemType.Tail)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
rain.Prior.Type = Rain.Item.RainItemType.Body;
prior = rain.Prior;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (item.Type == Rain.Item.RainItemType.Back)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
prior = rain.Prior;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
switch (item.Type)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case Rain.Item.RainItemType.Head:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Graph.DrawString(new string(item.CH, 1), _RainFont, RainHeadBrush, item.X, item.Y);
break;
}
case Rain.Item.RainItemType.Body:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Graph.DrawString(new string(item.CH, 1), _RainFont, RainBodyBrush, item.X, item.Y);
break;
}
case Rain.Item.RainItemType.Tail:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Graph.DrawString(new string(item.CH, 1), _RainFont, RainTailBrush, item.X, item.Y);
break;
}
case Rain.Item.RainItemType.Back:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Graph.FillRectangle(BackBrush, item.X, item.Y, item.Width, item.Height);
break;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (prior != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
switch (prior.Type)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case Rain.Item.RainItemType.Head:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Graph.DrawString(new string(prior.CH, 1), _RainFont, RainHeadBrush, prior.X, prior.Y);
break;
}
case Rain.Item.RainItemType.Body:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Graph.DrawString(new string(prior.CH, 1), _RainFont, RainBodyBrush, prior.X, prior.Y);
break;
}
case Rain.Item.RainItemType.Tail:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Graph.DrawString(new string(prior.CH, 1), _RainFont, RainTailBrush, prior.X, prior.Y);
break;
}
case Rain.Item.RainItemType.Back:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Graph.FillRectangle(BackBrush, prior.X, prior.Y, prior.Width, prior.Height);
break;
}
}
}
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
item.Owner.Way = Rain.RainWay.Reverse;
}
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!_ShowWindow.Disposing)
_ShowWindow.Invoke(DrawRainEvent, this, new EventArgs());
}
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 停止显示字符雨
/// </summary>
public void Stop()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
RUN = false;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 暂停显示
/// </summary>
public void Pause()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
PAUSE = true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 恢复暂停
/// </summary>
public void Resume()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
PAUSE = false;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//protected void Paint()
//{
// lock (RainLOCK)
// {
// for (int i = RainItems.Count - 1; i >= 0; i--)
// {
// Rain.Item item = RainItems[i];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// if (item.X < BMP.Width)
// {
// if (item.Y < BMP.Height)
// {
// //if (e.ClipRectangle.X <= item.X && e.ClipRectangle.Y <= item.Y && e.ClipRectangle.Width >= item.Width && e.ClipRectangle.Height >= item.Height)
// {
// switch (item.Type)
// {
// case Rain.Item.RainItemType.Head:
// {
// e.Graphics.DrawString(new string(item.CH, 1), _RainFont, RainHeadBrush, item.X, item.Y);
// break;
// }
// case Rain.Item.RainItemType.Body:
// {
// e.Graphics.DrawString(new string(item.CH, 1), _RainFont, RainBodyBrush, item.X, item.Y);
// break;
// }
// case Rain.Item.RainItemType.Tail:
// {
// e.Graphics.DrawString(new string(item.CH, 1), _RainFont, RainTailBrush, item.X, item.Y);
// break;
// }
// case Rain.Item.RainItemType.Back:
// {
// e.Graphics.FillRectangle(BackBrush, item.X, item.Y, item.Width, item.Height);
// break;
// }
// }
// }
// }
// else
// {
// item.Owner.Way = Rain.RainWay.Reverse;
// }
// }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// if (item.Type == Rain.Item.RainItemType.Back)
// {
// RainItems.Remove(item);
// }
// }
// }
//}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private class Rain
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private static Font _RainFont;
public static Font RainFont
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _RainFont; }
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_RainFont = value;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
_RainHeight = (int)(value.Size * value.FontFamily.GetLineSpacing(FontStyle.Regular) / value.FontFamily.GetEmHeight(FontStyle.Regular)) + 1;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static Color BackColor;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static Color RainBodyColor;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static Color RainHeadColor;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static Color RainTailColor;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static int _RainHeight;
public static int RainHeight
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _RainHeight; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static object RangeLock = new object();
private static List<Rectangle> Ranges = new List<Rectangle>();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Queue<Item> Chars = new Queue<Item>();
private int _Length;
private int _BeginX;
private int _BeginY;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private RainWay _Way;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public RainWay Way
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _Way; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _Way = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private Rectangle Range;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public enum RainWay : byte
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 正向
/// </summary>
Obverse = 0,
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 反向
/// </summary>
Reverse = 1
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public bool Inited
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return Chars.Count > 0; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public bool Init(int Width, int Y)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
bool Ret = true;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Uninit();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (Rand.Next(30) < 10)
return false;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
_BeginX = Rand.Next(Width);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Size s = TextRenderer.MeasureText(" ", _RainFont);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Range = new Rectangle(_BeginX, Y, s.Width, s.Height * _Length);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (Rectangle r in Ranges)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (r.Contains(this.Range) || r.IntersectsWith(this.Range))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Ret = false;
break;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (Ret)
Ranges.Add(this.Range);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
_BeginY = Y;
_Way = RainWay.Obverse;
_Prior = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return Ret;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Uninit()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.Range != null && Ranges.Contains(this.Range))
Ranges.Remove(this.Range);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
item#region item
public class Item
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private Rain _Owner;
public Rain Owner
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _Owner; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public enum RainItemType : byte
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Head = 0,
Body = 1,
Tail = 2,
Back = 3
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private char _CH;
public char CH
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _CH; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private int _X;
public int X
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _X; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private int _Y;
public int Y
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _Y; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private int _Width;
public int Width
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _Width; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private int _Height;
public int Height
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _Height; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private RainItemType _Type;
public RainItemType Type
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _Type; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _Type = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Item(Rain Owner, char CH, int X, int Y, RainItemType Type)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._Owner = Owner;
this._CH = CH;
this._X = X;
this._Y = Y;
Size s = TextRenderer.MeasureText(new string(CH, 1), RainFont);
this._Width = s.Width;
this._Height = s.Height;
this._Type = Type;
}
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Rain(int Length)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_Length = Length;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Item GetItem(char CH)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Item Ret = null;
if (_Way == RainWay.Obverse)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Ret = Add(CH);
if (Ret == null)
_Way = RainWay.Reverse;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Ret = Remove();
if (Ret == null)
_Way = RainWay.Obverse;
}
return Ret;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private Item _Prior = null;
public Item Prior
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _Prior;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private Item Add(char CH)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Item Ret = null;
if (Chars.Count < _Length)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Ret = new Item(this, CH, _BeginX, _BeginY + RainHeight * Chars.Count, Chars.Count == 0 ? Item.RainItemType.Tail : Item.RainItemType.Head);
if (Ret.Type == Item.RainItemType.Head)
_Prior = Chars.Last();
Chars.Enqueue(Ret);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return Ret;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private Item Remove()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Item Ret = null;
if (Chars.Count > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Ret = Chars.Dequeue();
Ret.Type = Item.RainItemType.Back;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (Chars.Count > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_Prior = Chars.First();
_Prior.Type = Item.RainItemType.Tail;
}
else
_Prior = null;
}
return Ret;
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
测试程序下载
使用的时候,先设定ShowWindow,这个属性决定在哪个控件上显示字符雨,然后还可以设置如下属性:
MaxLength:每条字符雨的最大字符数量
MinLength:每条字符雨的最小字符数量
RainBodyColor:字符雨中间的颜色
RainCharacters:字符雨可以使用到的字符
FainFont:字体
RainHeadColor:最前面的字符的颜色
RainTailColor:最后面字符的颜色
StreamsCount:字符雨的数量
还有如下方法可供调用:
Start:开始显示
Stop:停止显示
Pause:暂停显示
Resume:恢复暂停