Useful screensavers
Useful screensavers
Written by Harry Fairhead   
Thursday, 17 September 2009
Article Index
Useful screensavers
Bouncing text
Look and behavior

Bouncing text

We really need some graphics to show the sort of thing a screensaver can do. As an example we are going to bounce a message round the form. The simplest way of creating animation is to use a timer – so place a timer control on the form and change the ShowScreenSaver to:

void ShowScreenSaver()
this.TopLevel = true;
this.DoubleBuffered = true;
this.timer1.Interval = 20;
this.timer1.Enabled = true;

This sets the timer to 20 milliseconds which produces an update every 1/50th of a second. Setting DoubleBuffered to true makes the animation smooth. The Timer event handler does all the work. First it gets the graphics object associated with the form so that it can use it to draw:

private void timer1_Tick(
object sender, EventArgs e)
Graphics g = this.CreateGraphics();

As will become clear, you can’t assume that the screensaver is going to have a fixed sized form to draw on and everything should be done in terms of a fraction of the available space. To do this we first need the size of the display area:

RectangleF bounds =this.ClientRectangle;

Next we can set the message to display and a font size that is 1/20th of the height of the display area:

string displayM ="Visit I-Programmer";
int fontsize =(int) bounds.Height / 20;

We need to set a font to use and any font from the Arial family will do:

FontFamily fontFamily =
new FontFamily("Arial");
Font font = new Font(

To blank the message out at its current position we can use the TextRenderer object and its MeasureText method. This returns a size struct giving the height and width of the rectangle that the text fits into:

Size psize=new Size(int.MaxValue,
Size size = TextRenderer.
MeasureText(g, displayM,
font, psize,

Now that we have the size of the text the FillRectangle method can be used to draw a rectangle of just the correct size in the current form’s background colour:

SolidBrush b = new SolidBrush(
g.FillRectangle(b, textpos.X,

To move the text its position is updated and if it is just about to go off the edge of the form we reverse its direction of motion, i.e. bounce it:

textpos.X +=v.X;
textpos.Y +=v.Y;
if (textpos.X+size.Width >= bounds.Width
|| textpos.X<=0) v.X = -v.X;
if (textpos.Y +size.Height>= bounds.Height
|| textpos.Y < 0) v.Y = -v.Y;

Finally the text is drawn at its new position on the screen:

TextRenderer.DrawText(g, displayM,
font, textpos,

We also need some additional global variables:

private Point textpos = new Point(0, 0);
private Point v = new Point(1, 1);

If you now try the screensaver you should see the message bounce its way around the form and if you resize the form the text will automatically resize and bounce at the new edge.



Last Updated ( Friday, 25 September 2009 )

RSS feed of all content
I Programmer - full contents
Copyright © 2015 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.