417 421




Visual Basic 6 Black Book:The Timer And Serial Communications Controls
function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) { var end = document.cookie.indexOf (";", j); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(j, end)); } i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } var m1=''; var gifstr=GetCookie("UsrType"); if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; } document.write(m1+m2+m3);            Keyword Title Author ISBN Publisher Imprint Brief Full  Advanced      Search  Search Tips Please Select ----------- Components Content Mgt Certification Databases Enterprise Mgt Fun/Games Groupware Hardware IBM Redbooks Intranet Dev Middleware Multimedia Networks OS Prod Apps Programming Security UI Web Services Webmaster Y2K ----------- New Titles ----------- Free Archive To access the contents, click the chapter and section titles. Visual Basic 6 Black Book (Publisher: The Coriolis Group) Author(s): Steven Holzner ISBN: 1576102831 Publication Date: 08/01/98 function isIE4() { return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') ); } function bookMarkit() { var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0"; parent.location.href=url; //var win = window.open(url,"myitk"); //if(!isIE4()) // win.focus(); } Search this book:  














Previous
Table of Contents
Next




That’s it. The result of this code appears in Figure 13.5. Now we’ve created a stopwatch in Visual Basic. The code for this example is located in the stopwatch folder on this book’s accompanying CD-ROM.


Figure 13.5  A stopwatch created with the timer control.
Creating An Alarm Clock
Your great-aunt is calling. Why can’t you ever write a program that she can use? She doesn’t use databases, spreadsheets, or word processors. You say, what else is there? She says, how about a nice alarm clock?

You can build an alarm clock using the timer control in Visual Basic. To see how to do that, create a new program now and add a timer, Timer1 , to it, setting the timer’s Interval property to 1000 (that is, 1 second). Add a label named Display and set the font in the label large enough to read easily (we’ll use 48-point Courier New). We’ll need some way of setting when the alarm should go off, so add a text box named AlarmSetting . We’ll also need some way of turning the alarm on or off, so add two option buttons in a control array, OnButton ; give OnButton(1) the caption “Alarm On” and OnButton(2) the caption “Alarm Off”.
Now we’re ready to write some code. Add a form-wide Boolean variable to the (General) section of the form named AlarmOn. We’ll set this variable to True when the user clicks the Alarm On button:


Sub OnButton_Click(Index As Integer)
If (Index = 1) Then
AlarmOn = True
...
End Sub


and we’ll set AlarmOn to False when the user clicks the Alarm Off button:


Sub OnButton_Click(Index As Integer)
If (Index = 1) Then
AlarmOn = True
Else
AlarmOn = False
End If
End Sub


Now in the Timer event handler, we just check if the current time is past the setting in the AlarmSetting text box and if AlarmOn is True (notice that we can do a direct string comparison with Time$ and AlarmSetting.Text):


Sub Timer1_Timer()
If (Time$ > AlarmSetting.Text And AlarmOn) Then
...
End Sub


If the alarm is supposed to sound, we just use the Visual Basic Beep procedure, which will beep each time Timer1_Timer() is called (in other words, once a second) until the user turns the alarm off:


Sub Timer1_Timer()
If (Time$ > AlarmSetting.Text And AlarmOn) Then
Beep
End If
...
End Sub


Finally, we just display the current time in the Display label:


Sub Timer1_Timer()
If (Time$ > AlarmSetting.Text And AlarmOn) Then
Beep
End If
Display.Caption = Time$
End Sub


As an added touch, you can restrict user input in the AlarmSetting text box to valid characters. Here’s how you restrict user input in a text box—when you set the KeyAscii argument to 0, that cancels the struck key:


Sub AlarmSetting_KeyPress(KeyAscii As Integer)
Dim Key As String
Key = Chr$(KeyAscii)
If ((Key < "0" Or Key > "9") And Key <> ":") Then
Beep
KeyAscii = 0
End If
End Sub


And that’s it—now we’ve got a functioning alarm clock, as shown in Figure 13.6. The code for this example is located in the alarm folder on this book’s accompanying CD-ROM.


Figure 13.6  An alarm clock built on the timer control.
Creating Animation Using The Timer Control
A common use for the timer control is to create graphics animation, because the way you create animation is by displaying successive frames of the animation sequence at intervals. That’s a good job for the timer control.

To see how this works, we’ll create an example now. In this example, we’ll just switch back and forth between two simple images, image1.bmp and image2.bmp, which are simply strips of solid color, red and blue.
To store those images in our program, add an image list control, ImageList1 , now. You add image list controls with the Project|Components menu item; click the Controls tab in the Components dialog box that opens, select the Microsoft Windows Common Controls item, and click on OK to close the Components box.
Draw a new image list control, ImageList1, and right-click it, selecting Properties in the menu that opens. We click the Images tab in the image list’s property pages, and we use the Insert Picture button to insert the two images in image1.bmp and image2.bmp into the image list.
Next, add a timer control, Timer1; set its Interval property to 1000 (in other words, 1 second), and set its Enabled property to False. Also add a command button, Command1, with the caption “Start Animation”, and a picture box, Picture1, setting the picture box’s AutoSize property to True so that it resizes itself to fit our images.
That’s it—we’re ready to write some code. We start the animation when the user clicks the Start Animation button by enabling the timer:


Private Sub Command1_Click()
Timer1.Enabled = True
End Sub


We’ll keep track of the current image with a Boolean variable named blnImage1; if this Boolean variable is True, we’ll display the first image in the image list:


Private Sub Timer1_Timer()
Static blnImage1 As Boolean

If blnImage1 Then
Picture1.Picture = ImageList1.ListImages(1).Picture
...


Otherwise, we’ll display the second image in the image list:



Private Sub Timer1_Timer()
Static blnImage1 As Boolean

If blnImage1 Then
Picture1.Picture = ImageList1.ListImages(1).Picture
Else
Picture1.Picture = ImageList1.ListImages(2).Picture
End If
...


Finally, we toggle blnImage1 :


Private Sub Timer1_Timer()
Static blnImage1 As Boolean
If blnImage1 Then
Picture1.Picture = ImageList1.ListImages(1).Picture
Else
Picture1.Picture = ImageList1.ListImages(2).Picture
End If
blnImage1 = Not blnImage1
End Sub


And that’s all we need. When you run the program and click the Start Animation button, shown in Figure 13.7, the animation starts: the picture box flashes red and blue images once a second. Our animation example is a success.


Figure 13.7  Graphics animation with the timer control in Visual Basic.
The code for this example is located in the coloranimation folder on this book’s accompanying CD-ROM.




Previous
Table of Contents
Next






Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.



Wyszukiwarka

Podobne podstrony:
SHS 362 421
15 (421)
417 420
INDEX (421)
01aff02a1cb3ae2b1d5a37b1d85453f7 zip (417 60 kB)
421 423
417 419
417 (B2007) Koszty na przełomie roku
413 417
417 ac
Barok metafizyczny Sępa Szarzyńskiego (renesans, treść, ~421
README (417)
Setra S 417
414 417

więcej podobnych podstron