Я помню, как когда-то писал написание часового пользовательского элемента управления. Вот основной код для выполнения вашего запроса.
Private Sub Paint_Clock(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles Clock.Paint
Dim _independentHands as Boolean = False
Dim g As Graphics = e.Graphics
Dim centrePoint As Point = New Point(Clock.Width \ 2, Clock.Height \ 2)
Dim _time As New TimeSpan(5, 2, 15)
'pens'
Dim hourPen As New Pen(Color.Black, 3)
Dim minPen As New Pen(Color.Black, 2)
Dim secPen As New Pen(Color.Red, 1)
'clock hand lengths'
Dim halfClockWidth As Integer = Clock.Width \ 2
Dim hourLength As Integer = CInt(halfClockWidth * (4 / 6)) - 5
Dim minLength As Integer = CInt(halfClockWidth * (5 / 6)) - 5
Dim secLength As Integer = CInt(halfClockWidth * (5 / 6)) - 5
Dim secLength2 As Integer = CInt(halfClockWidth * (1 / 6))
'angles'
Dim secAngle As Single = CSng(_time.Seconds / 60) * 360
Dim secAngle2 As Single = secAngle - 180
Dim minAngle As Single = CSng(_time.Minutes / 60) * 360
Dim hrAngle As Single = CSng((_time.Hours - 12) / 12) * 360
If Not _independentHands Then minAngle += (secAngle / 60)
If Not _independentHands Then hrAngle += (minAngle / 12)
'centre point'
Dim pointPen As New Pen(Color.Black, 4)
Dim pointRect As New Rectangle(centrePoint.X - 2, centrePoint.Y - 2, 4, 4)
'antialias on'
g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
'draw the background'
g.DrawImage(My.Resources.ClockBack, 0, 0, Clock.Width, Clock.Height)
'draw the hands'
g.DrawLine(hourPen, centrePoint, GetPoint2(centrePoint, hrAngle, hourLength))
g.DrawLine(minPen, centrePoint, GetPoint2(centrePoint, minAngle, minLength))
g.DrawLine(secPen, centrePoint, GetPoint2(centrePoint, secAngle, secLength))
g.DrawLine(secPen, centrePoint, GetPoint2(centrePoint, secAngle2, secLength2))
'draw the centre point'
g.DrawEllipse(pointPen, pointRect)
'draw the glass'
g.DrawImage(My.Resources.ClockGlass, 0, 0, Clock.Width, Clock.Height)
End Sub
Private Function GetPoint2(ByVal startPoint As Point, _
ByVal angle As Single, _
ByVal length As Integer) As Point
Dim x, y As Integer
Dim sp As Point = startPoint
'normalize'
Do While angle - 360 > 0
angle -= 360
Loop
Do While angle < 0
angle += 360
Loop
If angle = 360 Then angle = 0
Dim rad = angle * (Math.PI / 180) 'angle in radians'
'calc the new point'
x = CInt(length * Math.Sin(rad))
y = CInt(length * Math.Cos(rad))
Return New Point(sp.X + x, sp.Y - y)
End Function
Примечания
1. Добавьте PictureBox с именем Clock в вашу форму (или пользовательский контроль)
2. Добавьте ресурс ClockBack для фона ваших часов (циферблата).
3. Добавьте ресурс ClockGlass для гладкой поверхности часов.
4. Оставьте код выше в вашей форме.
5. Установите интервал таймера равным 1000, и у него будет Tick
событие Refresh
или Invalidate
Часы [который является PictureBox].
Обратите внимание, что переменная _independentHands
делает стрелки часов независимыми друг от друга при значении true.
Например, когда установлено значение False
, 4:30 будет иметь часовую стрелку на полпути между 4 и 5. Когда True
, часовая стрелка будет работать на 4 до 4:59:59 и «подпрыгнет» до 5 в 5: 00: 00.
Я предпочитаю оставить это ложным, чтобы дать ощущение естественных часов.