How to write a Screen Saver

The basic requirements for creating a screen saver are:

  1. In the properties of your application set the title to:
      SCRNSAVE: name

    The 'name' part will appear on the list of available screen savers in the control panel. Note that this is not the title of the form, it is the application title and must be set in the project properties.

  2. When you compile the program use .SCR for the extension instead of .EXE as normal. Place the compiled code in the Windows\System directory (it can also go in Windows, but System is preferable).

  3. When your application starts it should check for a previous instance and stop if one is found:
        Sub Form_Load()
           If App.PrevInstance Then
             Unload Me ' NEVER use END!
             Exit Sub
           End If
        End Sub
  4. Check the command line for switches:

    /c configure mode - this will happen when a user clicks the Configure button in the screen saver dialog (Note: when a user right-clicks the .SCR file and chooses "configure" the command line will be blank)
    /s setup mode - display/update configuration options only
    /p nnn preview mode - "nnn" is a handle to the preview window
    /a nnn password support check - "nnn" is a handle to the Display Properties main window

 

When you show your Screen Saver (switch /s)

  1. You don't need the form caption, so set BorderStyle to 0 - None.
  2. You have to maximize your form. You can do this in two ways:
    • Set WindowState property to 2 - Maximized
    • In the Form_Load sub write the following code:

      Move 0, 0, Screen.Width, Screen.Height
  3. You need to hide cursor, like this:

    Insert this declaration string to a module:
       Public Declare Function  ShowCursor& Lib "user32" (ByVal bShow&)

    And insert the following code into the Form_Load sub:

       Do
       Loop Until ShowCursor(False) < -5

    Comments: Windows maintains an internal display count that increments for each call of this function if bShow is TRUE and decrements if bShow is FALSE. The cursor will be displayed when this count is greater or equal to zero.

    But if you are in DOS-mode when your Screen Saver is to run, you need to repeat this hiding operation at least 5 times to be sure the cursor will be disappear.

  4. Don't forget to display the cursor again at the end of your program:

    In the Form_Unload procedure write the following code:

       Do
       Loop Until ShowCursor(True) > 5

    Comments: I have checked and found that I only need to repeat setting ShowCursor(True) until it returns 0, but to be 100% sure repeat it 5 more times.

  5. Unload your Screen Saver when a user presses any key or clicks or moves the mouse. To do this, insert the following code string into the Form_KeyDown and the Form_MouseDown procedures:

       Unload Me

    If you insert this code string in the Form_MouseMove procedure your Screen Saver would never run. There will probably be a MouseMove event or two at startup that will need to be discarded. So for example insert the following code in your form:

       Dim nMouseMove As Long
    
       Private Sub Form_MouseMove(Button As Integer, Shift As Integer, _
                                  X As Single, Y As Single)
          If nMouseMove > 2 Then Unload Me
          nMouseMove = nMouseMove + 1
       End Sub
Articles