Page 2 of 4
Now we are already to try the screensaver but there is one small complication.
At the moment the project is designated as a console project and as such it displays a command window and refuses to display Windows forms.
In fact we really want a Windows project and have created a console project just to get the system to generate the main function and other related code. To make the project work all that is necessary is to use the Project,Properties menu item and in the dialog box that opens change the Output type from Console Application to Windows Application and set the Startup object to be the ScreenSaver1.Program to ensure that it is the main function that starts running first.
Now we can run the program to check that the screen form does appear. Next we need to install the screensaver. To do this you have to navigate to the project’s debug directory, find ScreenSaver1.exe and rename it to ScreenSaver1.scr.
Next you can either copy it to the Windows/System32 directory, right click on the desktop, select the Screen Saver tab and finally select ScreenSaver1 in the drop down list. Copying the file to the System32 directory makes the installation permanent in the sense that it is available in the Screen Saver tab dropdown list.
While you are testing the screensaver a quicker way is to right click on the ScreenSaver1.scr file and select Install from the drop-down menu. This starts the Windows Screen Saver Dialog Box which you can use to select your screensaver. This isn’t a permanent installation in the sense that if you select another screensaver at a later time your screensaver isn’t re-presented in the list of possible choices.
You can click the preview button to see what the screensaver actually does. If you want to check that it really does start after a period of inactivity change the “Wait” time to 1 minute. It is worth noticing that all that happens when your “screensaver” starts is that the screen form appears. Nothing else happens to the form unless you close it, using the close box.
You can see that the screensaver mechanism is a good way of getting almost any program started during a period of inactivity and it doesn’t have to stop running the instant the user returns.
A form based screensaver
The method described so far is the traditional approach that you will find in the documentation and examples. Sometimes it’s a good way of implementing a screensaver but if you are planning to use a form to create the display then there is a better way to build an application.
Start a new Windows form project called ScreenSaver2. The easiest way to get at the command line arguments/parameters is to use
We need to modify the form's contructor:
m_args = Environment.
this.TopLevel = false;
and add the global variable:
private string m_args;
Now any method in Form1 can access m_args to discover what the command line parameters are. Notice that this method of retrieving the command line parameters always returns the execution path as the first parameter i.e. the first real parameter is in m_args.
The TopLevel property is set to false to make the form invisible. Notice you can’t make the form loaded on startup invisible by using Hide or setting Visible=false because the Run method makes the form visible after the Load event handler is finished. With these changes all of the command line argument processing can be performed in the Form Load event handler:
private void Form1_Load(
object sender, EventArgs e)
if (m_args.Length > 1)
string arg = m_args.ToLower().
And the ShowScreenSaver function is just:
this.TopLevel = true;