none
Creating a Analog Clock RRS feed

  • Question

  • Dear Friends,

    Can anyone teach me how to create an Analog Clock (I know only MS VB 2005 / 2008)?
    I would be very happy to have it step by step.

    Thanks
    A Real Novice Programmer !
    Wednesday, December 16, 2009 5:27 AM

Answers

  • Hi V.K.S.B.K_,

    Here is my code for an analogue clock taken from this thread.>>

    http://social.msdn.microsoft.com/Forums/en-US/vbpowerpacks/thread/4de7e0e8-6e6e-46a1-96e8-c5a79c61a09a


    Option Strict On
    
    Public Class Form1
    
        Friend WithEvents myTimer As New Timer
        Private tickSum As Double
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Me.DoubleBuffered = True
            Me.WindowState = FormWindowState.Maximized
            ' 100 here is 1 tenth of a second so 50 lines are drawn every 5 seconds
            'and that is only for the second hand, the minute and hour hands are constantly
            'updated too in proportion to the time.
    
            'If you change the interval to 1000 you will get a second
            'hand tick every second instead of a smooth sweeping second hand.>>
            myTimer.Interval = 100
            myTimer.Start()
        End Sub
    
        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    
            'Used to hold the formCenter.>>
            Dim formCenter As Point
            'Set the FORM BackColor to BLACK.>>
            Me.BackColor = Color.Black
            'Get the center of the FORM.>>
            Dim centreX As Integer = Me.Width \ 2
            Dim centreY As Integer = Me.Height \ 2
            'Assign it to the formCenter point.>>
            formCenter = New Point(centreX, centreY)
            'Set the radius to the full half-height-40.>>
            Dim radius As Integer = centreY - 40
    
            'Draw the 12 hour positions.>>
            Dim x, y, x1, y1, x2, y2 As Double
            Dim myPen As New Pen(Color.White, 10)
            For num As Double = 0 To 2 * Math.PI Step (2 * Math.PI) / 12
                x1 = radius * Math.Cos(num) + formCenter.X
                y1 = radius * Math.Sin(num) + formCenter.Y
                x2 = (radius - 10) * Math.Cos(num) + formCenter.X
                y2 = (radius - 10) * Math.Sin(num) + formCenter.Y
                e.Graphics.DrawLine(myPen, CInt(x1), CInt(y1), CInt(x2), CInt(y2))
            Next
    
            Dim hrHand As Double = tickSum Mod 43200
            Dim minHand As Double = tickSum Mod 3600
            Dim secHand As Double = tickSum Mod 60
    
            Dim hrPen As New Pen(Color.LightGreen, 12)
            x = 0.7 * radius * Math.Cos((2 * Math.PI * hrHand / 43200) + 1.5 * Math.PI) + formCenter.X
            y = 0.7 * radius * Math.Sin((2 * Math.PI * hrHand / 43200) + 1.5 * Math.PI) + formCenter.Y
            e.Graphics.DrawLine(hrPen, formCenter.X, formCenter.Y, CInt(x), CInt(y))
    
            Dim minPen As New Pen(Color.Blue, 6)
            x = 0.95 * radius * Math.Cos((2 * Math.PI * minHand / 3600) + 1.5 * Math.PI) + formCenter.X
            y = 0.95 * radius * Math.Sin((2 * Math.PI * minHand / 3600) + 1.5 * Math.PI) + formCenter.Y
            e.Graphics.DrawLine(minPen, formCenter.X, formCenter.Y, CInt(x), CInt(y))
    
            Dim secPen As New Pen(Color.Red, 1)
            x = radius * Math.Cos((2 * Math.PI * secHand / 60) + 1.5 * Math.PI) + formCenter.X
            y = radius * Math.Sin((2 * Math.PI * secHand / 60) + 1.5 * Math.PI) + formCenter.Y
            e.Graphics.DrawLine(secPen, formCenter.X, formCenter.Y, CInt(x), CInt(y))
    
        End Sub
    
        Private Sub myTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles myTimer.Tick
    
            Me.Invalidate()
            Dim ticksInHalfADay As Long = 864000000000 \ 2
            tickSum = Math.Round((Now.Ticks Mod ticksInHalfADay) / 10000000, 2)
            Me.Text = "VB.Net clock: " & Now.ToLongTimeString
    
        End Sub
    End Class

    Regards,

    John

    I'm currently looking for work in Vb.Net software development. :-)
    Saturday, December 19, 2009 12:25 AM
  • Start here:

    http://msdn.microsoft.com/en-us/library/aa289159(VS.71).aspx

    Summary: Provides a simple but demonstrative example of how a clock demo can take advantage of the GDI+ features made available by the .NET Framework, as well as sharpen up your Visual Basic .NET skills. (6 printed pages)

    • Proposed as answer by Narayanan Dayalan Wednesday, December 16, 2009 5:49 AM
    • Marked as answer by V.K.S.B.K Saturday, December 19, 2009 4:01 AM
    Wednesday, December 16, 2009 5:33 AM
  • Dear Acamar,

    Thanks for the quick reply.

    Thanks
    VKSBK

    A Real Novice Programmer !
    • Marked as answer by V.K.S.B.K Saturday, December 19, 2009 4:06 AM
    Wednesday, December 16, 2009 5:38 AM
  • Hi again V.K.S.B.K_,

    This version shows the minute positions too.   :-)   >>

    Option Strict On
    
    Public Class Form1
    
        Friend WithEvents myTimer As New Timer
        Private tickSum As Decimal
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Me.WindowState = FormWindowState.Maximized
            'DoubleBuffered = TRUE stops the flickering effect.>>
            Me.DoubleBuffered = True
            ' 100 here is 1 tenth of a second so 50 lines are drawn every 5 seconds
            'and that is only for the second hand, the minute and hour hands are constantly
            'updated too in proportion to the time.>>
            myTimer.Interval = 100
            myTimer.Start()
        End Sub
    
        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    
            'Used to hold the formCenter.>>
            Dim formCenter As Point
            'Set the FORM BackColor to BLACK.>>
            Me.BackColor = Color.Black
            'Get the center of the form.>>
            Dim centreX As Integer = Me.Width \ 2
            Dim centreY As Integer = (Me.Height \ 2) - 10
            'Assign it to the formCenter point.>>
            formCenter = New Point(centreX, centreY)
            'Set the radius to the full half-height-40.>>
            Dim radius As Integer = centreY - 40
    
            'Draw the minute positions.>>
            Dim x, y, x1, y1, x2, y2 As Double
            Dim myPen As New Pen(Color.White, 10)
            myPen = New Pen(Color.White, 5)
            For num As Double = 0 To 2 * Math.PI Step (2 * Math.PI) / 60
                x1 = radius * Math.Cos(num) + formCenter.X
                y1 = radius * Math.Sin(num) + formCenter.Y
                x2 = (radius - 15) * Math.Cos(num) + formCenter.X
                y2 = (radius - 15) * Math.Sin(num) + formCenter.Y
                e.Graphics.DrawLine(myPen, CInt(x1), CInt(y1), CInt(x2), CInt(y2))
            Next
    
            'Draw the 12 hour positions.>>
            myPen = New Pen(Color.Blue, 10)
            For num As Double = 0 To 2 * Math.PI Step (2 * Math.PI) / 12
                x1 = radius * Math.Cos(num) + formCenter.X
                y1 = radius * Math.Sin(num) + formCenter.Y
                x2 = (radius - 40) * Math.Cos(num) + formCenter.X
                y2 = (radius - 40) * Math.Sin(num) + formCenter.Y
                e.Graphics.DrawLine(myPen, CInt(x1), CInt(y1), CInt(x2), CInt(y2))
            Next
    
            Dim hrHand As Double = tickSum Mod 43200
            Dim minHand As Double = tickSum Mod 3600
            Dim secHand As Double = tickSum Mod 60
    
            Dim hrPen As New Pen(Color.LightGreen, 12)
            x = 0.7 * radius * Math.Cos((2 * Math.PI * hrHand / 43200) + 1.5 * Math.PI) + formCenter.X
            y = 0.7 * radius * Math.Sin((2 * Math.PI * hrHand / 43200) + 1.5 * Math.PI) + formCenter.Y
            e.Graphics.DrawLine(hrPen, formCenter.X, formCenter.Y, CInt(x), CInt(y))
    
            Dim minPen As New Pen(Color.Yellow, 6)
            x = 0.95 * radius * Math.Cos((2 * Math.PI * minHand / 3600) + 1.5 * Math.PI) + formCenter.X
            y = 0.95 * radius * Math.Sin((2 * Math.PI * minHand / 3600) + 1.5 * Math.PI) + formCenter.Y
            e.Graphics.DrawLine(minPen, formCenter.X, formCenter.Y, CInt(x), CInt(y))
    
            Dim secPen As New Pen(Color.Red, 1)
            x = radius * Math.Cos((2 * Math.PI * secHand / 60) + (1.5 * Math.PI)) + formCenter.X
            y = radius * Math.Sin((2 * Math.PI * secHand / 60) + (1.5 * Math.PI)) + formCenter.Y
            e.Graphics.DrawLine(secPen, formCenter.X, formCenter.Y, CInt(x), CInt(y))
    
        End Sub
    
        Private Sub myTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles myTimer.Tick
    
            Me.Refresh()
            Dim ticksInHalfADay As Long = 864000000000 \ 2
            tickSum = CDec((Now.Ticks Mod ticksInHalfADay) / 10000000)
            Me.Text = "VB.Net clock: " & Now.ToLongTimeString
            'Me.Text = "Seconds since 12 midnight or 12 noon = " & Math.Round(tickSum, 2).ToString
    
        End Sub
    End Class

    Regards,

    John

    I'm currently looking for work in Vb.Net software development. :-)
    • Marked as answer by V.K.S.B.K Saturday, December 19, 2009 4:01 AM
    Saturday, December 19, 2009 1:00 AM

All replies

  • Start here:

    http://msdn.microsoft.com/en-us/library/aa289159(VS.71).aspx

    Summary: Provides a simple but demonstrative example of how a clock demo can take advantage of the GDI+ features made available by the .NET Framework, as well as sharpen up your Visual Basic .NET skills. (6 printed pages)

    • Proposed as answer by Narayanan Dayalan Wednesday, December 16, 2009 5:49 AM
    • Marked as answer by V.K.S.B.K Saturday, December 19, 2009 4:01 AM
    Wednesday, December 16, 2009 5:33 AM
  • Dear Acamar,

    Thanks for the quick reply.

    Thanks
    VKSBK

    A Real Novice Programmer !
    • Marked as answer by V.K.S.B.K Saturday, December 19, 2009 4:06 AM
    Wednesday, December 16, 2009 5:38 AM

  • can you propose as answered please...
    Don't judge me, just Upgrade me. Thanks!

    This thread was already Proposed As Answer by Narayanan Dayalan Wednesday, December 16, 2009 5:49 AM

    there is different between propose and mark

    kaymaf

    If that what you want, take it. If not, ignored it and no complain
    Friday, December 18, 2009 9:52 PM

  • can you propose as answered please...
    Don't judge me, just Upgrade me. Thanks!

    This thread was already Proposed As Answer by Narayanan Dayalan Wednesday, December 16, 2009 5:49 AM

    there is different between propose and mark

    kaymaf

    If that what you want, take it. If not, ignored it and no complain
    did I ask you something?
    Don't judge me, just Upgrade me. Thanks!



    Im just telling you the different between propose and mark, because this is not the first time i came  accross the same thing when you asked the OP to proposed thread that was already proposed as answer. Just accept the two words are not the same thing.

    kaymaf

    If that what you want, take it. If not, ignored it and no complain
    Friday, December 18, 2009 10:01 PM
  • @ Kaymaf

    Thanks, Sir.....

    If that what you want, take it. If not, ignored it and no complain................you are right..........

    Sorry my speech...............

    Don't judge me, just Upgrade me. Thanks!
    Friday, December 18, 2009 10:03 PM
  • Check this Same thread!

    Check Two Link of  "Sir Kaymaf"  Reply
    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/c90de309-b4d0-41c3-8554-e886464f3481
    Dont Scared With My Silly Example. LOL
    Saturday, December 19, 2009 12:14 AM
  • Hi V.K.S.B.K_,

    Here is my code for an analogue clock taken from this thread.>>

    http://social.msdn.microsoft.com/Forums/en-US/vbpowerpacks/thread/4de7e0e8-6e6e-46a1-96e8-c5a79c61a09a


    Option Strict On
    
    Public Class Form1
    
        Friend WithEvents myTimer As New Timer
        Private tickSum As Double
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Me.DoubleBuffered = True
            Me.WindowState = FormWindowState.Maximized
            ' 100 here is 1 tenth of a second so 50 lines are drawn every 5 seconds
            'and that is only for the second hand, the minute and hour hands are constantly
            'updated too in proportion to the time.
    
            'If you change the interval to 1000 you will get a second
            'hand tick every second instead of a smooth sweeping second hand.>>
            myTimer.Interval = 100
            myTimer.Start()
        End Sub
    
        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    
            'Used to hold the formCenter.>>
            Dim formCenter As Point
            'Set the FORM BackColor to BLACK.>>
            Me.BackColor = Color.Black
            'Get the center of the FORM.>>
            Dim centreX As Integer = Me.Width \ 2
            Dim centreY As Integer = Me.Height \ 2
            'Assign it to the formCenter point.>>
            formCenter = New Point(centreX, centreY)
            'Set the radius to the full half-height-40.>>
            Dim radius As Integer = centreY - 40
    
            'Draw the 12 hour positions.>>
            Dim x, y, x1, y1, x2, y2 As Double
            Dim myPen As New Pen(Color.White, 10)
            For num As Double = 0 To 2 * Math.PI Step (2 * Math.PI) / 12
                x1 = radius * Math.Cos(num) + formCenter.X
                y1 = radius * Math.Sin(num) + formCenter.Y
                x2 = (radius - 10) * Math.Cos(num) + formCenter.X
                y2 = (radius - 10) * Math.Sin(num) + formCenter.Y
                e.Graphics.DrawLine(myPen, CInt(x1), CInt(y1), CInt(x2), CInt(y2))
            Next
    
            Dim hrHand As Double = tickSum Mod 43200
            Dim minHand As Double = tickSum Mod 3600
            Dim secHand As Double = tickSum Mod 60
    
            Dim hrPen As New Pen(Color.LightGreen, 12)
            x = 0.7 * radius * Math.Cos((2 * Math.PI * hrHand / 43200) + 1.5 * Math.PI) + formCenter.X
            y = 0.7 * radius * Math.Sin((2 * Math.PI * hrHand / 43200) + 1.5 * Math.PI) + formCenter.Y
            e.Graphics.DrawLine(hrPen, formCenter.X, formCenter.Y, CInt(x), CInt(y))
    
            Dim minPen As New Pen(Color.Blue, 6)
            x = 0.95 * radius * Math.Cos((2 * Math.PI * minHand / 3600) + 1.5 * Math.PI) + formCenter.X
            y = 0.95 * radius * Math.Sin((2 * Math.PI * minHand / 3600) + 1.5 * Math.PI) + formCenter.Y
            e.Graphics.DrawLine(minPen, formCenter.X, formCenter.Y, CInt(x), CInt(y))
    
            Dim secPen As New Pen(Color.Red, 1)
            x = radius * Math.Cos((2 * Math.PI * secHand / 60) + 1.5 * Math.PI) + formCenter.X
            y = radius * Math.Sin((2 * Math.PI * secHand / 60) + 1.5 * Math.PI) + formCenter.Y
            e.Graphics.DrawLine(secPen, formCenter.X, formCenter.Y, CInt(x), CInt(y))
    
        End Sub
    
        Private Sub myTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles myTimer.Tick
    
            Me.Invalidate()
            Dim ticksInHalfADay As Long = 864000000000 \ 2
            tickSum = Math.Round((Now.Ticks Mod ticksInHalfADay) / 10000000, 2)
            Me.Text = "VB.Net clock: " & Now.ToLongTimeString
    
        End Sub
    End Class

    Regards,

    John

    I'm currently looking for work in Vb.Net software development. :-)
    Saturday, December 19, 2009 12:25 AM
  • Hi again V.K.S.B.K_,

    This version shows the minute positions too.   :-)   >>

    Option Strict On
    
    Public Class Form1
    
        Friend WithEvents myTimer As New Timer
        Private tickSum As Decimal
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Me.WindowState = FormWindowState.Maximized
            'DoubleBuffered = TRUE stops the flickering effect.>>
            Me.DoubleBuffered = True
            ' 100 here is 1 tenth of a second so 50 lines are drawn every 5 seconds
            'and that is only for the second hand, the minute and hour hands are constantly
            'updated too in proportion to the time.>>
            myTimer.Interval = 100
            myTimer.Start()
        End Sub
    
        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    
            'Used to hold the formCenter.>>
            Dim formCenter As Point
            'Set the FORM BackColor to BLACK.>>
            Me.BackColor = Color.Black
            'Get the center of the form.>>
            Dim centreX As Integer = Me.Width \ 2
            Dim centreY As Integer = (Me.Height \ 2) - 10
            'Assign it to the formCenter point.>>
            formCenter = New Point(centreX, centreY)
            'Set the radius to the full half-height-40.>>
            Dim radius As Integer = centreY - 40
    
            'Draw the minute positions.>>
            Dim x, y, x1, y1, x2, y2 As Double
            Dim myPen As New Pen(Color.White, 10)
            myPen = New Pen(Color.White, 5)
            For num As Double = 0 To 2 * Math.PI Step (2 * Math.PI) / 60
                x1 = radius * Math.Cos(num) + formCenter.X
                y1 = radius * Math.Sin(num) + formCenter.Y
                x2 = (radius - 15) * Math.Cos(num) + formCenter.X
                y2 = (radius - 15) * Math.Sin(num) + formCenter.Y
                e.Graphics.DrawLine(myPen, CInt(x1), CInt(y1), CInt(x2), CInt(y2))
            Next
    
            'Draw the 12 hour positions.>>
            myPen = New Pen(Color.Blue, 10)
            For num As Double = 0 To 2 * Math.PI Step (2 * Math.PI) / 12
                x1 = radius * Math.Cos(num) + formCenter.X
                y1 = radius * Math.Sin(num) + formCenter.Y
                x2 = (radius - 40) * Math.Cos(num) + formCenter.X
                y2 = (radius - 40) * Math.Sin(num) + formCenter.Y
                e.Graphics.DrawLine(myPen, CInt(x1), CInt(y1), CInt(x2), CInt(y2))
            Next
    
            Dim hrHand As Double = tickSum Mod 43200
            Dim minHand As Double = tickSum Mod 3600
            Dim secHand As Double = tickSum Mod 60
    
            Dim hrPen As New Pen(Color.LightGreen, 12)
            x = 0.7 * radius * Math.Cos((2 * Math.PI * hrHand / 43200) + 1.5 * Math.PI) + formCenter.X
            y = 0.7 * radius * Math.Sin((2 * Math.PI * hrHand / 43200) + 1.5 * Math.PI) + formCenter.Y
            e.Graphics.DrawLine(hrPen, formCenter.X, formCenter.Y, CInt(x), CInt(y))
    
            Dim minPen As New Pen(Color.Yellow, 6)
            x = 0.95 * radius * Math.Cos((2 * Math.PI * minHand / 3600) + 1.5 * Math.PI) + formCenter.X
            y = 0.95 * radius * Math.Sin((2 * Math.PI * minHand / 3600) + 1.5 * Math.PI) + formCenter.Y
            e.Graphics.DrawLine(minPen, formCenter.X, formCenter.Y, CInt(x), CInt(y))
    
            Dim secPen As New Pen(Color.Red, 1)
            x = radius * Math.Cos((2 * Math.PI * secHand / 60) + (1.5 * Math.PI)) + formCenter.X
            y = radius * Math.Sin((2 * Math.PI * secHand / 60) + (1.5 * Math.PI)) + formCenter.Y
            e.Graphics.DrawLine(secPen, formCenter.X, formCenter.Y, CInt(x), CInt(y))
    
        End Sub
    
        Private Sub myTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles myTimer.Tick
    
            Me.Refresh()
            Dim ticksInHalfADay As Long = 864000000000 \ 2
            tickSum = CDec((Now.Ticks Mod ticksInHalfADay) / 10000000)
            Me.Text = "VB.Net clock: " & Now.ToLongTimeString
            'Me.Text = "Seconds since 12 midnight or 12 noon = " & Math.Round(tickSum, 2).ToString
    
        End Sub
    End Class

    Regards,

    John

    I'm currently looking for work in Vb.Net software development. :-)
    • Marked as answer by V.K.S.B.K Saturday, December 19, 2009 4:01 AM
    Saturday, December 19, 2009 1:00 AM