Simple Windows Screensaver by ra00l


In this article, I will explain how to create and install a simple windows screensaver, similar to the Marquee screensaver; witch is installed on your system with Microsoft Windows. Screensavers are actually executable files, only difference is that they have a scr extension instead of exe. Before we begin, there are a few things to know about Windows screensavers. Screensavers are located in the [SystemFolder]system32. After a specified period of time the computer is idle (time witch is set trough the Display Properties -> Screen Saver), Windows is making a call to the current screesnsaver, telling him to start. When this happens, the specified screensaver starts executing, until the user presses/moves the mouse and/or types a keyboard key.

Most screensavers have also a configuration dialog, where you can set different options for the screensaver. These options can be saved either in the registry or in a configuration file.

The part where windows start the screensaver looks pretty easy, the hard part is where we must know when the user pressed the Settings button, so that we can display the settings dialog. Here is how it works. When the user presses the settings button, Windows is calling the current screensaver with the parameter “/c” followed by some numbers.

To start the screensaver, the current screensaver is called, along with the “/s” parameter.

Let’s now start designing our screensaver. As I told you earlier, we will do something simple, similar to the Marquee windows screensaver.

This article is accompanied by a demo screensaver along with the source code, to help you get started! In the following, I will present the parts that are important in the process of creating your own customisable screensaver.

First of all, you will need to have a options dialog, in witch you allow your user to change certain settings for the screensaver. In my example, I allow the changeing of the speed that the text rolls, the background color, the foreground color and the text that is displayed. Here it is:

I keep the settings in an xml file, on the hard drive.Also, you can keep yours in the windows registry, but i recommend the file, because messing with the registry can cause problems.

After you’ve created the settings dialog, you sould start working on the main form, the screensaver itself. The main form should have the following:

  • FrameBorderStyle property to None
  • WindowState property to Maximized
  • MaximizeBox to False
  • MinimizeBox to False
  • TopMost to True

Also, you may need a Timer object to do some redrawing at a specified interval, although it is not necessarIly requIred. Another thing you may need to do is use the SetStyle property to make the form react better when you do extensive drawing on it (like I did in the example code). One other thing that is of major importance. You must give the user a way to close the screensaver, otherwise it will get stuck looking at it and not be able to do anything. So you must close the program whenever the user presses a key on the keyboard, or clicks/moves the mouse.

Let’s take a quick look at my Main method, so you can understand better how to put it all together.

if (args.Length>0)
{
	//display the options...
	if (args[0].ToLower().Trim().Substring(0, 2) == "/c")
	{
		frmOptions op = new frmOptions();
		op.ShowDialog();
		Application.Exit();
	}

	if (args[0].ToLower().Equals("/s"))
	{
		Application.Run( new Form1());
	}
}
else
	Application.Run( new Form1());

If we don’t get a parameter when starting the application, we simply run it. But if we’ll get one, we check to see what they are. If we have something that starts with ‘/c’, then we will need to display the options. If we get the ‘/s’ parameter, we will need to start the screensaver.

To install the screensaver, you will need do go to the bin/Debug folder of your applications and replace the exe extension with scr. Now copy the application to your windows/system32 folder. You will see that you can select it from the display properties and set it as a screensaver; also you can view the settings and change them.

Until the next time, good luck and happy programming!

Raul POPESCU

Attachments:

Project Files: screensaver.zip