Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Oct 1, 1996

Screen Saver Development

What all do I need to know to create a screen saver in Visual Basic?

Windows screensavers are easy and fun to write in Visual Basic, but there are some very important things to know. Experts can go straight to the Code Section of this document.

A Windows screensaver is nothing more that a regular Windows executable file that has been renamed with the .scr extension. In Visual Basic, when you are making the executable file, you will need to set the Application Title in the Make EXE dialog box. This Application Title MUST be set to "SCRNSAVE title", where title is the text you want displayed in the Control Panel screensaver dropdown box.

Command Line Arguments

When Windows starts up a screensaver it calls it with the "/s" argument, and when it wants to Setup the screensaver it uses the "/c" argument. So, we use a code module called SCRNSAV.BAS. In SCRNSAV.BAS you will see that a Select Case statement is used to capture this argument. You will need to change the Startup Form in the options|Project Dialog Box to Sub Main.

Telling Windows that the Saver is Running

How long Windows waits before loading the screensaver is specified in the Control Panel. But if your screensaver doesn't tell Windows that it is running, Windows will reload the screensaver after that time passes again, even though the screensaver is already running. At first I thought I could remedy this situation by using VB 3.0's App object. The App.PrevInstance property will tell you whether or not there is a previous instance loaded.

This should've worked, and I got many comments saying that I must have messed something up, but it didn't. For some reason, with the screensaver this kills both instances, not just the second. But there is a way out. To fix this I found a Windows API call which tells Windows that the screensaver is inactive, so don't load one. To use this API you need to use the API call SystemParametersInfo. This function is used to change system wide parameters, such as whether or not the screensaver is active. Be careful when using this call, since changes are permanent. You will need to make sure that your screensaver turns the screensaver back off when it has ended. See Sub Main, and Sub ExitNice in the Code Section.

Hiding The Cursor

When you write a screensaver, you'll want it to hide the mouse cursor as well as whatever else your saver does. To do this you need to use the API call - ShowCursor. When ShowCursor( False ) is called, the cursor is hidden; when ShowCursor( True ) is called, the cursor is re-displayed. The Windows cursor is a shared object, so if your process hides it your process needs to redisplay it as well. See Code section.

Knowing when to end

When your screensaver ends is up to you, but generally you'll want it to end if any of the following occur: mouse moves, button pressed, key pressed. To do this you will need to call a routine to exit properly from each of these events in your screensaver form. See SaverForm. You need to call this routine because this is where the other half of the SystemParametersInfo call is made. If this is left out, the screensaver won't run again after it wakes up. Another problem is that the MouseMove message is sent if the cursor is over the form, REGARDLESS if it is moved or not. So, you need to check to see if it has moved somehow. See the Code Section for my solution. (Not necessarily the prettiest.



    DefInt A-Z
    Const SWP_NOSIZE = 1
    Const SWP_NOMOVE = 2
    Declare Function ShowCursor Lib "User" (ByVal bShow As Integer) As Integer
    Declare Sub SetWindowPos Lib "User" (ByVal hWnd, ByVal After, ByVal X, ByVal Y, ByVal cx, ByVal cy, ByVal Flags)
    Declare Function SystemParametersInfo Lib "User" (ByVal uAction As Integer, ByVal uparam As Integer, lpvParam As Any, ByVal fuWinIni
      As Integer) As Integer

  Sub Main
    Select Case Command$
      Case "/s", "/S"
        Res = SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 0, ByVal 0&, 0)
        Load SaverForm
        SetWindowPos SaverForm.hWnd, -1, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
        ok = DoEvents()
      Case "/c", "/C"
    End Select
  End Sub
You may also need to add some initialization code for whatever your screensaver does.
Sub ExitNice
    Res = ShowCursor(True)  'Turn the cursor back on
    'reset screensaver
    Res = SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 1, ByVal 0&, 0) 
  End Sub
    WindowState = 2		'Maximize the screensaver
    Me.Show		'Show the form
    This = ShowCursor(False)	'Hide the cursor

Form_MouseMove If (OldX = 0) And (OldY = 0) Then OldX = X OldY = Y Exit Sub End If If (OldX <> X) Or (OldY <> Y) Then ExitNice Else OldX = 0 OldX = 0 End If Form_Click ExitNice Form_MouseDown ExitNice Form_KeyDown ExitNice Form_KeyPress ExitNice


Windows will pass the "/c" argument to Sub Main if the "Setup" option is chosen from control panel. Here you can setup specific options for your screensaver. You might want to save these options in a .ini file (win.ini or your own). Its up to you! If your Config.Frm has a "Test" feature which starts the screensaver from the Config form, then you will need to be careful about remembering to turn on the cursor after the screensaver starts, and then turn it off before it ends.


Conger, James L.., The Wait Group's Windows API Bible: The Definitive Programmer's Reference. The Wait Group: 1993. ISBN 1-878739-15-8 VBZ: The Electronic Journal on Visual Basic. Copyright 1993 User Friendly, Inc. Issue 01: January/February 1993


This information is provided free of charge, and may be freely distributed. If you use portions of this document elsewhere, please indicate where you got it. All of the information here has been used and tested by me in Visual Basic 3.0 Professional. Use at your own risk. Visual Basic and Microsoft Windows are registered trademarks of Microsoft Corp. Some small further info can be found in the Microsoft Knowledge Base (at the Microsoft Web Site). The article number is Q146907. It seems (according to this article) that in VB4.0 you can only create screen savers with the 16-bit version?! Seems a trifle silly to me. If anyone has the method to do this in 32-bit mode I'm sure the rest of the readers would be interested.

DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date