' Analog Clock ' Downloaded from www.publicjoe.co.uk ' ' This software is provided 'as-is', without any express or implied warranty. ' In no event will the author(s) be held liable for any damages arising from ' the use of this software. ' ' Permission is granted to anyone to use this software for any purpose, ' including commercial applications, and to alter it and redistribute it ' freely. Imports System Imports System.Drawing Imports System.Windows.Forms Imports System.Drawing.Drawing2D Imports System.Runtime.InteropServices Public Class AnalogClock : Inherits Form Private components As System.ComponentModel.IContainer Private myTimer As New Timer() Public Sub New() MyBase.New() InitializeComponent() ' Enable Double Buffering to remove flicker Me.SetStyle(ControlStyles.AllPaintingInWmPaint, true) Me.SetStyle(ControlStyles.UserPaint, true) Me.SetStyle(ControlStyles.DoubleBuffer, true) 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 Private Sub InitializeComponent() ' ' myTimer ' ' Adds the event and the event handler for the method that will ' process the timer event to the timer. AddHandler myTimer.Tick, AddressOf TimerEventProcessor myTimer.Enabled = True myTimer.Interval = 1000 myTimer.Start() ' 'Form1 ' Me.FormBorderStyle = FormBorderStyle.FixedSingle Me.MaximizeBox = false Me.Name = "AnalogClock" Me.Text = "Analog Clock" Me.ClientSize = New Size( 240, 240 ) Me.Font = New Font("Arial", 12F, FontStyle.Regular, GraphicsUnit.Point, 0) End Sub 'Run the application Public Shared Sub Main() Application.Run(New AnalogClock( )) End Sub ' This is the method to run when the timer is raised. Private Sub TimerEventProcessor(myObject As Object, myEventArgs As EventArgs) ' Redraw the clock Me.Invalidate() End Sub Private Sub AnalogClock_Paint( ByVal sender As Object, _ ByVal e As PaintEventArgs) _ Handles MyBase.Paint ' Get Graphics Object Dim g As Graphics = e.Graphics ' Create Rectangle To Limit brush area. Dim rect As New Rectangle(20, 20, 230, 230) ' Create Brush Dim linearBrush As New LinearGradientBrush( _ rect, _ Color.FromArgb(0, 0, 0), _ Color.FromArgb(120, 120, 255), _ 225 ) ' Draw Outer Rim to screen. g.FillEllipse( linearBrush, 20, 20, 200, 200 ) linearBrush.LinearColors = New Color() { Color.FromArgb(120, 120, 225), Color.FromArgb(0, 0, 0) } ' Draw Inner Rim to screen. g.FillEllipse( linearBrush, 30, 30, 180, 180 ) linearBrush.LinearColors = New Color() { Color.FromArgb(0, 0, 0), Color.FromArgb(120, 120, 255) } ' Draw face to screen. g.FillEllipse( linearBrush, 33, 33, 174, 174 ) ' Create Brush Dim numeralBrush As New SolidBrush(Color.White) ' Create Font Dim textFont As New Font( "Arial Bold", 12F ) ' g.DrawEllipse( New Pen(Color.White, 1), 40, 40, 160, 160 ) ' g.DrawEllipse( New Pen(Color.White, 1), 60, 60, 120, 120 ) ' Draw Numerals g.DrawString( "12", textFont, numeralBrush, 109, 40 ) g.DrawString( "11", textFont, numeralBrush, 75, 50 ) g.DrawString( "10", textFont, numeralBrush, 47, 75 ) g.DrawString( "9", textFont, numeralBrush, 43, 110 ) g.DrawString( "8", textFont, numeralBrush, 52, 145 ) g.DrawString( "7", textFont, numeralBrush, 75, 170 ) g.DrawString( "6", textFont, numeralBrush, 113, 180 ) g.DrawString( "5", textFont, numeralBrush, 150, 170 ) g.DrawString( "4", textFont, numeralBrush, 173, 145 ) g.DrawString( "3", textFont, numeralBrush, 182, 110 ) g.DrawString( "2", textFont, numeralBrush, 173, 75 ) g.DrawString( "1", textFont, numeralBrush, 150, 50 ) ' In order to draw the hands, we need to Translate to the center of the clock. g.TranslateTransform(120, 120, MatrixOrder.Append) ' Get the current time Dim hour As Integer = DateTime.Now.Hour Dim min As Integer = DateTime.Now.Minute Dim sec As Integer = DateTime.Now.Second ' Create Brushes and Pens Dim hourBrush As New SolidBrush(Color.White) Dim minuteBrush As New SolidBrush(Color.LightGray) Dim secondPen As New Pen(Color.Red, 1) ' Create angles Dim secondAngle As Single = 2.0 * Math.PI * sec/60.0 Dim minuteAngle As Single = 2.0 * Math.PI * ( min + sec/60.0 )/60.0 Dim hourAngle As Single = 2.0 * Math.PI * ( hour + min/60.0)/12.0 ' Set centre point Dim centre As New Point(0, 0) ' Draw Hour Hand Dim gpHour As New GraphicsPath() Dim HourArrow As Point() = { _ New Point( Convert.ToInt32(40 * Math.Sin(hourAngle)), _ Convert.ToInt32(-40 * Math.Cos(hourAngle)) ), _ New Point( Convert.ToInt32(-5 * Math.Cos(hourAngle)), _ Convert.ToInt32(-5 * Math.Sin(hourAngle)) ), _ New Point( Convert.ToInt32(5 * Math.Cos(hourAngle)), _ Convert.ToInt32(5 * Math.Sin(hourAngle)) ), _ New Point( Convert.ToInt32(40 * Math.Sin(hourAngle)), _ Convert.ToInt32(-40 * Math.Cos(hourAngle))) } gpHour.AddPolygon(HourArrow) g.FillPath( hourBrush, gpHour ) g.FillEllipse( hourBrush, -5, -5, 10, 10 ) ' Draw Minute Hand Dim gpMinute As New GraphicsPath() Dim MinuteArrow As Point() = { _ New Point( Convert.ToInt32(70 * Math.Sin(minuteAngle)), _ Convert.ToInt32(-70 * Math.Cos(minuteAngle)) ), _ New Point( Convert.ToInt32(-5 * Math.Cos(minuteAngle)), _ Convert.ToInt32(-5 * Math.Sin(minuteAngle)) ), _ New Point( Convert.ToInt32(5 * Math.Cos(minuteAngle)), _ Convert.ToInt32(5 * Math.Sin(minuteAngle)) ), _ New Point( Convert.ToInt32(70 * Math.Sin(minuteAngle)), _ Convert.ToInt32(-70 * Math.Cos(minuteAngle))) } gpMinute.AddPolygon(MinuteArrow) g.FillPath( minuteBrush, gpMinute ) g.FillEllipse( minuteBrush, -5, -5, 10, 10 ) ' Draw Second Hand Dim secHand As New Point( Convert.ToInt32( 70 * Math.Sin(secondAngle) ), _ Convert.ToInt32( -70 * Math.Cos(secondAngle) ) ) g.DrawLine(secondPen, centre, secHand) ' Now tidy up linearBrush.Dispose() numeralBrush.Dispose() textFont.Dispose() hourBrush.Dispose() minuteBrush.Dispose() secondPen.Dispose() End Sub End Class