Here we created Analog Clock using VB.NET coding. This is the mini student project and you can learn some coding techniques in this project. this analog clock works in Visual studio 2008 version or Visual Studio 2012 version. No database required to created this analog clock. This mini project created in single form , 12 labels , 3 shapes, and 2 timers used to develop this project code. Working on this mini project will improve your skill on vb.net and any graphical integrated project is bit interesting concept.
Software Requirements:
- Programming Language: VB.NET
Source code
Today, I’ll show you how to generate and display digital and analogue time. Let’s get started with this tutorial! 1. Begin by creating a Windows Form Application for this tutorial by following the steps below in Microsoft Visual Studio: Navigate to File, New Project, and then Windows Application.
Imports System.Math Imports System.Drawing.Drawing2D Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Public WithEvents tmrTick As System.Windows.Forms.Timer Friend WithEvents ctxPopup As System.Windows.Forms.ContextMenu Friend WithEvents ctxExit As System.Windows.Forms.MenuItem <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.components = New System.ComponentModel.Container Me.tmrTick = New System.Windows.Forms.Timer(Me.components) Me.ctxPopup = New System.Windows.Forms.ContextMenu Me.ctxExit = New System.Windows.Forms.MenuItem ' 'tmrTick ' Me.tmrTick.Enabled = True Me.tmrTick.Interval = 1000 ' 'ctxPopup ' Me.ctxPopup.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.ctxExit}) ' 'ctxExit ' Me.ctxExit.Index = 0 Me.ctxExit.Shortcut = System.Windows.Forms.Shortcut.AltF4 Me.ctxExit.Text = "E&xit" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(8, 19) Me.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(255, Byte), CType(255, Byte)) Me.ClientSize = New System.Drawing.Size(104, 160) Me.ContextMenu = Me.ctxPopup Me.Font = New System.Drawing.Font("Times New Roman", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None Me.Name = "Form1" Me.ShowInTaskbar = False Me.Text = "Form1" End Sub #End Region Private m_Face As Bitmap Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Draw the face without the hands. DrawFace() ' Put the form in the lower right corner. Dim working_rect As Rectangle = Screen.GetWorkingArea(New Point(0, 0)) Me.Left = working_rect.Right - Me.Width Me.Top = working_rect.Bottom - Me.Height End Sub ' Draw the clock's face without hands. Private Sub DrawFace() ' Make a Bitmap to hold the clock face. m_Face = New Bitmap(Me.ClientRectangle.Width, Me.ClientRectangle.Height) Dim gr As Graphics = Graphics.FromImage(m_Face) ' Use a purple background. This will later be transparent. gr.Clear(Color.Purple) ' Fill the clock face with CornflowerBlue. Dim inner_rect As New Rectangle(0, 0, _ Me.ClientRectangle.Width - 1, _ Me.ClientRectangle.Height - 1) gr.FillEllipse(Brushes.CornflowerBlue, inner_rect) ' Draw the clock face. gr.DrawEllipse(Pens.Blue, inner_rect) ' Draw the tic marks and numerals. Dim cx As Integer = (Me.ClientRectangle.Width - 1) \ 2 Dim cy As Integer = (Me.ClientRectangle.Height - 1) \ 2 Dim dtheta As Double = PI / 30 Dim theta As Double = -10 * dtheta Dim x1, y1, x2, y2 As Double Dim txt As String For i As Integer = 0 To 59 ' Draw the tic marks. x1 = cx + cx * Cos(theta) y1 = cy + cy * Sin(theta) If i Mod 5 = 0 Then ' Label the digit. txt = (i \ 5 + 1).ToString() ' Find the point lined up along the tic mark. x2 = cx + (cx - 1) * Cos(theta) * 0.8 y2 = cy + (cy - 1) * Sin(theta) * 0.8 ' Create a rotated font. DrawRotatedText(gr, txt, _ CSng(360 * (i + 5) / 60), _ x2, y2) x2 = cx + cx * Cos(theta) * 0.9 y2 = cy + cy * Sin(theta) * 0.9 Else x2 = cx + cx * Cos(theta) * 0.95 y2 = cy + cy * Sin(theta) * 0.95 End If gr.DrawLine(Pens.Blue, CSng(x1), CSng(y1), CSng(x2), CSng(y2)) theta += dtheta Next i ' Display the clock face on the form's background. Me.BackgroundImage = m_Face ' Set TransparencyKey so the purple background is transparent. Me.TransparencyKey = Color.Purple End Sub ' Draw text centered at (cx, cy) and ' rotated by angle degrees. Private Sub DrawRotatedText(ByVal gr As Graphics, ByVal txt As String, ByVal angle As Single, ByVal cx As Double, ByVal cy As Double) ' Make a StringFormat that centers text. Dim string_format As New StringFormat string_format.Alignment = StringAlignment.Center string_format.LineAlignment = StringAlignment.Center ' Make a GraphicsPath that draws the text. Dim graphics_path As New GraphicsPath( _ Drawing.Drawing2D.FillMode.Winding) Dim ix As Integer = CInt(cx) Dim iy As Integer = CInt(cy) graphics_path.AddString(txt, _ Me.Font.FontFamily, _ Me.Font.Style, Me.Font.Size, _ New Point(ix, iy), _ string_format) ' Make a rotation matrix representing ' rotation around the point (ix, iy). Dim rotation_matrix As New Matrix rotation_matrix.RotateAt(angle, New PointF(ix, iy)) ' Transform the GraphicsPath. graphics_path.Transform(rotation_matrix) ' Draw the text. gr.FillPath(Brushes.Black, graphics_path) End Sub ' Draw the clock's face with hands. Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint Const HOUR_R As Double = 0.3 Const MIN_R As Double = 0.5 Const SEC_R As Double = 0.75 If m_Face Is Nothing Then Exit Sub ' Copy the face onto the bitmap. e.Graphics.DrawImage(m_Face, 0, 0) ' Draw the hands. Dim cx As Double = Me.ClientRectangle.Width / 2 Dim cy As Double = Me.ClientRectangle.Height / 2 Dim x1, y1, theta As Double ' Draw the hour hand. Dim time_span As TimeSpan = Now.TimeOfDay() theta = time_span.TotalHours() / 12 * 2 * PI - PI / 2 x1 = cx + cx * Cos(theta) * HOUR_R y1 = cy + cy * Sin(theta) * HOUR_R e.Graphics.DrawLine(Pens.Red, _ CSng(cx), CSng(cy), CSng(x1), CSng(y1)) ' Draw the minute hand. theta = time_span.TotalMinutes() / 60 * 2 * PI - PI / 2 x1 = cx + cx * Cos(theta) * MIN_R y1 = cy + cy * Sin(theta) * MIN_R e.Graphics.DrawLine(Pens.Blue, _ CSng(cx), CSng(cy), CSng(x1), CSng(y1)) ' Draw the second hand. theta = time_span.TotalSeconds() / 60 * 2 * PI - PI / 2 x1 = cx + cx * Cos(theta) * MIN_R y1 = cy + cy * Sin(theta) * MIN_R e.Graphics.DrawLine(Pens.White, _ CSng(cx), CSng(cy), CSng(x1), CSng(y1)) ' Draw a circle in the middle. e.Graphics.FillEllipse(Brushes.Black, _ CSng(cx - 3), CSng(cy - 3), 5, 5) End Sub ' Close the application's form. Private Sub ctxExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ctxExit.Click Me.Close() End Sub ' Invalidate to cause a Paint. Private Sub tmrTick_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrTick.Tick Me.Invalidate() End Sub End Class
Note: This mini student project is for diploma, BCA and ITI students. This download link has source code of this project and it has exe file of this project. Kindly install winrar to extract this project code.That’s the only thing there is to it. You’ll then be able to easily add multiple clocks to your project and manipulate them however you like. Colorize the hands and dynamically adjust the sizing and timing based on your own metrics. It can be used for stop watches, dashboards, code timing (with a higher resolution timer and, ideally, a different thread), and so on. This project is ideal for those who want to learn how to draw on panels using a double buffering panel, timers, and a little geometry. Here’s a quick look at what it looks like in action. I hope you enjoyed it, and thank you for taking the time to read it.
Leave a Reply