Creating a Screen Saver
By: Lorenzo Dalla Vecchia Date: 03/09/1998

The structure of a Screen Saver is very simple. It consist of only one form with the following properties:

Property Setting
Caption ""
ControlBox False
MinButton False
MaxButton False
BorderStyle 0 - None
WindowState 2 - Maximized
BackColor &H0

With these properties, the form will be black, will have no border, buttons or title bar and will cover all the screen.
So, create a form and give it the above properties.

Now we must do two other things. First, each screen saver is a form that stays on the top of all others. Second, we notice that, during the display of a Screen Saver, the mouse cursor desappear.
To make our program do these two things, we first put these two lines in the declarations section of the form:

Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

Private Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long

We need a Sub that works with the first API and makes the form topmost.

Sub AlwaysOnTop (FrmID As Form, OnTop As Boolean)
   Const SWP_NOMOVE = 2
   Const SWP_NOSIZE = 1
   Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
   Const HWND_TOPMOST = -1
   Const HWND_NOTOPMOST = -2

   If OnTop Then
      OnTop = SetWindowPos(FrmID.hWnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)
   Else
      OnTop = SetWindowPos(FrmID.hWnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)
   End If
End Sub

The next part is the load-up of our Screen Saver: put these lines in the Load event of the form.

Select Case LCase(Left(Command, 2))
Case "/p": End
Case "/s"
Case Else: Me.Hide: frmSettings.Show: Exit Sub
End Select

Dim x As Integer
Call AlwaysOnTop(Me, True)
x = ShowCursor(False)

The form will always stay on top and the cursor will desappear.
When the Screen Saver unloads, we must do the inverse. Put these lines in the
Unload event of the form.

Dim x As Integer
x = ShowCursor(True)

The first 5 lines of the Form_Load event are needed to make the Screen Saver work, work as an example or show its settings. Windows tells the Screen Saver what to do through command line parameters: /S tells the Screen Saver to start working, /P tells the Screen Saver to start working in a preview area, and no parameters makes the Screen Saver show its settings dialog. So, you may create a form called frmSettings that gives the opportunity to set Screen Saver parameters and to save them in a INI file or in the Registry; that dialog must unload the program when it is finished.

Another important thing to do, is to handle the events that unload the Screen Saver. A Screen Saver is usually unloaded when we move the mouse or hit a key, so we'll put the instruction Unload Me in the KeyPress event of the form. For the MouseMove or MouseClick events, use these lines:

Static Count As Integer
Count = Count + 1
If Count > 5 Then
   Unload Me
End If

Well, it is your turn now! Can you creat an original and good Screen Saver. You can use controls (even OCXs) to create the graphic, and add code anywhere you want. If you want, you can download this example, located in the samples section of the site, demostrating a full working Screen Saver.

When you have finished creating the look of the Screen Saver, it's the moment to compile it and bring it to life. Once you have compiled your Screen Saver, change its EXE extension to SCR. Then, copy it to the Windows\System folder.

To use the Screen Saver, go in the Control Panel, double-click the icon Screen, select the second tab (Screen Saver) and select your program from the Screen Saver dropdown list. Then, click OK.

Click Here!


Visual Basic Programming Zone is a website by Lorenzo Dalla Vecchia.
Webmaster's e-mail: webmaster@vbprogzone.cjb.net
Hosted by InWind: www.inwind.it