Writing Windows Screensavers in VB

This section is quite brief as VB is not my primary language for writing screensavers.
Summary
Three things need to be done.
  1. Name the screensaver by setting the application title
  2. Deal with the command line parameters and stop multiple instances of your application being launched.
  3. Have a timer to control the operation of your animation

Visual Basic 3

The secret to getting windows to recognise the VB programme as a screen saver is to give the application title of the form:

SCRNSAVE: Interesting Title

Remember that only Win 3.1 and WinNT will use this name. Win95 uses the name from the executable file. Remeber to name the file with a .SCR suffix.

Then use a bit of code like the following as your sub main:

Sub Main()
'Stop a second copy of this program from being run concurrently...
'Don't try to use an If/End if for this, your saver will alternate on and off.
    If app.PrevInstance Then End
    
'Check if we are doing a setup by looking at the command line.
    If LCase$(Command$) = "/c" Then
        FrmSetup.Show 'Setup form
    Else
	FrmMain.Show ' The main screensaver
    End If
End Sub
The FrmSetup form will either display a Help about or allow the user to configure you screensaver.

The FrmMain will contain a timer control to control the animation. You can either make this form hidden and draw direct to the screen using API calls or you can maximise this form to hide the rest of the applications. Make sure your Form's Maximized property is set, and that it's Border Style is set to None. Add code the the move mouse, mouse down events, to unload the screensaver.

I like to use the SETCURSOR API call to hide the cursor for this form.


Visual Basic 5 and up

The example provided here does not take into account the more sophisticated things you can do with Win95 but allows you to have a simple working screensaver. I have not used these techniques with VB4 or VB6 but I would expect them to work.

When debugging the screensaver, use the ability to set the command line options to control which part gets activated.

The 32 bit systems are less fussy in identifying screensavers but you can still setup the application title in the form:

SCRNSAVE: Interesting Title

Remember that only WinNT will use this name. Win95 uses the name from the executable file. Remember to compile the file with a .SCR suffix.

Then use a bit of code like the following as your sub main:

Public Sub Main()
    'Stop a second copy of this program from being run concurrently...
    If App.PrevInstance Then End
    'Check if we are doing a setup..
    If Left$(LCase$(Command()), 2) = "/c" Or Trim$(Command()) = "" Then
        FrmSetup.Show
        Exit Sub
    End If
    If Left(LCase$(Command()), 2) = "/p" Then
    ' If clever then do preview in the dialog as defined by handle
    ' Not implemented in this example
        Exit Sub ' Do not activate by the attempt to preview
    End If
    ' /A to support changeing of passwords	
    ' Not implemented in this example
    FrmMain.Show
End Sub
The FrmSetup form will either display a Help about or allow the user to configure you screensaver.

The FrmMain will contain a timer control to control the animation. You can either make this form hidden and draw direct to the screen using API calls or you can maximise this form to hide the rest of the applications. Make sure your Form's Maximized property is set, and that it's Border Style is set to None. Add code for the move mouse, mouse down events, to unload the screensaver.

I like to use the SETCURSOR API call to hide the cursor for this form.

Experts
If you wish to expand the parsing of the command lines then you can provide a long variable and convert the command line to a window handle. You can then use Windows API calls to do the drawing instead of native VB. Don't forget to allow for the instance where the command line is empty.
Dim HWndParent as long
HWndParent = Val(Right$(Command$, Len(Command$) - 2))

VB .NET

I have not yet had chance to research how the .NET framework interacts with screensavers but as far as I am aware, all the above principles still stand.


Reference

Search for books on Amazon.Amazon.co.uk Logo


Home Education Employment Hobbies Screen Savers
Email : Andy.Clark@Dial.Pipex.Com - Last Updated : 12th Dec 1998