โค้ดไม่ยากขอไม่อธิบายให้ยืดยาว โหลดไปดูกันเองละกันครับ
โปรเจ็กนี้เขียนด้วย Visual Studio 2013 และ .Net 4.0
โค้ดโปรแกรม
Public Class Form1
Dim Minute1 As Bitmap
Dim Second1 As Bitmap
Dim HourP As Bitmap
Dim Height1, Width1 As Integer
Friend WithEvents Timer1 As New System.Windows.Forms.Timer
Friend WithEvents PictureBox1 As New System.Windows.Forms.PictureBox
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Size = New Size(316, 338)
Me.Timer1.Interval = 1000
Me.Timer1.Enabled = True
Me.PictureBox1.BackColor = System.Drawing.SystemColors.ActiveCaptionText
Me.PictureBox1.Location = New System.Drawing.Point(-1, 0)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(300, 300)
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False
Me.PictureBox1.BackColor = Color.White
Me.Controls.Add(PictureBox1)
Me.Label1.BackColor = Color.Black
Me.Label1.ForeColor = Color.Yellow
Me.Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) _
Handles Timer1.Tick
Height1 = PictureBox1.Size.Height
Width1 = PictureBox1.Size.Width
Dim TM As Bitmap = New Bitmap(Height1, Width1)
Dim G As Graphics = Graphics.FromImage(TM)
PictureBox1.Controls.Clear()
Dim x1 As Integer = Height1 / 2
Dim y1 As Integer = Width1 / 2
G.TranslateTransform(x1, y1)
'หน้าปัด
Dim PenBlack As New Pen(Brushes.Black, 5)
Dim PenGreen As New Pen(Brushes.Green, 4)
For i As Integer = 1 To 60
G.ResetTransform()
G.TranslateTransform(x1, y1)
G.RotateTransform(i * 6)
G.DrawLine(PenGreen, 0, -131, 0, -135)
Next
For i As Integer = 1 To 12
G.ResetTransform()
G.TranslateTransform(x1, y1)
G.RotateTransform(i * 30)
G.DrawLine(PenBlack, 0, -115, 0, -145)
Next
Dim HPen As New Pen(Color.Blue, 20) 'เข็มสั้น
G.ResetTransform()
G.TranslateTransform(x1, y1)
G.RotateTransform(((Now.Hour * 30) + (Now.Minute * 0.5)))
G.DrawLine(HPen, 0, 20, 0, -100)
Dim MinPen As New Pen(Color.Orange, 10) 'เข็มยาว
G.ResetTransform()
G.TranslateTransform(x1, y1)
G.RotateTransform((((Now.Minute * 60) + Now.Second) * 0.1))
G.DrawLine(MinPen, 0, 20, 0, -140)
Dim SecPen As New Pen(Color.Red, 4) 'เข็มวินาที
G.ResetTransform()
G.TranslateTransform(x1, y1)
G.RotateTransform((Now.Second * 6))
G.DrawLine(SecPen, 0, 20, 0, -138)
Dim PointPen As New Pen(Color.Black, 2) 'หมุด
Dim PointPenRed As New Pen(Color.Red, 12)
G.ResetTransform()
G.TranslateTransform(x1, y1)
G.DrawEllipse(PointPenRed, -2, -2, 4, 4)
G.DrawEllipse(PointPen, -2, -2, 4, 4)
PictureBox1.Image = TM
Label1.Text = Now.Hour & "." & Now.Minute & "." & Now.Second
End Sub
End Class
ดาว์นโหลดโปรเจ็ก
ในบางครั้งเข็มมันก็ไม่จำเป็นต้องตรงเสมอไป เปลี่ยนจาก DrawLine เป็น DrawCurve แทนซะก็สวยไปอีกแบบ