In my earlier post, I created a simple app where one can create a form with the desired string glittering in different colours.
Writing a Simple Text thatĀ glitters
Now I have created a class to make this a little more flexible, i.e. this class can now be used to add glittering text to any control that inherits from System.Windows.Forms.Control.
using System;
using System.Drawing;
public class GlitterText
{
private string strGlitterText;
private System.Windows.Forms.Control GlitterControl;
private System.Timers.Timer GlitterTime;
private int X, Y,GlitterInterval;
public GlitterText(String _text,System.Windows.Forms.Control _GlitterControl,int _X=0,int _Y=0,int _GlitterInterval=100)
{
this.strGlitterText = _text;
this.GlitterControl = _GlitterControl;
this.X = _X;
this.Y = _Y;
this.GlitterInterval = _GlitterInterval;
InitialiseGlitterTimer();
}
private void InitialiseGlitterTimer()
{
this.GlitterTime = new System.Timers.Timer(this.GlitterInterval);
this.GlitterTime.AutoReset = true;
this.GlitterTime.Elapsed += new System.Timers.ElapsedEventHandler(GlitterControl_Paint);
this.GlitterTime.Start();
}
private void GlitterControl_Paint(object sender,EventArgs ea)
{
try
{
Random r = new Random(DateTime.Now.Millisecond);
Brush mybrush = new SolidBrush(Color.FromArgb(r.Next(255), r.Next(255), r.Next(255)));
Graphics g = this.GlitterControl.CreateGraphics();
g.DrawString(this.strGlitterText, new Font("Times New Roman", 14F), mybrush, new Point(this.X, this.Y));
}
catch { }
}
}