-
This is rather useless, but it's a nifty procedure for introducing a form to the screen that I just wrote. I don't feel like posting a demo, so I'll leave that up to you. You'll need five forms. Form1, Form2, Form3, Form4, and Main. The Form#'s should have a background color of straight green (RGB 0, 255, 0), as should the Main form. The Form#'s should be border style 0 (none) and the Main form can be whatever the hell you want. Main should be set to CenterScreen, too. You'll have to declare the Sleep API call, too. With that said, on with the show:
Make the startup object Main. Pop it open and put this in the Form_Load.
Code:
Private Sub Form_Load()
Call OpenAnim
End Sub
Now, of course, we've gotta make the OpenAnim sub. Excuse my sloppy coding.
Code:
Sub OpenAnim()
' Form1 and 2 are horizontal.
' Form3 and 4 are vertical.
Main.Visible = True
Main.Visible = False 'This is neccesary to get form Main to set it's corrdinates. That's VB for you- go figure.
DoEvents
Part0:
'Starting placement... This'll put the four forms where they need to be, hopefully independent of the placement of Main and your screen resolution.
Form1.Height = Screen.Height / 50
Form1.Width = Main.Width
Form1.Top = Main.Top
Form1.Left = 0 - Form1.Width
Form2.Height = Screen.Height / 50
Form2.Width = Main.Width
Form2.Top = Main.Top + Main.Height - Form2.Height
Form2.Left = Screen.Width
Form3.Width = Screen.Width / 50
Form3.Height = Main.Height
Form3.Top = 0 - Form3.Height
Form3.Left = Main.Left
Form4.Width = Screen.Width / 50
Form4.Height = Main.Height
Form4.Top = Screen.Height
Form4.Left = Main.Left + Main.Width - Form4.Width
Form1.Visible = True
Form2.Visible = True
Form3.Visible = True
Form4.Visible = True
DoEvents
Part1: 'This is where the first stage of animation takes place.
For i = 1 To 400
If Form1.Left < Main.Left Then
Form1.Left = Form1.Left + 50
Else
Form1.Left = Main.Left
End If
If Form2.Left > Main.Left Then
Form2.Left = Form2.Left - 50
Else
Form2.Left = Main.Left
End If
If Form3.Top < Main.Top Then
Form3.Top = Form3.Top + 37.5
Else
Form3.Top = Main.Top
End If
If Form4.Top > Main.Top Then
Form4.Top = Form4.Top - 37.5
Else
Form4.Top = Main.Top
End If
If Form1.Left = Main.Left And Form2.Left = Main.Left And Form3.Top = Main.Top And Form4.Top = Main.Top Then
GoTo Part2
End If
DoEvents
Sleep 10 'You can meddle with this to taste. Smaller numbers are faster.
Next i
Part2:
Sleep 500 'Ditto.
DoEvents
BufferHeight = Main.Height
BufferWidth = Main.Width
Main.Height = 30
Main.Width = 40
Main.Left = Screen.Width / 2 - Main.Width / 2
Main.Top = Screen.Height / 2 - Main.Height / 2
Main.Visible = True
DoEvents
For i = 1 To 800 'This is the second stage of animation.
Main.Left = Screen.Width / 2 - Main.Width / 2
Main.Top = Screen.Height / 2 - Main.Height / 2
DoEvents
If Main.Width < BufferWidth Then
Main.Width = Main.Width + (10 * 2)
Else
Main.Width = BufferWidth
End If
If Main.Height < BufferHeight Then
Main.Height = Main.Height + (7.5 * 2)
Else
Main.Height = BufferHeight
End If
Main.Left = Screen.Width / 2 - Main.Width / 2
Main.Top = Screen.Height / 2 - Main.Height / 2
DoEvents
Sleep 10
If Main.Height = BufferHeight And Main.Width = BufferWidth Then GoTo Part4
Next i
Part4:
Form1.Hide
Form2.Hide
Form3.Hide
Form4.Hide
For i = 255 To 0 Step -1 'Here we change the color of Main from black to green. You can mess with this to change it to whatever you want.
Main.BackColor = RGB(0, i, 0)
DoEvents
Sleep 10
Next i
End Sub
And that's a wrap. You can tack more code onto the end of this to make all the controls that may or may not be on the Main form appear. If anybody else has any nifty animations, post 'em here. I'm always interested in this kind of thing.
~Zero the Inestimable
-
Hey zero thats pretty cool! just don't forget to DIM those 3 variables ;)
-
No one said he has option explicit on :p
But anyways, it looks pretty flashy. I didnt try it yet hold on im on it...
-
wow, thats pretty cool!
Hey I have a question. Can I use the Sleep API (with doevents) for a more accurate timer for my game?
Code:
Do
DoEvents
AiThink
DoUnits
DoHud
DrawScrn
DoEvents
Sleep 20
Loop
? It's an RTS like starcraft and I need a really accurate timer if I have a hope in hell of getting it multiplayer.
-
I don't think you want to use the Sleep function for that purpose, the Sleep function pauses your app for the requested number of milliseconds, i.e.
Code:
Sleep 5000 ' pause program for 5 seconds
-
ya I know how to use it dude.
but doesnt it pause like the whole windows? Thats why you need the doevents right?
-
It pauses your app (in fact if you pause it for too long your app will show up in the task manager as 'Not Responding') in other words the DoEvents that is after the Sleep sub will not occur until the requested amount of time is finished.
-
Timers, delays, &c.
Using the Sleep API call pauses your entire application. It pauses EVERYTHING in your entire application. This means that if you have something like this:
Code:
Label1.Visible = True
Sleep 1000
Label1.Forecolor = RGB(255,0,128)
Sleep 1000
You'd think Label1 would appear, wait one second, and then turn purple, right? Wrong. All you'd see is nothing for a delay of two seconds, and then LAbel1 would pop up, both visible and purple. Y'see, Label1 would become visible as far as the code is concerened, but not on the screen. The Sleep call jumps right in there and pauses everything without waiting for a screen refresh. That's what the DoEvents is for. It doesn't make the Sleep call any more accurate, but it makes the results visible. Thus:
Code:
Label1.Visible = True
DoEvents
Sleep 1000
Label1.Forecolor = RGB(255,0,128)
Doevents
Sleep 1000
This would give you the desired result, Label1 appearing, waiting one second, and turning purple.
Now, for more accurate delays that don't meddle with the operation of your program, you can use GetTickCount. GetTickCount is a call that returns an insanely large number that is the exact number of milliseconds that Windows has been running since it was booted. The GetTickCount API declaration goes like this:
Code:
Declare Function GetTickCount Lib "kernel32" () As Long
Operation is dead simple, you just drop it into a variable:
Code:
Variable = GetTickCount
Now, to actually make a delay, you need to do a little fancy footwork. You'll need two varibles. One should be a global (so it will remain if the subroutine happens to end) called something like "GlobalDelay". The second is just a temporary variable and can be called "Temp" or something. Also, you'll have to set GlobalDelay to the current tick count at the beginning of your program like this:
Code:
Sub Form_Load()
GlobalDelay = GetTickCount
End Sub
Now, for a delay of a set amount, it would work like this:
Code:
Temp = GetTickCount 'Get the current tick count into Temp.
Do While Temp < GlobalDelay + 1000 'Compare Temp to GlobalDelay. You can change the 1000 there to anything you want.
DoEvents 'Keeps it from meddling with program operation.
Loop
GLobalDelay = Temp 'Sets up GlobalDelay for next time.
You can tack other things inside the loop to repeat during the delay (screen refreshes, mayhap?) or yo can put things below it and constantly loop back to the top of the procedure, which gives you the delay, executes the code after it, gives the delay, executes the code, &c.
And that's a wrap. Hope that helps you.
~Zero the Inestimable
-
Timer?
Why don´t just use the "Timer" ?
Doesn´t it work the same way? If not, what´s the difference?
-
Yeah, but Timers are EVIL!!! [url"http://orion.spaceports.com/~mccloud/english/coding/tutorial/1.2.html"]Click here for more information ;)[/url]
-
You could use Windows API timers, they're not quite so evil as timer controls ;) The functions are SetTimer and KillTimer if I remember right.
-
Q:
how do you declare the sleep API call?
explain it to me since I am a newbie, but learning real fast.
Scoutt
-
Sleep API Call
The sleep API call decalre is as follows. It goes in the Declarations section of a module (Not the General Declarations section of a form!).
Code:
'Sleep API Declare
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
'Sleep Syntax
Sleep 500 'The 500 is milliseconds, thus 1000 would be one full second.
And just for the hell of it, the GetTickCount API call declare is as follows. It does in the same place as Sleep.
Code:
'GetTickCount API Declare
Declare Function GetTickCount Lib "kernel32" () As Long
'GetTickCount Syntax
Variable = GetTickCount
~Zero the Inestimable
-
Thanks Zero, I understand it now. I think that was very cool too
Scoutt