
How-To... - Creating A Screen Saver - Part 2
Now that you have altered your form to behave like a screensaver, you have to design the saver to do something, and have a config box to let the user change the settings of it.
Part 2 shows you how to make a basic scrolling logo, make a config form, and work with INI files.
Step 1
Open up Part 1(if you don't have it, click here to download it). In the General Declarations, paste the following :-
' Below function is used to READ
from INI file
Declare Function GetPrivateProfileString Lib "kernel32"
Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal
lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As
String, ByVal nSize As Long, ByVal lpFileName As String) As Long
' Below function is used to WRITE to INI file
Declare Function WritePrivateProfileString Lib "kernel32"
Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal
lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
'Below : Variables for INI Reading/Writing...
Dim lpAppName As String
Dim lpKeyName As String
Dim lpBuffer As String
Dim nSize As Long
Dim lpDefault As String
Dim lpFileName As String
Dim ret As Long
'Used to read/write text that will be scrolled,
'must be Globabl...
Global ScrollText As String
Now, in the procedure Main, select all the contents, and paste the following code :-
BeforeRun 'Call the BeforeRun procedure...
'If the saver is already running, then end...
If App.PrevInstance Then End
'If the screen saver is told to run by Windows
'then show the main form...
If InStr(Command, "/s") > 0 Then
frmSaver.Show
End If
'If the screen saver is told to run the Config
'box, then show the form, and unload the main form
'NOTE: The main form must be unloaded
If InStr(Command, "/c") > 0 Then
frmConfig.Show
Unload frmSaver
End If
'If the screen saver is told to preview, then
'unload the main form(The preview is the small
'computer screen in the Display Properties...
If InStr(Command, "/p") > 0 Then
Unload frmSaver
End If
Next, paste the code below(under the End Sub part of the Main procedure)
Sub BeforeRun()
'This procedure makes sure the INI file exists
'before the saver starts, and reads the text
'that will be scrolled...
'Fill the INI variables with the appropriate info...
'The section of the INI...
lpAppName = "VB How-To"
'The location of the INI...
lpFileName = "C:\vbht_sv.ini"
'First off all the file needs to be created.
'Always, when writing to INI files,
the file
'be already be created. The INI file used
'here will be created when the Form loads.
'But, if it already exists, then don't create
'it. (Uses the Dir Statement)
'Store vbhowto.ini if the file already exists, else
'nothing will be stored....
temp$ = Dir("C:\vbht_sv.ini")
'If temp$ is greater or less than vbhowto.ini then
'If it is greater or less, this means the file does
'not exist...
If temp$ <> "vbht_sv.ini" Then
'Creates the file and opens it for output
'Just means that the file is open for
'the program to write to it...
Open "C:\vbht_sv.ini" For Output As #1
'Print [VbHowTo] to the file...
Print #1, "[VB How-To]"
'Print Details to the file...
Print #1, "Scroller Text="
Print #1, "Password = "
Close #1 'Close the file...
End If
Read_Ini 'Call the Read_Ini
procedure...
'Load the variable ScrollText into the Label...
frmSaver.lblScroll.Caption = ScrollText
End Sub
On the form, place a label and call it lblScroll - give it any properties you want, apart from LEFT which must be 0. The design I have chosen needs LEFT to be 0 (I admit the design is a bit rough, and sometimes doesn't work properly). Place a timer on the form called tmrScroll and give it an Interval of 10 and make sure ENABLED is TRUE.
Double-click the timer and paste the following code between the Sub and End Sub parts:-
Private Sub tmrScroll_Timer()
lblScroll.Left = lblScroll.Left + 25
If lblScroll.Left >= 11880 Then
lblScroll.Left = -4815
End If
End Sub
Step 3
Insert a new form and give it a name of frmConfig and give it any design you want - just make sure there is two text boxes - txtScrollText and txtPassword - also two buttons - cmdOk and cmdCancel. Enter some text into the txtScrollText TEXT property then save the form and project. Paste the following code into the General Declarations part :-
Private Sub cmdCancel_Click()
Unload Me
End Sub
Private Sub cmdOk_Click()
Write_Ini
End
End Sub
Private Sub Form_Load()
Read_Ini 'Call the Read_Ini procedure...
'Load the scrolling text in the INI file
'into the Text Box...
txtScrollText.Text = ScrollText
End Sub
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 better
working screensaver. Part 3 will fix features that have errors in it(the errors
are suppose to be there :-), and incorporate the password scheme.
You can download this example by clicking here