Screen Saver Template

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

The purpose of this article and code is to provide you with a quick and easy way to get started writing your own screen saver in VB. I am currently using VB5. The code to be downloaded with this article includes a file named “ScreenSaver.frm”. This is your screen saver template. I have also included a sample screen saver named “Sample.scr” for you to try, and the project files used to create it.

To start using your new screen saver template, your first step should be to copy the file “ScreenSaver.frm” to the “TemplateForms” directory which should be located inside the directory where VB resides on your computer. If this directory does not exist, create it exactly like the name above shows and copy the file into the directory you just created. Now when you start a new VB project and add a new form, you should see a template labeled “ScreenSaver” in the “Add Form” dialog box. If this doesn’t work for you, see the troubleshooting section located at the bottom of this document before you proceed.

Now that you have put your new template into VB, let me tell you how to use it. First start a new VB project and add a new “Screen Saver” form as described in the end of the previous paragraph. Now you will need to give your project a name. Do this by highlighting your project in the “Project Explorer window, and typing a name in the “Name” section of the “Properties” window. Next you need to give your application a title and description. To do this, click on “Project” then “(Your project’s Name) Properties” and go to the “General” tab. In the “Project Description” textbox, type “SCRNSAVE:” followed by the description of your screen saver. I used “SCRNSAVE: Sample Screen Saver” for my description. You always need to preceed your project description with “SCRNSAVE:” when making a screen saver so that Windows will know that this application is truly a screen saver. Now, to give your application a title, on the same dialog box, go to the “Make” tab. In the “Title” textbox type the title of your application, preceeded by “SCRNSAVE:” just as we did above with the project description. I used the title “SCRNSAVE: Sample Screen Saver”. Now save your project and we can move on to the easy part.

You basically already have the code for a functioning screen saver. At this point, it would only show a blank screen, but with a little creativity you can fix that. Windows calls your screen saver with command line options that tell your screen saver the mode it should display itself in. There are three modes that your screen saver should handle, I’ll call them “Regular”, “Preview” and “Settings”. Regular mode is when your screen saver is called to save the screen. Preview mode is when your screen saver is called to show a preview of itself on that little monitor in the “Display Settings” dialog box. Lastly, Settings mode is when your screen saver should allow the user to change settings that are specific to your screen saver, like how many stars to be shown at a time or whether to use sound or not. Most of this is handled for you in the “Form_Load” subroutine. It handles the command line arguments that windows passes to your screen saver and calls the appropriate function to setup the operation mode of your screen saver. There are places provided for you to place your initialization code for each mode located in the “Form_Load” subroutine. These places are marked with comments that explain exactly what to do. One more thing you need to know is when you compile your screen saver, instead of giving your executible file a “.exe” extension, give it a “.scr” extension, for example “MyScreenSaver.scr”. This is the file extension that Windows recognizes as being a screen saver. Just copy your “.scr” file to the Windows directory, select it in the “Display Settings” dialog box and you should have your very own screen saver.

This gets you started on your way to making your own screen saver, but it leaves the creativity up to you. My sample screen saver is very simple, but hopefully you can use it as a base to create some very cool screen savers. Don’t forget, the purpose of a screen saver is to prevent screen burn-in, so make sure that your screen saver doesn’t display any static images, or otherwise, you will defeat the whole purpose of your screen saver.

Troubleshooting

I am having problems getting my template form to appear in the “Add Form” dialog box.

Open VB and click on “Tools” then “Options” and go to the “Environment” tab. Make sure that the “Forms” checkbox is checked in the “Show Templates For:” frame. Also make sure that the “Templates Directory” textbox contains the path to VB’s home directory with “Template” attached to the end of it. For example, I am using VB5, and mine looks like this “C:PROGRAM FILESDEVSTUDIOVBTemplate”. If this is not so, then you should copy the file “ScreenSaver.frm” to the “Forms” directory that is located within the directory shown in the “Templates Directory” textbox. If the “Forms” directory doesn’t exist within the directory just mentioned, create it, then copy the file there.

Download demo project – 8 KB

Download source – 7 KB

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read