Building a Screen Saver in Visual Basic
Windows comes with programs known as screen savers, which are executed automatically after a period of inactivity. The theory behind a screen saver is that it will "save" your monitor from having information permanently burned on the picture tube. This burning was particularly a problem with older text-only terminals, which could have the same menu sitting on them for hours at a time. (It is also the reason that owners' manuals for televisions warn against using video games.) You can set the screen saver in the Screen Saver tab of the Display Properties dialog box, as shown in Figure 23.2.
You can write a VB program to act as a screen saver.
In recent years, the screen saver has almost become an art form, with everything
from 3D pipes to flying toasters. However, if you want to be really creative,
you can create your own screen saver. In Visual Basic, this task is actually
very easy. You write the screen saver just like a normal program, but you program
the events differently. For example, mouse movement stops the program. In the
next section, you create a simple screen saver.Setting Up the Main Form
Your screen saver will actually be a Standard EXE project. To begin, start a new Standard EXE project, and modify the form's design time properties as follows:-
Set the WindowState property to Maximized.
-
Set the BorderStyle property to None.
-
Set the BackColor property to the color black.
Dim nMouseCount As Integer
Private Sub Form_KeyPress(KeyAscii As Integer)
End
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, _
X As Single, Y As Single)
nMouseCount = nMouseCount + 1
If nMouseCount > 5 Then End
End Sub
Note that you use a counter so that the program does not end on the first MouseMove
event. You need this counter because Windows sends a couple of MouseMove
events to the program when it starts.Adding Animation
Run the screen saver program in the Visual Basic IDE, and you see a blank screen until you press a key or move the mouse. This screen saver is not very exciting; it needs some animation, which can be added with the Timer control and some Image controls. The Timer control is covered in Chapter 4, "Using Visual Basic's Default Controls." Go ahead and add a simple animation now. For my screen saver, I created a car chase, as you can see in Figure 23.3.
This screen saver form is shown in design mode, with all the animation elements.
- See "Creating a Simple Animation," p. 83.
Dim CarX As Integer, CarY As Integer
Private Sub Form_Load()
If App.PrevInstance Then End
CheckCommandLine
CarX = Me.Width + 1000
CarY = 0
End Sub
Private Sub tmrMain_Timer()
CarX = CarX - 35
CarY = CarY + 13
If CarX < LeftBorder - 1000 Then Form_Load
imgCar.Top = CarY
imgCar.Left = CarX
imgBronco.Top = CarY + 230
imgBronco.Left = CarX - 7000
DoEvents
End Sub
Also, note that the program ends in the Load event if a previous instance
occurs. This code is very important because Windows sometimes launches multiple
instances of a running screen saver.Interacting with Windows
Now that you have the screen saver animation completed, you just need to add a few lines of code so that your program works properly as a screen saver. As you may know, the Screen Saver tab of the Display Properties dialog box allows you to configure and preview a screen saver. In each case, Windows sends a different command line to your program. Add the CheckCommandLine function to your form so that it acts appropriately:Sub CheckCommandLine()
Dim sCmdLine As String
sCmdLine = Trim$(LCase$(Command$))
'RUNNING AS A SCREEN SAVER
If sCmdLine = "/s" Or sCmdLine = "" Then
Exit Sub
End If
'RUNNING IN SETUP (CONFIG) MODE
If sCmdLine = "/c" Then
MsgBox "Config Screen Not Available"
End
End If
'RUNNING IN PREVIEW MODE
If Left$(sCmdLine, 2) = "/p" Then
'parameter can be retrieved with Val(Mid$(sCmdLine,3))
End
End If
End Sub
Finally, all you need to do is compile your project, change the filename extension
from .EXE to .SCR, and copy the program to the Windows directory. It should
then automatically show up in the list of available screen savers in the Screen
Saver tab of the Display Properties dialog box.