Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 531
  • Last Modified:

make analog watch within VB2008

Salam

How can I make a analog watch within VB2008

Thanks
0
Ahmadal_najjar2003
Asked:
Ahmadal_najjar2003
1 Solution
 
uucknaaaCommented:
Hey ..

Here's one:

http://www.dreamincode.net/code/snippet225.htm

Check It Out!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is a pared down version of a more complex clock I wrote.  This version simply displays the current time:
Imports System.Drawing.Drawing2D
Public Class Form1
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ac As New AnalogClock
        ac.Location = New Point(10, 10)
        Me.Controls.Add(ac)
    End Sub
 
End Class
 
Public Class AnalogClock
    Inherits UserControl
 
    Private Const PI As Single = 3.1415926535897931
 
    ' these two determine the clock position
    ' counterclockwise motion increases angle
    ' Valid values are 0 --> 359 (six degrees = one minute on clock)
    ' up = 0
    ' left = 90
    ' down = 180
    ' right = 270
 
    ' Time = 3:55
    Private hourHand As Single = 270
    Private minuteHand As Single = 30
    Private secondHand As Single
 
    ' these are the actual time values
    Private _curHour As Integer
    Private _curMinute As Integer
    Private _curSecond As Integer
 
    Public ReadOnly Property Hour() As Integer
        Get
            Return _curHour
        End Get
    End Property
    Public ReadOnly Property Minute() As Integer
        Get
            Return _curMinute
        End Get
    End Property
    Public ReadOnly Property Second() As Integer
        Get
            Return _curSecond
        End Get
    End Property
    Public ReadOnly Property Time() As TimeSpan
        Get
            Return New TimeSpan(Me.Hour, Me.Minute, IIf(Me.ShowSecondHand, Me.Second, 0))
        End Get
    End Property
 
    Private _ShowSecondHand As Boolean = True
    Public Property ShowSecondHand() As Boolean
        Get
            Return _ShowSecondHand
        End Get
        Set(ByVal value As Boolean)
            _ShowSecondHand = value
        End Set
    End Property
 
    Private _ShowCurrentTime As Boolean = True
    Private clockFace As Bitmap
    Private WithEvents BackgroundWorker1 As New System.ComponentModel.BackgroundWorker
 
    Private Sub AnalogClock_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        BackgroundWorker1.WorkerReportsProgress = True
        BackgroundWorker1.RunWorkerAsync()
    End Sub
 
    Public Sub SetClock(ByVal dt As DateTime)
        _curHour = IIf(dt.Hour = 0, 12, IIf(dt.Hour <= 12, dt.Hour, dt.Hour - 12))
        _curMinute = dt.Minute
        _curSecond = dt.Second
 
        hourHand = (-_curHour + 12) * 30
        minuteHand = -6 * (_curMinute - 60)
        secondHand = -6 * (_curSecond - 60)
 
        Me.Refresh()
    End Sub
 
    Private Function DrawClockFace() As Bitmap
        Dim bmp As New Bitmap(Me.Width, Me.Height)
        Using g As Graphics = Graphics.FromImage(bmp)
            Dim p As Point
            Dim a, s As Integer
            Dim cx, cy As Single
 
            ' Compute the center of the UserControl
            cx = Me.Width / 2
            cy = Me.Height / 2
 
            ' Clear the box
            g.Clear(Me.BackColor)
 
            ' Main Circle of the Clock
            Dim radius As Double = Me.Width * 0.8 / 2
            g.DrawEllipse(Pens.Black, New Rectangle(cx - radius, cy - radius, radius * 2, radius * 2))
 
            For a = 0 To 354 Step 6
                ' Minute Tick Marks
                p = GetPoint(cx, cy, a, Me.Width * 0.6 / 2)
                g.DrawRectangle(Pens.Blue, New Rectangle(p.X, p.Y, 1, 1))
 
                If a Mod 30 = 0 Then
                    ' Hour Tick Marks
                    p = GetPoint(cx, cy, a, Me.Width * 0.7 / 2)
                    g.DrawRectangle(Pens.Red, New Rectangle(p.X, p.Y, 1, 1))
 
                    ' Put the Hours (1 to 12) on the Clock Face
                    p = GetPoint(cx - 6, cy - 8, a, Me.Width * 0.9 / 2)
                    s = 12 - a / 30
                    If s = 0 Then
                        s = 12
                    End If
                    g.DrawString(s, Me.Font, Brushes.Black, p.X, p.Y)
                End If
            Next a
        End Using
 
        Return bmp
    End Function
 
    Private Sub AnalogClock_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        If IsNothing(Me.BackgroundImage) Then
            Me.BackgroundImage = DrawClockFace()
        End If
 
        Dim p As Point
        Dim cx As Single = Me.Width / 2
        Dim cy As Single = Me.Height / 2
 
        Using pn As New Pen(Color.Red, 2)
            p = GetPoint(cx, cy, hourHand, Me.Width * 0.48 / 2)
            e.Graphics.DrawLine(pn, cx, cy, p.X, p.Y)
        End Using
 
        Using pn As New Pen(Color.Blue, 2)
            p = GetPoint(cx, cy, minuteHand, Me.Width * 0.58 / 2)
            e.Graphics.DrawLine(pn, cx, cy, p.X, p.Y)
        End Using
 
        If Me.ShowSecondHand Then
            p = GetPoint(cx, cy, secondHand, Me.Width * 0.48 / 2)
            e.Graphics.DrawLine(Pens.Black, cx, cy, p.X, p.Y)
        End If
 
        Dim rc As New Rectangle(cx, cy, 1, 1)
        rc.Inflate(3, 3)
        e.Graphics.FillEllipse(Brushes.Black, rc)
    End Sub
 
    Private Function GetDistance(ByVal Ax As Single, ByVal Ay As Single, ByVal Bx As Single, ByVal By As Single) As Single
        Return Math.Sqrt((Ax - Bx) ^ 2 + (Ay - By) ^ 2) ' Pythagoreans Theorem
    End Function
 
    Private Function GetPoint(ByVal cx As Single, ByVal cy As Single, ByVal angleInDegrees As Single, ByVal distance As Single) As Point
        ' angleInDegrees
        ' Valid values are 0 --> 359 (six degrees = one minute on clock)
        ' draw a line from the center of the circle:
        ' 0 = up
        ' 90 = left
        ' 180 = down
        ' 270 = right
 
        ' Offset and Invert Angle then Convert Degrees to Radians
        angleInDegrees = -(angleInDegrees + 90) * PI / 180
 
        ' Compute the point that is at the specified angle from horizontal and at the specifed distance
        Return New Point(cx + distance * Math.Cos(angleInDegrees), cy + distance * Math.Sin(angleInDegrees))
    End Function
 
    Private Function GetAngleInDegrees(ByVal cx As Single, ByVal cy As Single, ByVal X As Single, ByVal Y As Single) As Single
        ' draw a line from the center of the circle
        ' to the where the cursor is...
        ' If the line points:
        ' up = 0 degrees
        ' right = 90 degrees
        ' down = 180 degrees
        ' left = 270 degrees
 
        Dim dx As Single = X - cx
        Dim dy As Single = Y - cy
        Dim angle As Single
 
        If dx = 0 Then
            ' Straight up and down causes div by zero error
            angle = IIf(dy <= 0, 0, 180)
        Else
            ' Compute Angle in Radians
            angle = Math.Atan(dy / dx)
            ' Convert to Degrees
            angle = angle * (180 / PI)
 
            ' Make it a number from 0 to 360 degrees
            If X > cx Then
                angle = 90 + angle
            Else
                angle = 270 + angle
            End If
        End If
 
        Return angle
    End Function
 
    Private Sub AnalogClock_SizeChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.SizeChanged
        If Me.Height <> Me.Width Then
            Me.Height = Me.Width
        End If
        clockFace = DrawClockFace()
        Me.BackgroundImage = clockFace
    End Sub
 
    Private Function GetHourAngle(ByVal hour As Integer) As Integer
        hour = IIf(hour = 0, 12, IIf(hour <= 12, hour, hour - 12))
        Return (-hour + 12) * 30
    End Function
 
    Private Function GetMinuteAngle(ByVal minute As Integer) As Integer
        Return -6 * (minute - 60)
    End Function
 
    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        While True
            BackgroundWorker1.ReportProgress(0)
            System.Threading.Thread.Sleep(1000)
        End While
    End Sub
 
    Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        Me.SetClock(DateTime.Now)
    End Sub
 
End Class

Open in new window

AnalogClock-BareBonesCurrentTime.jpg
0
 
Ahmadal_najjar2003Author Commented:
very good coding
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now