Important alert: (current site time 7/16/2013 4:18:09 AM EDT)
 

VB icon

Creating a Screen Saver

Email
Submitted on: 6/9/1997
By: VB Tips and Source Code 
Level: Not Given
User Rating: By 3 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 74084
 
     Create a screen saver in VB!
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Creating a Screen Saver
' Description:Create a screen saver in VB!
' By: VB Tips and Source Code
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=162&lngWId=1'for details.'**************************************

In order to accomplish this task, start a new Visual Basic project. This example only requires a form - no VBXs or additional modules necessary. On the form, set the following properties:
•Caption = "" •ControlBox = False •MinButton = False •MaxButton = False •BorderStyle = 0 ' None •WindowState = 2 ' Maximized •BackColor = Black 
The next order of business is to place a line (shape control) on the form. Draw it to any orientation and color you wish. Set the color by using the BorderColor property. 
The last control that you will need to place on the form is a timer control. Set the timer's interval property anywhere from 100 to 500 (1/10 to 1/2 of a second). 
In the general declarations section of the form you will need to declare two API functions. The first of these (SetWindowPos) is used to enable the form to stay on top of all other windows. The second (ShowCursor) is used to hide the mouse pointer while the screen saver runs and to restore it when the screen saver ends. The declares look like the following: 
For VB3:
 Declare Function SetWindowPos Lib "user" (ByVal h%, ByVal hb%, ByVal x%, ByVal Y%, ByVal cx%, ByVal cy%, ByVal f%) As Integer
 Declare Function ShowCursor Lib "User" (ByVal bShow As Integer) As Integer
For VB4:
 Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
 Private Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long
The first SUB we will write will be the routine that we will call to keep the form always on top. Place this SUB into the general declarations section of the form. 
Sub AlwaysOnTop (FrmID As Form, OnTop As Integer)
' This function uses an argument to determine whether
' to make the specified form always on top or not
Const SWP_NOMOVE = 2
Const SWP_NOSIZE = 1
Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
If OnTop Then
OnTop = SetWindowPos(FrmID.hWnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)
Else
OnTop = SetWindowPos(FrmID.hWnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)
End If
End Sub
The next issue we will take up will be the issue of getting the program started. This is of course the Form_Load event procedure. The actions we will take in this procedure is to randomize the number generator (so that the line moves around differently each time the screen saver is activated). We will also call the AlwaysOnTop SUB so that it will appear over everything else on the screen. 
Sub Form_Load ()
Dim x As Integer ' Declare variable
Randomize Timer' Variety is the spice of life
AlwaysOnTop Me, True ' Cover everything else on screen
x = ShowCursor(False) ' Hide MousePointer while running
End Sub
Now, before we handle the logic of making the line bounce around the screen, let's go ahead and handle shutting the program down. Most screen savers terminate when one of two things happen. Our's will end when the mouse is moved or when a key is pressed on the keyboard. Therefore we will need to trap two event procedures. Since there are no controls on the screen that can generate event procedures, we need to trap them at the form level. We will use the Form_KeyPress and Form_MouseMove event procedures to handle this. They appear as follows: 
Sub Form_KeyPress (KeyAscii As Integer)
Dim x As Integer
x = ShowCursor(True) ' Restore Mousepointer
Unload Me
End
End Sub
Sub Form_MouseMove (Button As Integer, Shift As Integer, x As Single, Y As Single)
Static Count As Integer
Count = Count + 1 ' Give enough time for program to run
If Count > 5 Then
x = ShowCursor(True) ' Restore Mousepointer
Unload Me
End
End If
End Sub
Finally, we need to handle the logic necessary to cause motion on the screen. I have created two sets of variables. One set DirXX handles the direction (1=Right or Down and 2=Left or Up) of the motion for each of the line control's four coordinates. The other set SpeedXX handles the speed factor for each of the line's four coordinates. These will be generated randomly (hence the Randomize Timer statement in Form_Load). These variables are Static, which of course means that each time the event procedure is called, they will retain their values from the preceeding time. The first time through the procedure they will also be set to zero. Therefore the program will assign these random values the first time through. From that point on, the program checks the direction of movement of each of the four coordinates and relocates them to a new position (the distance governed by the SpeedXX variable). The last section of code simply checks these coordinates to see if they left the visible area of the form and if they did their direction is reversed. This of course goes in the Timer's event procedure. 
Sub Timer1_Timer ()
Static DirX1 As Integer, Speedx1 As Integer
Static DirX2 As Integer, Speedx2 As Integer
Static DirY1 As Integer, Speedy1 As Integer
Static DirY2 As Integer, Speedy2 As Integer
' Set initial Direction
If DirX1 = 0 Then DirX1 = Rnd * 3
If DirX2 = 0 Then DirX2 = Rnd * 3
If DirY1 = 0 Then DirY1 = Rnd * 3
If DirY2 = 0 Then DirY2 = Rnd * 3
' Set Speed
If Speedx1 = 0 Then Speedx1 = 60 * Int(Rnd * 5)
If Speedx2 = 0 Then Speedx2 = 60 * Int((Rnd * 5))
If Speedy1 = 0 Then Speedy1 = 60 * Int((Rnd * 5))
If Speedy2 = 0 Then Speedy2 = 60 * Int((Rnd * 5))
' Handle Movement
' If X1=1 then moving right else moving left
' If X2=1 then moving right else moving left
' If Y1=1 then moving down else moving up
' If Y2=1 then moving down else moving up
If DirX1 = 1 Then
Line1.X1 = Line1.X1 + Speedx1
Else
Line1.X1 = Line1.X1 - Speedx1
End If
If DirX2 = 1 Then
Line1.X2 = Line1.X2 + Speedx2
Else
Line1.X2 = Line1.X2 - Speedx1
End If
If DirY1 = 1 Then
Line1.Y1 = Line1.Y1 + Speedy1
Else
Line1.Y1 = Line1.Y1 - Speedy1
End If
If DirY2 = 1 Then
Line1.Y2 = Line1.Y2 + Speedy2
Else
Line1.Y2 = Line1.Y2 - Speedy2
End If
' Handle bouncing (change directions if off screen)
If Line1.X1 < 0 Then DirX1 = 1
If Line1.X1 > Me.ScaleWidth Then DirX1 = 2
If Line1.X2 < 0 Then DirX2 = 1
If Line1.X2 > Me.ScaleWidth Then DirX2 = 2
If Line1.Y1 < 0 Then DirY1 = 1
If Line1.Y1 > Me.ScaleHeight Then DirY1 = 2
If Line1.Y2 < 0 Then DirY2 = 1
If Line1.Y2 > Me.ScaleHeight Then DirY2 = 2
End Sub
Once you have entered all the preceeding code you have a nice little program that looks like a screen saver. You can compile it into an EXE and run it anytime you like. However, to make it into a true Windows screen-saver you need to do the following steps:
1.Choose "Make EXE File" from the File menu. 2.In the "Application Title" text box, type in the following: SCRNSAVE:VB4UandME Example 3.Change the extension in the EXE filename to have an SCR extension instead of an EXE. 4.Change the destination directory to your Windows directory (where all screen savers need to reside) 5.Click OK and let the compilation proceed. 
At this point, you should be able to bring up the Windows Control Panel and select VB4UandME Example as the new screen saver. For Windows 3.1 this is found in the Desktop icon within Control Panel. For Windows 95, it is found in the Display icon in Control Panel (second tab).


Other 13 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
4/14/1999 3:27:00 PMDan

When I run the program All I see is a black screen with some lines. How can i make it animated/ For example, Make The Lines Move Around The Screen???
(If this comment was disrespectful, please report it.)

 
4/15/1999 7:01:00 PMJosh

Whenever I go to Control Panel,then display,and choose this screensaver,it starts up the saver,and i didn't even do anything.Help!
(If this comment was disrespectful, please report it.)

 
4/16/1999 2:25:00 AMArje

Josh,
I've got the same problem. It does seem to make a difference if you place the code that is in the Load section in the Activate section of the form. It than jumps to black and back again to the control form. It usually is stable, but not always!!
Does anybody know any code that can be included to use it in the windows way?
Thanks in advance.
(If this comment was disrespectful, please report it.)

 
4/16/1999 8:46:00 PMMatt

I tried to get the ShowCursor code to work, (declare the API and all) in VB6 and it would only work sometimes. I told it to hide the cursor on form1_mousemove and it would only do it part of the time. Any ideas?
(If this comment was disrespectful, please report it.)

 
4/19/1999 6:14:00 AMEvan

Writing a simple form to return command, when
the form ( saved as .exe -> .scr) is called from
display, command reads something like
'/p 2396'
Any ideas?
(If this comment was disrespectful, please report it.)

 
4/25/1999 5:18:00 PMMatthew

Don't worry about the Cursor not hiding. I have the same problem, but it always works once you make it into an exe (actually a .scr)
(If this comment was disrespectful, please report it.)

 
4/26/1999 11:44:00 AMGavin

dim strCmdLine as string

strCmdLine = left(Command,2)

if strCmdLine = "/p" then
End
elseif strCmdLine = "/c" then
'Function to call when 'Settings'
' button is pushed
end if

I place this code in a module (where I do various other checks, etc), but it'll probably work in Form_Load too.
(If this comment was disrespectful, please report it.)

 
6/12/1999 3:23:00 PMbob

If you're having problems with nothing moving, make sure you put a timer control in the form with the interval between 100 and 500! Hope this helps.
(If this comment was disrespectful, please report it.)

 
6/16/1999 3:19:00 PMHVPro

the MinButton = false and stuff isn't working on VB 5... =(
(If this comment was disrespectful, please report it.)

 
6/20/1999 1:07:00 PMD

dis a a koo code
i didnt have a signle prob wif it
you can add all the lines you want
just modify the code a lil
and dont foget to set the color of the lines.....
hella job on dis scrnsvr


(If this comment was disrespectful, please report it.)

 
7/2/1999 11:47:00 AMMe

I would like to know what code I need to add to keep the program from executing more than once by the windows screen saver utility. it executes it every time your set time expires. so if your delay is 3 minutes, every three minutes another instance of the program starts.

(If this comment was disrespectful, please report it.)

 
7/3/1999 11:40:00 AMchunmeng

hi, "Me"
maybe you can try this:

if App.PrevInstance = true then end

then the screen saver won't run if it detect itself running.

(If this comment was disrespectful, please report it.)

 
7/3/1999 6:22:00 PMBarely Stable Technologies

Too long to put in 1000 characters...
Goto here: Http://www.cris.com/~Janetda/bs/screensaver.html
its covers everything...
(If this comment was disrespectful, please report it.)

 
7/16/1999 1:11:00 PMJoe

A simple fix to the saver starting when you open the properties menu -

If Not Command$ = "/s" Then
End
End If
Windows starts savers with the /s switch, so this keeps the saver from loading (kind of) unless Windows is calling it.

(If this comment was disrespectful, please report it.)

 
7/18/1999 11:17:00 AMemory

it's a great stuff...
keep on creating such as this!
it's cool!
(If this comment was disrespectful, please report it.)

 
7/19/1999 10:48:00 AMET

Great Code!!!

I've decided to make a screen saver for my
work. I want to have or Logo bouncing around
and binary code scrolling in the background.

Problems:
A. - Whenever I try to load it from the windows dialog,
it automatically loads
B. - I don't know how to make a settings dialog for when
the user presses "Settings"

Help me, please
Thanks
--ET
(If this comment was disrespectful, please report it.)

 
7/23/1999 11:04:00 AMStealth

Hey ET:

The command for the settings window is "/c" instead of "/s".

Hope that helps
(If this comment was disrespectful, please report it.)

 
8/15/1999 11:38:00 AMjyoti rathod

IT REALLY GOOD WORK...HOPE SO SEE SOME MORE IN THE FUTURE
BSET OF LUCK

(If this comment was disrespectful, please report it.)

 
8/27/1999 11:45:00 AMLunacy

This Works Fine for me
(If this comment was disrespectful, please report it.)

 
8/28/1999 6:22:00 PMh

/p ####
the #### is the handle to the little window that you can make a preview of the saver in
(If this comment was disrespectful, please report it.)

 
10/29/1999 4:48:00 PMDoug

This code werks great!!
(If this comment was disrespectful, please report it.)

 
11/13/1999 3:31:00 AMsun- hee

Great Code!
Thank you!!!
(If this comment was disrespectful, please report it.)

 
12/24/1999 8:50:46 AMRicky

I've manage to get it to work after some trials and changes, but when the saver is deactivated ... the password protect doesn't work. Can anybody help ??
(If this comment was disrespectful, please report it.)

 
1/12/2000 6:17:04 PMUltimatum

I sure would like to know how you people figure this kinda stuff out =)
(If this comment was disrespectful, please report it.)

 
1/22/2000 4:34:24 PMmobile

Can Someone post a zip version of this including all the changes to the prog as in the feedback's Im only getting half conversations. Ta
(If this comment was disrespectful, please report it.)

 
1/31/2000 6:30:08 PMsteve

HELP! i have vb4 32bit and i cant get it too work

(If this comment was disrespectful, please report it.)

 
6/9/2000 5:43:41 PMetrask

I didnt make a scrnsvr i just used the always on top code do you have any ideas as to how to make it always activated, too (IE: user cant click off the form or theyget a beep? like a login screen?)
(If this comment was disrespectful, please report it.)

 
8/3/2000 11:53:16 AMMatthew Dale Ernst

Im have trouble, I wanted to use a series of pictures and I cant figure out how.
Can you help me?
thanks, Matthew Dale Ernst
Maxfieldstanton@hotmail.com

(If this comment was disrespectful, please report it.)

 
6/2/2003 6:39:29 PM

i did some messing around a while back
if you create any program and rename it to .scr then select it as your screensaver it will pop up as a screensaver but without putting in the code to stop your program from running twice (wich you could do with a reg key or txt file) on execute check for the file or key then close or create the file.key create the file then on closing kill the file or change the regkey to another value there is a harder more complex way to do it but it takes too much code and time
(If this comment was disrespectful, please report it.)

 
6/2/2003 6:53:51 PM

if you put in your own animation on a form then it will do the animation
if you want to put upa the tme then you can do that (only thing is it wont be restricted by the screensaver password
(If this comment was disrespectful, please report it.)

 
10/15/2003 4:08:58 PM

hi:

i am looking for a way to write the code into the functions below to calculate the checksum for a given number.and verify the validity of the number based on the check sum.


(If this comment was disrespectful, please report it.)

 
10/14/2005 9:08:04 PMTara

Hi, Thanks for writing the code it has been very helpful. My whole class has this problem, that when the screensaver is installed, instead of starting at the correct time, it just puts a little box on the bottom left side of the screen. Like it is minimized. Or, it starts up every minute without there being no activity. Can someone help us please?? Thanks
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.