Tipp 0350 Analoge Uhr -2-
Autor/Einsender:
Datum:
  Detlev Schubert
06.08.2003
Entwicklungsumgebung:   VB 5
Bekanntlich führen viele Wege nach Rom. Ein Beispiel, wie eine analoge Uhr programmiert werden kann, ist in unserem Tipp Analoge Uhr - 1 - zu sehen.
Auch in diesem Tipp werden alle Elemente mit den VB-Funktionen Line und Draw gezeichnet. Der entscheidende Unterschied zum vorgenannten Tipp besteht jedoch darin, dass sich die Uhr der Größe der Form anpasst.
 
Option Explicit

Dim CenterX As Long, CenterY As Long, MySize As Long
Dim PI As Double
Dim DPicture As PictureBox

Private Sub Form_Load()
  PI = 3.14159265359
  Set DPicture = Picture1
End Sub

Private Sub Form_Resize()
  Picture1.Width = Form1.ScaleWidth - Picture1.Left * 2
  Picture1.Height = Form1.ScaleHeight - Picture1.Top * 2
  CenterX = Picture1.ScaleWidth / 2
  CenterY = Picture1.ScaleHeight / 2
  If CenterX > CenterY Then CenterX = CenterY
  If CenterY > CenterX Then CenterY = CenterX
  MySize = CenterX / 50
  Picture2.Top = Picture1.Top
  Picture2.Left = Picture1.Left
  Picture2.Width = Picture1.Width
  Picture2.Height = Picture1.Height
End Sub

Private Sub Timer1_Timer()
  Timer1.Interval = 1000
  If DPicture = Picture1 Then
    Set DPicture = Picture2
  Else
    Set DPicture = Picture1
  End If
  DrawClock
  DPicture.ZOrder 0
End Sub

Private Sub DrawDot(X1, Y1)
  DPicture.Line ((X1 + CenterX) - MySize, _
        (Y1 + CenterY) - MySize)-((X1 + CenterX) + MySize, _
        (Y1 + CenterY) - MySize), RGB(255, 255, 255), B
  DPicture.Line ((X1 + CenterX) - MySize, _
        (Y1 + CenterY) - MySize)-((X1 + CenterX) - MySize, _
        (Y1 + CenterY) + MySize), RGB(255, 255, 255), B
  DPicture.Line ((X1 + CenterX) - MySize, _
        (Y1 + CenterY) + MySize)-((X1 + CenterX) + MySize, _
        (Y1 + CenterY) + MySize), RGB(0, 0, 0), B
  DPicture.Line ((X1 + CenterX) + MySize, _
        (Y1 + CenterY) - MySize)-((X1 + CenterX) + MySize, _
        (Y1 + CenterY) + MySize), RGB(0, 0, 0), B
End Sub

Private Sub DrawClock()
  Dim X As Long, Y As Long, Rad As Double
  Dim X1 As Long, Y1 As Long

  DPicture.Cls

  X = CenterX

  Y = MySize * 2
  For Rad = PI * 2 To 0 Step -(PI / 6)
    X1 = Math.Cos(Rad) * (X - CenterX) + _
         Math.Sin(Rad) * (Y - CenterY)
    Y1 = Math.Cos(Rad) * (Y - CenterY) - _
         Math.Sin(Rad) * (X - CenterX)
    DrawDot X1, Y1
  Next Rad

  Y = MySize * 20
  DPicture.DrawWidth = MySize / 5

  Rad = -PI / 6 * Hour(Time)
  X1 = Math.Cos(Rad) * (X - CenterX) + _
       Math.Sin(Rad) * (Y - CenterY)
  Y1 = Math.Cos(Rad) * (Y - CenterY) - _
       Math.Sin(Rad) * (X - CenterX)
  DPicture.Line (CenterX, CenterY)-(X1 + CenterX, Y1 + CenterY), _
        RGB(0, 0, 0)

  Y = MySize * 10
  DPicture.DrawWidth = MySize / 5

  Rad = -PI / 30 * Minute(Time)
  X1 = Math.Cos(Rad) * (X - CenterX) + _
       Math.Sin(Rad) * (Y - CenterY)
  Y1 = Math.Cos(Rad) * (Y - CenterY) - _
       Math.Sin(Rad) * (X - CenterX)
  DPicture.Line (CenterX, CenterY)-(X1 + CenterX, Y1 + CenterY), _
        RGB(0, 0, 0)

  Y = MySize * 4
  DPicture.DrawWidth = 1

  Rad = -PI / 30 * Second(Time)
  X1 = Math.Cos(Rad) * (X - CenterX) + _
       Math.Sin(Rad) * (Y - CenterY)
  Y1 = Math.Cos(Rad) * (Y - CenterY) - _
       Math.Sin(Rad) * (X - CenterX)
  DPicture.Line (CenterX, CenterY)-(X1 + CenterX, Y1 + CenterY), _
        RGB(255, 0, 0)

  Form1.Caption = Format(Time, "hh:nn:ss")
End Sub
 
Weitere Links zum Thema
Analoge Uhr - 1 -
Countdown
Exakte Zeitmessung

Windows-Version
95
98/SE
ME
NT
2000
XP
Vista
Win 7
VB-Version
VBA 5
VBA 6
VB 4/16
VB 4/32
VB 5
VB 6


Download  (4,5 kB) Downloads bisher: [ 3665 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Projekte | Tutorials | API-Referenz | VB-/VBA-Tipps | Komponenten | Bücherecke | VB/VBA-Forum | VB.Net-Forum | DirectX-Forum | Foren-Archiv | DirectX | VB.Net-Tipps | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Freitag, 16. September 2011