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)
Sub Main()
'Use the following code to display a configuration form (optional) If Command$ = "/c" ThenfrmConfig.Show ' display configuration form
Exit Sub End If 'With a screen saver you may only run one instance at a timeIf App.PrevInstance Then ' If already running, end the application.
End ElsefrmSaver.Show 1 ' Show the screen saver form (displays modal).
End If End Sub
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 DoEventsLoop Until Timer - Temp 0.01
Loop Until lblSaver.Left + lblSaver.Width <0 'Increment the vertical position lblSaver.Top = lblSaver.Top + lblSaver.HeightLoop Until lblSaver.Top + lblSaver.Height Me.Height
LoopEnd Sub
Static count As IntegerIf count 2 Then
End Else count = count + 1 End If
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.