English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

1 answers

1. Make an Analogue Clock

For this I am using a small form, (2260 x 2310 in pixels).

You will need a label (name = label1) somewhere near the bottom and a timer control (name = timer1, interval = 100, enabled = true).

This uses trigonometry, to calculate where the hands should be.

In Declarations:

Option Explicit
Const pi = 3.141592654
Dim MyTime As String
Dim sec As Integer, hr As Integer, Min As Integer
Dim X As Integer, Y As Integer
Dim x1 As Integer, y1 As Integer
Dim xsold As Integer, ysold As Integer
Dim xmold As Integer, ymold As Integer
Dim xhold As Integer, yhold As Integer
Dim Rad
Dim a As Integer

In Form_Load:

xsold = x1 ' This is the old x for the seconds
ysold = y1 ' likewise this is for the mins
xmold = x1
ymold = y1
xhold = x1
yhold = y1

In timer1_Timer()

Dim c1, c2, c3 ' Variant since no As

c1 = RGB(255, 255, 0)
c2 = RGB(0, 0, 255)
c3 = RGB(255, 0, 0)
label1.Caption = Time$
MyTime = Time$
sec = Val(Right(MyTime, 2))
Min = Val(Mid(MyTime, 4, 2))
hr = Val(Left(MyTime, 2))
If hr > 12 Then hr = hr - 12

Call runtime(sec, c1, 5)
If Int(a / 5) = (a / 5) Then Call runtime(Min, c2, 4)

If a = 0 Then Call runtime(hr, c3, 2)

a = a + 1
If a = 29 Then a = 0

What I've done here is called the runtime sub at different times through out 30 secs, for the secs. mins and hrs. This make the animation less "jurky".

Now make this procedure:

Public Sub runtime(value As Variant, colour As Variant, i As Integer)

Circle (x1, y1), 550, RGB(255, 255, 255)
If i = 2 Then
Rad = (30 * value) * (pi / 180)
Else
Rad = (6 * value) * (pi / 180)
End If
X = x1 + (i * 100 * (Sin(Rad)))
Y = y1 - (i * 100 * (Cos(Rad)))
Select Case i
Case 5
frmClock.Line (x1, y1)-(xsold, ysold), RGB(0, 0, 0)
frmClock.Line (x1, y1)-(X, Y), colour
xsold = X
ysold = Y
Case 4
frmClock.Line (x1, y1)-(xmold, ymold), RGB(0, 0, 0)
frmClock.Line (x1, y1)-(X, Y), colour
xmold = X
ymold = Y

Case 2
frmClock.Line (x1, y1)-(xhold, yhold), RGB(0, 0, 0)
frmClock.Line (x1, y1)-(X, Y), colour
xhold = X
yhold = Y
End Select


End Sub

So, there you have it, an analogue clock.

2007-03-28 08:01:55 · answer #1 · answered by rod 6 · 0 0

fedest.com, questions and answers