Tools Links Login

Screensaver Tutorial *UPDATED* (Again)

This article is for you who struggle to find out how to create a screensaver with vb. This article is an update to my last one. This one includes a working source code example. I have updated this yet again. This one ends on mouse movements. Please vote for me.

Original Author: Person82484536536578579365897

Code





Screensaver Tutorial


In this tutorial you will learn how to create a
screensaver with visual basic.  It is
fairly easy once you study it so now lets get into the project.


 


 


We will create a screensaver today


 


To start off, start a new project in visual basic.


Name the project “Blanker”.


Add four forms and name one ‘frmMain', one
‘frmSettings', one ‘frmPassSetup' and one ‘frmPassword'. Also add a new module.


 


Click the menu project|Blanker Properties.


In the ‘General' tab, change the ‘Startup Object'
to Sub Main.


Choose the ‘Make' tab and in the title
text box, type “SCRNSAVER Blanker”


Now
click ok and you're ready to start coding!


In
the module you created, copy these API's into it:


 


Option
Explicit 'All variables must be declared


'Constants


Public
Const SW_SHOWNORMAL = 1


Private
Const APP_NAME = "Blanker"


‘API's


‘This
Function Shows and hides the cursor.


Declare
Function ShowCursor Lib "user32" (ByVal bShowstyle="mso-spacerun: yes"> 
As Long) As Long


‘This
function finds if another instance of the saver is running.


Declare
Function FindWindow Lib "user32" Alias
"FindWindowA"(ByVallpClassName As String, ByVal lpWindowName As
String) As Long


clear=all style='page-break-before:always'>

Now copy the main sub:


 


Sub
Main ()


‘This
sub is called when windows wants the screensaver to run a certain event.


‘This
locates what windows wants and activates it.


Select
Case Mid(UCase$(Trim$(Command$)), 1, 2)


 


Case
"/C" 'Configurations mode called


frmSettings.Show
1


 


Case
"", "/S" 'Screensaver mode


runScreensaver


 


Case
"/A" 'Password protect dialog


frmPassSetup.Show
1


Case
"/P" 'Preview mode


‘The
preview mode is very advanced. It is when you see a clip of it on the little
‘monitor. Just leave the monitor screen blank.


End


End
Select


End
Sub


 


Now copy the other
functions that go into the module:


 


Private
Sub runScreensaver() 'Run the screen saver


checkInstance
'Make sure no other instances are running


ShowCursor
False 'Disable cursor


'load
Screen Saver's main form


Load
frmMain


frmMain.Show


End
Sub


 


 


Private
Sub checkInstance()


'If
no previous instance is running, exit sub


If
Not App.PrevInstance Then Exit Sub


 


'check
for another instance of screen saver


If
FindWindow(vbNullString, APP_NAME) Then Exit Sub


 


'Set
our caption so other instances can find


'us
in the previous line.


 


frmMain.Caption
= APP_NAME


End
Sub


 


Sub
exitScreensaver() 'Exit the screensaver


ShowCursor
True ‘Show the cursor


End


End
Sub


Now for the Settings
form.


Set the form's
properties as follows:


 
















Property



Setting



Border Style



4 –
FixedToolWindow



Caption



About



Now just add a label and
set it's caption to “Created By: “ and your name or something.


 


That form had no code.


Now let's move on to the
password setup form.


Set the caption to
“Password” and the border style to 4.


Now add a label that
says “Password:” and add a text box beside it named txtPassword. Now add two
command buttons with one named “cmdOK” and one named “cmdCancel”. Set the
captions to ‘OK' and ‘Cancel'.


Now copy this code:


 


Private
Sub cmdCancel_Click()


Unload
Me


End
Sub


 


Private
Sub cmdOK_Click()


SaveSetting
"Blanker", "Settings", "Password",
txtPassword.Text


Unload
Me


End
Sub


 


Now let's get into the
other password form.


Set the form's settings
as the same as the other password's form's settings.


Now add the same
controls as the password setup form with the same properties.


Now just enter this
code:


 


Private
Sub cmdOK_Click()


If
txtPassword.Text = GetSetting("Blanker", "Settings",
"Password", "") Then


exitScreensaver


Unload
Me


Else


txtPassword.Text
= ""


Me.Hide


MsgBox
"Wrong password! Try again!", vbOKOnly + vbCritical,
"Password"


Unload
Me


End
If


End
Sub


 


Private
Sub cmdCancel_Click()


Unload
Me


End
Sub


 


Now for the main form:


Set the form's
properties as so:
























Property



Setting



BackColor



&H00000000&



BorderStyle



0 – None



Caption



Blanker



WindowState



2 -
Maximized



Now add a line control
anywhere on the form and set it's BorderColor to &H00FFFFFF&. Now just
copy this code and your screensaver will be finished.


 


Public
bWhite As Boolean


Private
Sub Form_Activate()


Line1.X1
= frmMain.Width 2


Line1.Y1
= frmMain.Height 2


Line1.X2
= frmMain.Width 2


Line1.Y2
= frmMain.Height 2


Timer1.Enabled
= True


End
Sub


 


Private
Sub Form_Click()


If
GetSetting("Blanker", "Settings", "Password",
"") <> "" Then


frmPassword.Show
'If a password is set then show the password box


Else


exitScreensaver
'exit the screensaver


End
If


End
Sub


 


Private
Sub Form_DblClick()


If
GetSetting("Blanker", "Settings", "Password",
"") <> "" Then


frmPassword.Show
'If a password is set then show the password box


Else


exitScreensaver
'exit the screensaver


End
If


End
Sub


 


Private
Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)


If
GetSetting("Blanker", "Settings", "Password",
"") <> "" Then


frmPassword.Show
'If a password is set then show the password box


Else


exitScreensaver
'exit the screensaver


End
If


End
Sub


 


Private
Sub Form_KeyPress(KeyAscii As Integer)


If
GetSetting("Blanker", "Settings", "Password",
"") <> "" Then


frmPassword.Show
'If a password is set then show the password box


Else


exitScreensaver
'exit the screensaver


End
If


End
Sub


 


Private
Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)


If
GetSetting("Blanker", "Settings", "Password",
"") <> "" Then


frmPassword.Show
'If a password is set then show the password box


Else


exitScreensaver
'exit the screensaver


End
If


End
Sub


Private
Sub Form_Load()


ShowCursor
False 'Hide the cursor


bWhite
= True


End
Sub


Private
Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As
Single)


If
GetSetting("Blanker", "Settings", "Password",
"") <> "" Then


frmPassword.Show
'If a password is set then show the password box


Else


exitScreensaver
'exit the screensaver


End
If


End
Sub


Private
Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As
Single)


Static
x0 As Integer


Static
y0 As Integer


style="mso-spacerun: yes">    ' Do nothing except in screen saver mode.


style="mso-spacerun: yes">    If RunMode <> rmScreenSaver Then
Exit Sub


 


style="mso-spacerun: yes">    '
Unload on large mouse movements.


style="mso-spacerun: yes">    If ((x0 = 0) And (y0 = 0)) Or _


style="mso-spacerun: yes">        ((Abs(x0 - X) < 5) And (Abs(y0 - Y)
< 5)) _


style="mso-spacerun: yes">        Then


style="mso-spacerun: yes">            ' It's a small movement.


style="mso-spacerun: yes">            x0 = X


style="mso-spacerun: yes">            y0 = Y


style="mso-spacerun: yes">            Exit Sub


style="mso-spacerun: yes">    End If


exitScreensaver


End
Sub


Private
Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)


If
GetSetting("Blanker", "Settings", "Password",
"") <> "" Then


frmPassword.Show
'If a password is set then show the password box


Else


exitScreensaver
'exit the screensaver


End
If


End
Sub


Private
Sub Timer1_Timer()


Line1.BorderWidth
= Line1.BorderWidth + 3 'Increase the border width by 3


If
Line1.BorderWidth >= 1000 Then


Line1.BorderWidth
= 1 'If the line's border width gets bigger than the screen then set it back to
one.


If
bWhite = True Then


Line1.BorderColor
= vbGreen


bWhite
= False


GoTo
This


End
If


If
bWhite = False Then


Line1.BorderColor
= vbWhite


bWhite
= True


End
If


End
If


This:


End
Sub


style='font-weight:normal'>Don't forget: Compile and then change extention
to.scr.


style='font-weight:normal'>Now my tutorial ends on making screen savers. If you
need further instructions because you did not understand, then check the source
code that came with this tutorial.


style='font-size:11.0pt;mso-bidi-font-size:12.0pt'>Edited By: Aaron Lindsay Now
The Age Of Eleven


style='font-size:11.0pt;mso-bidi-font-size:12.0pt'>Created By: Aaron Lindsay Of
The Age Of Ten


style='font-size:11.0pt;mso-bidi-font-size:12.0pt'>Friday, March 30, 2002


style='font-size:11.0pt;mso-bidi-font-size:12.0pt'>Please Vote


style='font-size:11.0pt;mso-bidi-font-size:12.0pt'> 


Note:  When
testing the screensaver from visual basic, don't use the shortcut of F5 to
start it because it will instantly disappear. Click on the ‘>' part.


Note #2: This was created in VB6. There is no guarantee
that it will work on other versions (It won't work on VB4 16 bit version &
lower).


Note #3: If you're making your own screensaver based on
this tutorial, change all the ‘Blanker's to the name of your choice.




About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 90 times

Categories

Visual Basic 6

Attachments

Screensave741784212002.zip
Posted: 9/3/2020 3:45:00 PM
Size: 21,826 bytes


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.