Two people asked almost simultaneously the same question:
> I like your cute stopwatch, and would love to experiment with it - so much
> more fun than Hello World. Would you be so kind as to send me the source
> code for this? I'm a complete VB idiot, and this would be as good a place
> as any to start playing...
Enclosed below is the complete source code (which can be cut and pasted into
VB):
=======================================
The current version of the stopwatch consists of three objects: Form1 (the
background form), TimeDisplay (a text box), and StartStopButton (a command
button).
FORM1 code:
Option Explicit
Dim DisplayFlag As Long
Dim DisplayState As Long
Sub DisplayTime(starttime)
Dim hours As Single
Dim minutes As Single
Dim seconds As Single
Dim currenttime As Single
Dim elapsedtime As Single
Dim timetext As String
Do
elapsedtime = Timer - starttime
If elapsedtime < 0 Then elapsedtime = 86400 + elapsedtime
currenttime = elapsedtime
hours = Int(currenttime / 3600)
If hours < 10 Then timetext = "0" & hours & ":" Else timetext = hours &
":"
currenttime = currenttime - hours * 3600
minutes = Int(currenttime / 60)
If minutes < 10 Then timetext = timetext & "0" & minutes & ":" Else
timetext = timetext & minutes & ":"
currenttime = Int((currenttime - minutes * 60) * 100) / 100
If currenttime < 10 Then timetext = timetext & "0" & currenttime Else
timetext = timetext & currenttime
If Len(timetext) = 8 Then timetext = timetext & ".00" Else timetext =
Left(timetext & "00", 11)
TimeDisplay.Alignment = vbCenter
TimeDisplay.Text = timetext
DoEvents
Loop While DisplayState And elapsedtime < 86399.95
If elapsedtime >= 86399 Then
TimeDisplay.Text = "23:59:59.99"
StartStopButton.Caption = "Start"
DisplayState = False
End If
End Sub
Private Sub Form_Load()
TimeDisplay.Text = "00:00:00.00"
End Sub
Private Sub Form_Unload(Cancel As Integer)
End
End Sub
Private Sub StartStopButton_Click()
If DisplayState Then
StartStopButton.Caption = "Start"
DisplayState = False
Call SetWindowPos(Form1.hWnd, -2, 0, 0, 0, 0, &H3)
Else
StartStopButton.Caption = "Stop"
DisplayState = True
Call SetWindowPos(Form1.hWnd, -1, 0, 0, 0, 0, &H3)
TimeDisplay.Text = "00:00:00.00"
Call DisplayTime(Timer)
End If
End Sub
MODULE1 code:
Option Explicit
Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal
hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long,
ByVal cy As Long, ByVal wFlags As Long) As Long
=======================================
As you can see, it isn't much code.
When I get some time in the next few days, I'll stick in the alarm function
that Brice wants. You may also notice that I didn't use the Timer Control
function that Tom Emerson thought that I might. There's a fairly profound
philosophical reason for that and I would be pleased to explain it, if anyone
is interested.
The code above is appropriate for Version 2.0 of the BYM Stopwatch (Sunday's
version instead of Saturday's). Version 2 now accounts for (i) the timer
crossing the day boundary (at which time the timer function resets), (ii) the
stopwatch always reside "on top" when counting, and (iii) it automatically
stops at the end of one day at "23:59:59.99".
Wirt Atmar
* To join/leave the list, search archives, change list settings, *
* etc., please visit http://raven.utc.edu/archives/hp3000-l.html *
|