Quick Search for:  in language:    
Create,screen,saver
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Visual Basic Stats

 Code: 3,011,557. lines
 Jobs: 115. postings

 How to support the site

 
Sponsored by:

 

You are in:

 
Login



Latest Code Ticker for Visual Basic.
AniViewer
By Jerrame Hertz on 6/30


Click here to see a screenshot of this code!Raw Packet Sniffer
By Coding Genius on 6/30

(Screen Shot)

Check the support of a record set
By Freebug on 6/30


B++ Builder - VB without runtimes
By Anthonius on 6/30


Mr Blonde - Chat Program
By Mr Blonde on 6/30


MSN Messenger Status Detector
By Ryan Cain on 6/30


MSN advanced
By alias1990 on 6/30


MSN Messenger advanced
By alias1990 on 6/30


Locate Database
By Erica Ziegler-Roberts on 6/30


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

Creating a Screen Saver

Print
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

Users have accessed this code 46463 times.
 
 
     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 V
'     B!
' By: VB Tips and Source Code
'
'This code is copyrighted and has' limited warranties.Please see http://w
'     ww.Planet-Source-Code.com/vb/scripts/Sho
'     wCode.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 dete ' rmine whether ' to make the specified form always on t ' op 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 u ' p ' If Y2=1 then moving down else moving u ' p 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 notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
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 PM:Dan
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???
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/15/1999 7:01:00 PM:Josh
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!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/16/1999 2:25:00 AM:Arje
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/16/1999 8:46:00 PM:Matt
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?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/19/1999 6:14:00 AM:Evan
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?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/25/1999 5:18:00 PM:Matthew
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)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/26/1999 11:44:00 AM:Gavin
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/12/1999 3:23:00 PM:bob
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/16/1999 3:19:00 PM:HVPro
the MinButton = false and stuff isn't 
working on VB 5... =(
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/20/1999 1:07:00 PM:D
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/2/1999 11:47:00 AM:Me
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/3/1999 11:40:00 AM:chunmeng
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/3/1999 6:22:00 PM:Barely Stable Technologies
Too long to put in 1000 
characters...
Goto here: 
Http://www.cris.com/~Janetda/bs/screensa
ver.html
its covers everything...
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/16/1999 1:11:00 PM:Joe
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.
 
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/18/1999 11:17:00 AM:emory
it's a great stuff...
keep on creating 
such as this!
it's cool!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/19/1999 10:48:00 AM:ET
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/23/1999 11:04:00 AM:Stealth
Hey ET:
The command for the settings 
window is "/c" instead of "/s".
Hope 
that helps
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/15/1999 11:38:00 AM:jyoti rathod
IT REALLY GOOD WORK...HOPE SO SEE SOME 
MORE IN THE FUTURE
BSET OF LUCK
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/27/1999 11:45:00 AM:Lunacy
This Works Fine for me
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/28/1999 6:22:00 PM:h
/p ####
the #### is the handle to the 
little window that you can make a 
preview of the saver in
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/29/1999 4:48:00 PM:Doug
This code werks great!!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/13/1999 3:31:00 AM:sun- hee
Great Code!
Thank you!!!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/24/1999 8:50:46 AM:Ricky
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 
??
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/12/2000 6:17:04 PM:Ultimatum
I sure would like to know how you 
people figure this kinda stuff out =)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/22/2000 4:34:24 PM:mobile
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/31/2000 6:30:08 PM:steve
HELP!  i have vb4 32bit and i cant get 
it too work
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/9/2000 5:43:41 PM:etrask
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?)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/3/2000 11:53:16 AM:Matthew 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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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 
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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 
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | Visual Basic Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.