Results 1 to 4 of 4

Thread: Analog Clock in VB.NET: Resizable, Smooth, and System-Synced

  1. #1

    Thread Starter
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    563

    Analog Clock in VB.NET: Resizable, Smooth, and System-Synced

    This is an old project of mine I coded in Visual Basic 2010, framework 4, but it should work with newer versions of Visual Basic. It stays in sync with a system's time when resized. It's graphics are anti-aliased for smoothness.

    ___

    What you'll need for this project is a Timer control and a PictureBox docked as fill. You also need to create a class called "Clock".

    Form1:

    Code:
    Imports System.Drawing
    
    ' Analog resizable clock that stays in sync with system clock.
    
    Public Class Form1
    
         Private _clock As New Clock() ' Instance of the Clock class
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            Me.DoubleBuffered = True ' Enable double buffering for smoother rendering
            Me.TopMost = True ' Keep the clock always on top
            Timer1.Interval = 1000 ' Start with 1-second interval
            Timer1.Enabled = True
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
            Timer1.Interval = 1000 - DateTime.Now.Millisecond ' Sync Timer with the system clock
            PictureBox1.Invalidate() ' Redraws the clock every second
        End Sub
    
        Protected Overrides Sub OnResize(ByVal e As EventArgs)
            MyBase.OnResize(e)
            PictureBox1.Invalidate() ' Redraws the clock when the form is resized
        End Sub
    
        Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles PictureBox1.Paint
            Dim g As Graphics = e.Graphics
            g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias ' Enable anti-aliasing for smooth lines
    
            ' Calculate the clock's size and center
            Dim width As Integer = PictureBox1.ClientSize.Width
            Dim height As Integer = PictureBox1.ClientSize.Height
            Dim clockRadius As Single = Math.Min(width, height) / 2 - 20 ' Adjust for padding
            Dim center As New PointF(width / 2, height / 2)
    
            _clock.DrawClockFace(g, center, clockRadius) ' Draw the clock face
            _clock.DrawHands(g, center, clockRadius, DateTime.Now) ' Draw the clock hands
        End Sub
    
    End Class

    Clock:

    Code:
    Imports System.Drawing
    
    ' Class to handle drawing of the clock face and hands
    
    Public Class Clock
    
        ' Draws the clock face with tick marks
        Public Sub DrawClockFace(ByVal g As Graphics, ByVal center As PointF, ByVal radius As Single)
            For i As Integer = 0 To 59
                Dim angle As Double = Math.PI * 2 * i / 60 ' Calculate angle for each tick
    
                ' Outer point of the tick mark
                Dim outerX As Single = center.X + CSng(Math.Cos(angle) * radius)
                Dim outerY As Single = center.Y + CSng(Math.Sin(angle) * radius)
    
                ' Inner point of the tick mark
                Dim innerRadius As Single = If(i Mod 5 = 0, radius - (radius * 0.1F),
                                               radius - (radius * 0.05F))
                Dim innerX As Single = center.X + CSng(Math.Cos(angle) * innerRadius)
                Dim innerY As Single = center.Y + CSng(Math.Sin(angle) * innerRadius)
    
                ' Draw the tick mark
                Using pen As New Pen(Color.Black, If(i Mod 5 = 0, 2, 1))
                    g.DrawLine(pen, New PointF(outerX, outerY), New PointF(innerX, innerY))
                End Using
            Next
        End Sub
    
        ' Draws the clock hands based on the current time
        Public Sub DrawHands(ByVal g As Graphics, ByVal center As PointF, ByVal radius As Single, ByVal time As DateTime)
            Dim secondHandLength As Single = radius * 0.9F ' Length of the second hand
            Dim minuteHandLength As Single = radius * 0.9F ' Length of the minute hand
            Dim hourHandLength As Single = radius * 0.7F ' Length of the hour hand
    
            DrawHand(g, center, secondHandLength,
                     time.Second / 60.0, Pens.Red) ' Draw second hand
    
            DrawHand(g, center, minuteHandLength,
                     (time.Minute + time.Second / 60.0
                      ) / 60.0, New Pen(Color.Black, 1.6)) ' Draw minute hand
    
            DrawHand(g, center, hourHandLength,
                     (time.Hour Mod 12 + time.Minute / 60.0
                      ) / 12.0, New Pen(Color.Black, 2)) ' Draw hour hand
        End Sub
    
        ' Draws a single clock hand
        Private Sub DrawHand(ByVal g As Graphics, ByVal center As PointF, ByVal length As Single, ByVal fraction As Double, ByVal pen As Pen)
            Dim angle As Double = Math.PI * 2 * fraction - Math.PI / 2 ' Calculate the angle of the hand
            Dim x As Single = center.X + CSng(Math.Cos(angle) * length)
            Dim y As Single = center.Y + CSng(Math.Sin(angle) * length)
            g.DrawLine(pen, center, New PointF(x, y)) ' Draw the hand
        End Sub
    
    End Class
    Last edited by Peter Porter; Dec 22nd, 2024 at 08:52 PM.

  2. #2
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    812

    Re: Analog Clock in VB.NET: Resizable, Smooth, and System-Synced

    It's a nice little clock thanks for shareing. now do a little date one and you chould make a small widgets pack for windows.
    Good work.

  3. #3

    Thread Starter
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    563

    Re: Analog Clock in VB.NET: Resizable, Smooth, and System-Synced

    Quote Originally Posted by BenJones View Post
    It's a nice little clock thanks for shareing. now do a little date one and you chould make a small widgets pack for windows.
    Good work.
    Thanks!

    This clock is gonna be added to an unusual widget I'm working on. I'll provide a link to it as soon as it's all pieced together.

  4. #4
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    812

    Re: Analog Clock in VB.NET: Resizable, Smooth, and System-Synced

    Quote Originally Posted by Peter Porter View Post
    Thanks!

    This clock is gonna be added to an unusual widget I'm working on. I'll provide a link to it as soon as it's all pieced together.
    Be good a wether one to or a news one.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width