
How-To... - Creating A Screen Saver - Part 1
'How do I create a screen saver through Visual Basic?' is a question asked by a lot of VB users. And the answer is -- well, read on. Getting a form to act like a screensaver is easy, just a few properties changed here and there and some lines of code. The hard part is getting an actual screen saver design - like the Flying Windows saver - I for one do not know, at the moment, how to get an effect like that.
Part 1 of this How-To will show you how you can get your form to behave like a saver, and how to compile your file into a .scr file. Lets begin...
Step 1
Insert a standard module and go to the General
Declerations section and type sub main
and press ENTER. In this new procedure,
insert the following code :-
If App.PrevInstance Then End
If InStr(Command, "/s") > 0 Then
frmSaver.Show
End If
What it does : The first line makes sure it only one instance of your saver runs. This is required because Windows will try to run your saver again(and any saver for that matter) after the set interval even if it is already running. The next IF statement is when Windows passes the command '/s' to your save, the form will be displayed. All the '/s' means is display the saver. Ok, thats all the module code finished for the moment.
Step 2
On a form - give it a name of frmSaver (only for this example). Set the following properties in the Properties box
BACKCOLOR=&H00000000& {Black}
BORDERSTYLE=0 - None {You don't want a title bar displayed in your saver}
WINDOWSTATE=2 - Maximized {Make your saver take the whole screen}
Now, double-click on the Form and go to the Click procedure of the Form. In this, insert the following code :
End
All this does is end the saver when you click the mouse. In the MouseMove procedure, insert the following code :
Static OldX As Integer
Static OldY As Integer
If (OldX > 0 And OldY > 0) And (Abs(X - OldX) > 3 Or Abs(Y - OldY) > 3) Then
End
End If
OldX = X
OldY = Y
This tracks the positon of the mouse(X and Y) and if it moves, then end the saver. You may also wish to put End in the MouseDown, MouseUp, KeyPress and Keydown procedures. Thats all the code(for the moment). All that is left to do is compile it into an .scr file. Also, put a design or something on the form - black is a bit boring!.
Step 3
Before compiling, go into the TOOLS menu and click OPTIONS(may differ with versions) and in the Project tab, make sure Sub Main is selected in the Startup Form box and click OK to close the box. Next, click on FILE, then MAKE EXE FILE(may differ with versions) and in the filename box, type in a name for your saver followed by .scr .For example, vbhowto.scr and click Save to save it. Its best to save it in the Windows/System folder which means you can view it through the Screensaver tab in the Display Properties.
And there you have it, you now have a working screensaver. Part 2 will have more features, including a password and config box.
You can download this example by clicking here
-----------
Tip supplied by David Cowan(dodgesoft@hotmail.com)