Hej,
Mam wykonać zegar analogowy w Visual Basic'u. Udało mi się narysować tarczę, cyfry i wskazówkę sekund. Jednak zegar jest "zamrozony". Co trzeba zrobić by wskazówka była rysowana co sekundę? Domyślam się, że trzeba użyć timera ale nie w jaki sposób.
Oto cały kod:
Imports System.Drawing.Drawing2D
Public Class Form1
Dim d As DateTime
Dim g As Graphics
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
g = e.Graphics
DrawClock(g)
DrawNumbers(g)
DrawHands(g)
End Sub
//rysuje tarcze - dziala
Private Sub DrawClock(ByVal g As Graphics)
Dim b As Brush
b = Brushes.GhostWhite
g.FillEllipse(b, 10, 10, Me.ClientRectangle.Width - 20, Me.ClientRectangle.Height - 20)
Dim p As Pen = New Pen(Color.Black)
p.Width = 4
g.DrawEllipse(p, 10, 10, Me.ClientRectangle.Width - 20, Me.ClientRectangle.Height - 20)
Dim midX As Integer = ClientRectangle.Width / 2
Dim midY As Integer = ClientRectangle.Height / 2
g.FillEllipse(Brushes.Black, midX - 5, midY - 5, 10, 10)
End Sub
//rysuje cyfry - dziala
Private Sub DrawNumbers(ByVal g As Graphics)
Dim angle As Double = -60.0F
For n As Integer = 1 To 12
Dim midX As Integer = ClientRectangle.Width / 2
Dim midY As Integer = ClientRectangle.Height / 2
Dim ww As Double = (ClientRectangle.Width - 50) / 2.0F
Dim hh As Double = (ClientRectangle.Height - 50) / 2.0F
Dim x As Double = ww * Math.Cos(angle * Math.PI / 180.0F)
x += midX
Dim y As Double = hh * Math.Sin(angle * Math.PI / 180.0F)
y += midY
Dim s As String = Convert.ToString(n)
Dim ss As SizeF = g.MeasureString(s, Me.Font)
Dim sc As Integer = Int(ss.Width / 2)
Dim sh As Integer = Int(ss.Height / 2) - 1
g.DrawString(s, Me.Font, Brushes.Black, (x - sc), (y - sh), New StringFormat())
angle += 30
Next
End Sub
//rysuje wskazowke sekund - dziala
Private Sub DrawHands(ByVal g As Graphics)
Dim midX, midY As Integer
midX = (ClientRectangle.Left + ClientRectangle.Right) / 2
midY = (ClientRectangle.Top + ClientRectangle.Bottom) / 2
Dim angle As Double = d.Second * 6.0
Dim mTransform As Matrix = New Matrix()
mTransform.RotateAt(angle, New PointF(midX, midY))
Dim ps As Point = New Point(midX, 15)
Dim gps As GraphicsPath = New GraphicsPath()
gps.AddLine(midX, midY, ps.X, ps.Y)
gps.Transform(mTransform)
g.DrawPath(Pens.Red, gps)
End Sub
//timer???
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
End Sub
End Class