Writing a Simple Text that glitters

Posted by CorneringEki on January 27, 2012

A few days ago I was trying to create a text with different colours, and suddenly I had an idea; Why not create a text that changes colours randomly to produce a glittering effect...

All I needed was:
1. A timer,
2. A Random number to fill the rgb,
3. An event handler that would paint the form everytime the timer ticked.

This is a simple program, and does not require any explanations, whatsoever.


public partial class Form1 : Form
{
public Form1()
{

InitializeComponent();

}

private string strGlitterText;

private void Form1_Paint(object sender,EventArgs e)
{

Random r = new Random(DateTime.Now.Millisecond);
Brush mybrush = new SolidBrush(Color.FromArgb(r.Next(255), r.Next(255), r.Next(255)));
try
{
this.strGlitterText = "Glitter";
Graphics g = this.CreateGraphics();
g.DrawString(this.strGlitterText, new Font("Times New Roman", 14F), mybrush, new Point(30, 30));
}
catch (Exception f)
{ }

}

}

namespace GlitterText
{
partial class Form1
{
///

/// Required designer variable.
///

private System.ComponentModel.IContainer components = null;

///

/// Clean up any resources being used.
///

/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

///

/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

//Timer to change the colour of the text
this.GlitterTime = new System.Timers.Timer(10);
//Repaint the form window when the timer ticks
this.GlitterTime.Elapsed += new System.Timers.ElapsedEventHandler(Form1_Paint);
this.GlitterTime.AutoReset = true;
this.GlitterTime.Start();

this.Text = "Glitter";

this.ClientSize = new System.Drawing.Size(1280, 1024);
//Whenever repainting is required, the same event is called.
this.Paint+=new System.Windows.Forms.PaintEventHandler(Form1_Paint);
}

#endregion
private System.Timers.Timer GlitterTime;
}
}



[caption id="attachment_25" align="aligncenter" width="300" caption="Purple Colour"]Glittering text[/caption][caption id="attachment_26" align="aligncenter" width="300" caption="Green Colour"]Glittering text[/caption]

I have created a library to add this text to any UI control, and providing the position of the text or to center it by default.

Happy Coding,
Eklavya Mirani