Solved

using a doevent with a screen saver

Posted on 1998-12-03
3
231 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now