Creating screen savers in Visual Basic

Don't let those C++ programmers tell you that you have to create Windows 95 Screen Savers with their language. You can create a Windows 95 Screen Saver by taking the following steps. What the screen saver does is up to you. The sample project which follows this article creates a simple Scrolling Marquee.

Download the SAVER.VBP project (17kb)

  1. Create a new VB Project
  2. Add a Module and two forms to the project.
  3. Name one form frmSaver and one form frmConfig
  4. In the frmSaver, set the following properties: WindowState to 2 - Maximized, Caption = ""(no caption), ControlBox = False, MaxButton = False, MinButton = False
  5. Set the BackColor of frmSaver to black
  6. Place a label control on frmSaver, name it lblSaver
  7. Set the Caption property of lblSaver to "VB Screen Saver"
  8. Set the ForeColor of lblSaver to red and set it's BackStyle = 0 - Transparent
  9. The frmConfig form will be used to set configuration options, this form will be displayed when you click the "Settings" button from Windows 95. This is optional, for now we'll just fake it out. Add a single button to this form and add the code "End" behind this button.
  10. Create a Main subroutine and add the following code:
 
Sub Main()
 
   'Use the following code to display a configuration form (optional)
   If Command$ = "/c" Then
      frmConfig.Show   ' display configuration form
      Exit Sub
   End If
 
   'With a screen saver you may only run one instance at a time
   If App.PrevInstance Then   ' If already running, end the application.
      End
   Else
      frmSaver.Show 1             ' Show the screen saver form (displays modal).
   End If
      
End Sub
  1. In the GotFocus event of frmSaver add the following code, this does the simple animation:
 
Private Sub Form_GotFocus()
Dim Temp As Double
   Do 'Outermost loop is infinite, it will stop when the user moves their cursor
      lblSaver.Top = Me.Top
      Do
         lblSaver.Left = Me.Width
         Do
            'Decrement the horizontal position
            lblSaver.Left = lblSaver.Left - 60
               'Initialize the temp val (timer is seconds since midnight)
               'The following slightly increases the redraw time to
               'smooth the animation
               Temp = Timer
               Do
                  DoEvents
               Loop Until Timer - Temp  0.01
         Loop Until lblSaver.Left + lblSaver.Width <0
         'Increment the vertical position
         lblSaver.Top = lblSaver.Top + lblSaver.Height
      Loop Until lblSaver.Top + lblSaver.Height  Me.Height
   Loop
End Sub
  1. In the KeyDown, MouseMove and MouseDown events of frmSaver add the following code, causes the program to end when the user moves the mouse or presses a key:
 
      Static count As Integer
      If count  2 Then
         End
      Else
         count = count + 1
      End If
  1. In the properties of frmSaver, set the WindowState to 2 - Maximized
  2. Set the startup form for the project to "Sub Main"
  3. Test the program, it should look like a screen saver that halts when you move your mouse or press a key.
  4. When you compile your program, give it a .SCR extension instead of the default .EXE, In the File Make EXE File dialog, insert the string SCRNSAVE: (in upper case) at the beginning of the Application Title. For example: SCRNSAVE:VB Marquis.
  5. Move the new .SCR file to your \Windows\System directory and you will be able to select it from the standard Windows 95 Screen Saver Dialog.

Final Tips

Here are some instructions for creating a basic screen saver in Visual Basic. Please E-mail any comments to me.

1. Screen Saver Name
In Visual Basic 3.0's "Make EXE File" dialog, the text in the "Application Title" field will be used for the screen saver name. The screen saver's icon will be the icon from the specified form.

2. General Code
I suggest starting a screen saver as follows. The following code is displayed as part of the main form's Form_Load subroutine. If your application starts in a Main() subroutine in a module (*.BAS file), the code should still work.

 
 
 
 
Sub Form_Load ()
 
If App.PrevInstance Then End 
 
'* Do not execute a 2nd instance
 
 
 
Let C$ = UCase$(Trim$(Command$))
 
'* Trim command line parameters
 
'* and convert to upper case
 
 
 
If C$ = "/C" Then
 
'* If the screen saver is started with
 
'* a /C or /c switch, display the config-
 
'*  uration dialog.
 
'* Display Configuration Dialog
 
'* E.g., Config.Show 1
 
 
 
Else
 
'* Otherwise, go ahead and execute screen saver
 
'* E.g., Form1.Show
 
 
 
End If
 
End Sub
 

Also, if a previous instance is detected, the second instance will quit without warning. While I don't think this should be a problem, the second instance could flag the other copy to activate by setting an environment variable or something.

 

Hosted by www.Geocities.ws

1