Sunteți pe pagina 1din 5

Analog Clock Tutorial - Visual Basic .

NET
Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

Change both labels enable property to True.

Change the 12 labels text properties to the clock 12 hours. Change also the BackColor property
to white:

Add another label to the form and name it "time"

Let's add the code for the Form Load event:

?
1 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
2 Handles Me.Load
' adjust the clock numbers positions on the screen:
3 Label1.Location = New Point(505, 203)
4 Label2.Location = New Point(561, 261)
5
6
7
8 Label3.Location = New Point(592, 335)
9 Label4.Location = New Point(561, 404)
1 Label5.Location = New Point(505, 464)
0 Label6.Location = New Point(430, 489)
Label7.Location = New Point(357, 463)
11 Label8.Location = New Point(305, 408)
1 Label9.Location = New Point(272, 334)
2 Label10.Location = New Point(302, 259)
1 Label11.Location = New Point(354, 200)
3 Label12.Location = New Point(430, 177)
End Sub
1
4
1
5

Then add the following highlighted declarations to the code:

?
1 Public Class Form1
2 ' tick will be used to draw 60 marks for each second on the clock
3 Dim tick As Integer = 270
4 'tick2 will be used to draw 12 bold marks on each hour on the clock
Dim tick2 As Integer = 270
5

Then add the timer click event code

?
1 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
2 System.EventArgs) Handles Timer1.Tick
'timer1 will draw the hands of the clock
3 'convert seconds to angles
4 Dim seconds As Integer = (Now.Second * 6) + 270
5 'convert minutes to angles
6 Dim minutes As Integer = (Now.Minute * 6) + 270
'convert hours to angles
7 Dim hours As Integer = (Now.Hour * 30) + 270
8 'text label will hold the current time
9 Time.Text = Now
1 'creating graphics
0 Dim g As Graphics
g = Me.CreateGraphics
11 'creating pens
1 Dim hour As New Pen(Color.Blue)
2 Dim hour2 As New Pen(Color.White)
1 Dim second As New Pen(Color.Black)
Dim minute As New Pen(Color.Red)
3 Dim minute2 As New Pen(Color.White)
1 Dim white As New Pen(Color.White)
4 Dim circle As New Pen(Color.Black)
'assigning pens width
1
hour.Width = 8
5 hour2.Width = 10
1 second.Width = 1
6 minute.Width = 4
1 minute2.Width = 4
white.Width = 10
7 circle.Width = 5
1 'drawing the hands of the clock and their locations
8 g.DrawPie(hour2, 319, 219, 240, 240, hours - 30, 360)
1 g.DrawPie(minute2, 289, 189, 300, 300, minutes - 6, 360)
9 g.DrawPie(Pens.White, 269, 169, 340, 340, seconds - 6, 360)
g.DrawPie(hour, 319, 219, 240, 240, hours, 360)
2 g.DrawEllipse(white, 319, 219, 240, 240)
0 g.DrawPie(minute, 289, 189, 300, 300, minutes, 360)
2 g.DrawEllipse(white, 289, 189, 300, 300)
1 g.DrawPie(second, 269, 169, 340, 340, seconds, 360)
g.DrawEllipse(white, 269, 169, 340, 340)
2 'Draw a circle around the clock
2 g.DrawEllipse(circle, 249, 149, 380, 380)
2
3 End Sub
2
4
2
5
2
6
2
7
2
8
2
9
3
0
3
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8
3
9
4
0
4
1
4
2
4
3

Add Timer2 Tick event code:

?
1 Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As
2 System.EventArgs) Handles Timer2.Tick
' timer2 will draw the shape of the clock and the marks
3 tick += 6
4 tick2 += 30
5 Dim g As Graphics
6 Dim hoursMarks As New Pen(Color.Black)
hoursMarks.Width = 5
7
8 g = Me.CreateGraphics
9
1 g.DrawPie(Pens.Black, 249, 149, 380, 380, tick, 360)
0 g.DrawPie(hoursMarks, 249, 149, 380, 380, tick2, 360)
11 g.DrawEllipse(Pens.White, 269, 169, 340, 340)
1 g.FillEllipse(Brushes.White, 269, 169, 340, 340)
If tick > 800 Then
2 'drawing the shape is done and the timer will stop
1 Timer2.Stop()
3 tick = 270
1 tick2 = 270
4 End If
End Sub
1
5
1
6
1
7
1
8
1
9
2
0
2
1

Note: Change the form WindowState property to Maximum to better see the clock working

Download source code

- See more at: http://www.visual-basic-tutorials.com/analog-clock-tutorial-in-visual-


basic.htm#sthash.NitXB3Xb.dpuf

S-ar putea să vă placă și