Solved

using a doevent with a screen saver

Posted on 1998-12-03
3
234 Views
Last Modified: 2010-05-03
I want to write a VB program that works as a screen saver/security program.
The first form will have one type of animation on it that fills the screen, then
with a key press event, the second form with a different animation program
will start. With a mouse click, form 3, a password screen, will come in.
How and where do I place the DoEvent to stop the animation loop. Do you
have any examples of this kind of a program?
Thank you
0
Comment
Question by:rscptn
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
trillo earned 100 total points
ID: 1448075
First lets make a simple Draw routine (it will draw a circle with random dimentios.

Public Sub Draw(canvas as Object)
    Dim x As Integer
    Dim Y As Integer
    Dim radius As Integer
    Dim Colr As Long
    Dim i As Integer
    ScreenWidth = canvas.Width
    ScreenHeight = canvas.Height
    x = Rnd * ScreenWidth
    Y = Rnd * ScreenHeight
    Colr = Rnd * &HFFFFFF
    radius = Rnd * ScreenWidth / 400 * Size * Size
    canvas.FillColor = Colr
    canvas.FillStyle = vbFSSolid
    canvas.Circle (x, Y), radius, Colr
End Sub

Ideally you place a timer on the form that will be used to make the drawings, and place there the draw routine and this way you don't need to use the "DoEvents" function.

Private Sub Timer1_Timer()
    Draw Me
End Sub

The other possibillity is placing a endless loop when the form is made visible and the put some code like this.

Do
    Draw Me
    DoEvents
Loop While Status=1

-------------
Status is a global flag that is equal to 0, and you set it to 1 whenever you want to screensaver to terminate

trillo
0
 

Author Comment

by:rscptn
ID: 1448076
Would you clarify for me how to use the keypress and mouse click events to trigger the doevent and switch between two screen savers and a password screen.
Thanks
0
 
LVL 2

Expert Comment

by:trillo
ID: 1448077
I Can guess that you're not very familiar with the doEvents function so i'll expalin.
Supose you have and pseudo infinite loop such as next example, and supose your code provides a "Cancel" button to stop the loop.
When the user presses the cancel button, you set a flag to True.

Dim SomeFlag as Boolean

Private Sub Cancel_Click()
    SomeFlag = True
EndSub

' Let's say your loop looks like
While someflag = False do
    SomeFunction()
Loop

This code doesn't work because when you enter the loop, you'll never get out of it, because YOU DON'T GIVE A CHANCE TO THE CANCEL BUTTON TO BE PRESSED, meaning that the Flag will never change. The execution will remain in the loop, so the code for the cancel button will never be reached. That is the purpose of the DoEvents function.

' Now the loop looks like
While someflag = False do
    SomeFunction()
    DoEvents
Loop

Here, the DoEvents function allows the program to execute pending tasks (such as the code in the cancel button), and when all other tasks are done, DoEvents returns.

Now let's go back to the ScreenSaver.
Your loop, somewhere in Form1 looks like
.....
   While someflag = False do
       UpdateScreenSaver()
       DoEvents
   Loop
   Form2.show
   Unload Me

Private Sub form1_MouseMove(...)
    someflag=True
End Sub

Here's how it works, you will update teh ScreenSaver, then are calling DoEvents, this will check all pneding tasks, for example the mousemove event. If the user has not move the mouse, someFlag will still be False and your loop will Update the ScreenSaver again, and so forth. when the user moves the mouse, the screen saver stops because your SomeFlag is now True. this will pass the execution to the next line, loading form2 and unloading Form1.

On form2, you place a similar code to show the PassWord Screen.

If you still have questions, tell me.
Trillo
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Pull multiple cvs files into one access table 28 69
Protecting vb6 & .Net code Obfuscation 18 152
Child Form in front 4 49
Set email body to html using vbscript 6 49
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question