Option Explicit
Dim HourLength As Integer, MinuteLength As Integer, _
SecondLength As Integer
Dim MidX As Integer, MidY As Integer
Const PI = 3.14159
Sub LengthAndCentre()
Dim d As Integer
If Me.ScaleWidth < Me.ScaleHeight Then
HourLength = Me.ScaleWidth * 50 / 200
MinuteLength = Me.ScaleWidth * 80 / 200
SecondLength = Me.ScaleWidth * 90 / 200
Else
HourLength = Me.ScaleHeight * 50 / 200
MinuteLength = Me.ScaleHeight * 80 / 200
SecondLength = Me.ScaleHeight * 90 / 200
End If
MidX = Me.ScaleWidth \ 2
MidY = Me.ScaleHeight \ 2
Line1.X1 = MidX
Line2.X1 = MidX
Line3.X1 = MidX
'
Line1.Y1 = MidY
Line2.Y1 = MidY
Line3.Y1 = MidY
d = Shape1.BorderWidth \ 2
Shape1.Left = d
Shape1.Top = d
Shape1.Width = Me.ScaleWidth - d * 2
Shape1.Height = Me.ScaleHeight - d * 2
Call Timer1_Timer
End Sub
Private Sub DrawDial()
Dim I, HourX, HourY, MinuteX, MinuteY, DialLength As Integer
Me.Cls
If Me.ScaleWidth < Me.ScaleHeight Then
DialLength = Me.ScaleWidth * 92 / 200
Else
DialLength = Me.ScaleHeight * 92 / 200
End If
For I = 1 To 12
Me.DrawWidth = 4
HourX = DialLength * Cos(PI / 180 * (30 * I - 90)) + MidX
HourY = DialLength * Sin(PI / 180 * (30 * I - 90)) + MidY
PSet (HourX, HourY)
Next I
For I = 1 To 59
Me.DrawWidth = 2
MinuteX = DialLength * Cos(PI / 180 * (6 * I - 90)) + MidX
MinuteY = DialLength * Sin(PI / 180 * (6 * I - 90)) + MidY
PSet (MinuteX, MinuteY)
Next I
End Sub
Private Sub Form_Load()
Me.Caption = "Best Clock"
Me.AutoRedraw = True
Me.BackColor = &H80FF&
'
Shape1.BorderWidth = 4
Shape1.BorderColor = vbBlue
Line1.BorderWidth = 5
Line2.BorderWidth = 3
Line3.BorderWidth = 1
Line3.BorderColor = vbRed
'
Timer1.Interval = 1000
'
Call LengthAndCentre
Call Timer1_Timer
MsgBox "Resize the window To resize the clock...", , _
"Best Clock"
End Sub
Private Sub Form_Resize()
On Error Resume Next
Call LengthAndCentre
Call DrawDial
End Sub
Private Sub Timer1_Timer()
Dim Hours As Single, Minutes As Single, Seconds As Single
Dim TrueHours As Single
'Beep
Hours = Hour(Time)
Minutes = Minute(Time)
Seconds = Second(Time)
TrueHours = Hours + Minutes / 60
'HourHand
Line1.X2 = HourLength * Cos(PI / 180 * (30 * TrueHours - 90)) + MidX
Line1.Y2 = HourLength * Sin(PI / 180 * (30 * TrueHours - 90)) + MidY
'MinuteHand
Line2.X2 = MinuteLength * Cos(PI / 180 * (6 * Minutes - 90)) + MidX
Line2.Y2 = MinuteLength * Sin(PI / 180 * (6 * Minutes - 90)) + MidY
'SecondsHand
Line3.X2 = SecondLength * Cos(PI / 180 * (6 * Seconds - 90)) + MidX
Line3.Y2 = SecondLength * Sin(PI / 180 * (6 * Seconds - 90)) + MidY
End Sub