Exercice Visual Basic : Horloge

Ecrire le code VB qui permet de réaliser li'interface suivante
Le Principe c'est de créer une Horloge.



123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383Imports SystemImports System.Windows.FormsImports System.DrawingImports System.ComponentModelNamespace Composant Public Class Horloge12 Inherits Control Private t As New Timer() Private fAiguilleSec As Boolean = True '-------------------------------------------------------- Public Sub New() 'constructeur SetStyle(ControlStyles.DoubleBuffer Or ControlStyles.UserPaint Or _ ControlStyles.AllPaintingInWmPaint, True) AddHandler t.Tick, AddressOf Tempo t.Interval = 1000 t.Start() End Sub 'New '-------------------------------------------------------- Protected Overrides ReadOnly Property DefaultSize() As Size Get Return New Size(121, 121) End Get End Property '-------------------------------------------------------- Private Sub Tempo(ByVal myObject As [Object], ByVal myEventArgs As EventArgs) Invalidate() End Sub '-------------------------------------------------------- ("Appearance"), Description("Affiche l'aiguille des secondes."), _ DefaultValue(True)> _ Public Property AiguilleSec() As Boolean Get Return fAiguilleSec End Get Set(ByVal value As Boolean) fAiguilleSec = value Invalidate() End Set End Property '-------------------------------------------------------- Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim blackPen As New Pen(Color.Black, 1) Dim rect As New Rectangle(2, 2, 118, 118) e.Graphics.DrawEllipse(blackPen, rect) Dim d As DateTime = DateTime.Now Dim n, z, u, x0, y0, x1, y1, x2, y2, x3, y3 As Single Dim aigPoints(3) As PointF '----------------------- ' Aiguille des Secondes (si propriété AiguilleSec à true) If AiguilleSec Then n = d.Second * 200 / 60 z = n / 100 * 3.14159F u = (n + 50) / 100 * 3.14159F x0 = CSng(Math.Sin(z)) * 50 y0 = CSng(-Math.Cos(z)) * 50 x1 = CSng(-Math.Sin(z)) * 10 y1 = CSng(Math.Cos(z)) * 10 x2 = CSng(Math.Sin(u)) * 2 y2 = CSng(-Math.Cos(u)) * 2 x3 = CSng(-Math.Sin(u)) * 2 y3 = CSng(Math.Cos(u)) * 2 Dim redBrush As New SolidBrush(Color.Red) aigPoints(0).X = x1 + 60 aigPoints(0).Y = y1 + 60 aigPoints(1).X = x2 + 60 aigPoints(1).Y = y2 + 60 aigPoints(2).X = x0 + 60 aigPoints(2).Y = y0 + 60 aigPoints(3).X = x3 + 60 aigPoints(3).Y = y3 + 60 e.Graphics.FillPolygon(redBrush, aigPoints) e.Graphics.DrawPolygon(blackPen, aigPoints) redBrush.Dispose() End If '----------------------- ' Aiguille des Minutes n = d.Minute * 200 / 60 z = n / 100 * 3.14159F u = (n + 50) / 100 * 3.14159F x0 = CSng(Math.Sin(z)) * 50 y0 = CSng(-Math.Cos(z)) * 50 x1 = CSng(-Math.Sin(z)) * 10 y1 = CSng(Math.Cos(z)) * 10 x2 = CSng(Math.Sin(u)) * 4 y2 = CSng(-Math.Cos(u)) * 4 x3 = CSng(-Math.Sin(u)) * 4 y3 = CSng(Math.Cos(u)) * 4 Dim limeBrush As New SolidBrush(Color.Lime) aigPoints(0).X = x1 + 60 aigPoints(0).Y = y1 + 60 aigPoints(1).X = x2 + 60 aigPoints(1).Y = y2 + 60 aigPoints(2).X = x0 + 60 aigPoints(2).Y = y0 + 60 aigPoints(3).X = x3 + 60 aigPoints(3).Y = y3 + 60 e.Graphics.FillPolygon(limeBrush, aigPoints) e.Graphics.DrawPolygon(blackPen, aigPoints) limeBrush.Dispose() '----------------------- ' Aiguille des Heures n = d.Hour * 200 / 12 + d.Minute * 200 / 60 / 12 z = n / 100 * 3.14159F u = (n + 50) / 100 * 3.14159F x0 = CSng(Math.Sin(z)) * 35 y0 = CSng(-Math.Cos(z)) * 35 x1 = CSng(-Math.Sin(z)) * 10 y1 = CSng(Math.Cos(z)) * 10 x2 = CSng(Math.Sin(u)) * 4 y2 = CSng(-Math.Cos(u)) * 4 x3 = CSng(-Math.Sin(u)) * 4 y3 = CSng(Math.Cos(u)) * 4 Dim yellowBrush As New SolidBrush(Color.Yellow) aigPoints(0).X = x1 + 60 aigPoints(0).Y = y1 + 60 aigPoints(1).X = x2 + 60 aigPoints(1).Y = y2 + 60 aigPoints(2).X = x0 + 60 aigPoints(2).Y = y0 + 60 aigPoints(3).X = x3 + 60 aigPoints(3).Y = y3 + 60 e.Graphics.FillPolygon(yellowBrush, aigPoints) e.Graphics.DrawPolygon(blackPen, aigPoints) yellowBrush.Dispose() '----------------------- ' Chiffres Dim drawFont As New Font("Arial", 8) Dim drawBrush As New SolidBrush(Color.Black) e.Graphics.DrawString("12", drawFont, drawBrush, 55, 6) e.Graphics.DrawString("6", drawFont, drawBrush, 58, 101) e.Graphics.DrawString("3", drawFont, drawBrush, 105, 53) e.Graphics.DrawString("9", drawFont, drawBrush, 8, 53) '----------------------- drawFont.Dispose() drawBrush.Dispose() blackPen.Dispose() MyBase.OnPaint(e) End Sub 'OnPaint '-------------------------------------------------------- Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing And Not (t Is Nothing) Then t.Dispose() t = Nothing End If MyBase.Dispose(disposing) End Sub 'Dispose End Class 'HorlogeEnd Namespace 'Composant**********************************************************************************************Imports SystemImports System.Windows.FormsImports System.DrawingImports System.ComponentModelNamespace Composant Public Class Horloge123 Inherits Control Private t As New Timer() Private fAiguilleSec As Boolean = True '-------------------------------------------------------- Public Sub New() 'constructeur SetStyle(ControlStyles.DoubleBuffer Or ControlStyles.UserPaint Or _ ControlStyles.AllPaintingInWmPaint, True) AddHandler t.Tick, AddressOf Tempo t.Interval = 1000 t.Start() End Sub 'New '-------------------------------------------------------- Protected Overrides ReadOnly Property DefaultSize() As Size Get Return New Size(121, 121) End Get End Property '-------------------------------------------------------- Private Sub Tempo(ByVal myObject As [Object], ByVal myEventArgs As EventArgs) Invalidate() End Sub '-------------------------------------------------------- ("Appearance"), Description("Affiche l'aiguille des secondes."), _ DefaultValue(True)> _ Public Property AiguilleSec() As Boolean Get Return fAiguilleSec End Get Set(ByVal value As Boolean) fAiguilleSec = value Invalidate() End Set End Property '-------------------------------------------------------- Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim blackPen As New Pen(Color.Black, 1) Dim rect As New Rectangle(2, 2, 118, 118) e.Graphics.DrawEllipse(blackPen, rect) Dim d As DateTime = DateTime.Now Dim n, z, u, x0, y0, x1, y1, x2, y2, x3, y3 As Single Dim aigPoints(3) As PointF '----------------------- ' Aiguille des Secondes (si propriété AiguilleSec à true) If AiguilleSec Then n = d.Second * 200 / 60 z = n / 100 * 3.14159F u = (n + 50) / 100 * 3.14159F x0 = CSng(Math.Sin(z)) * 50 y0 = CSng(-Math.Cos(z)) * 50 x1 = CSng(-Math.Sin(z)) * 10 y1 = CSng(Math.Cos(z)) * 10 x2 = CSng(Math.Sin(u)) * 2 y2 = CSng(-Math.Cos(u)) * 2 x3 = CSng(-Math.Sin(u)) * 2 y3 = CSng(Math.Cos(u)) * 2 Dim redBrush As New SolidBrush(Color.Red) aigPoints(0).X = x1 + 60 aigPoints(0).Y = y1 + 60 aigPoints(1).X = x2 + 60 aigPoints(1).Y = y2 + 60 aigPoints(2).X = x0 + 60 aigPoints(2).Y = y0 + 60 aigPoints(3).X = x3 + 60 aigPoints(3).Y = y3 + 60 e.Graphics.FillPolygon(redBrush, aigPoints) e.Graphics.DrawPolygon(blackPen, aigPoints) redBrush.Dispose() End If '----------------------- ' Aiguille des Minutes n = d.Minute * 200 / 60 z = n / 100 * 3.14159F u = (n + 50) / 100 * 3.14159F x0 = CSng(Math.Sin(z)) * 50 y0 = CSng(-Math.Cos(z)) * 50 x1 = CSng(-Math.Sin(z)) * 10 y1 = CSng(Math.Cos(z)) * 10 x2 = CSng(Math.Sin(u)) * 4 y2 = CSng(-Math.Cos(u)) * 4 x3 = CSng(-Math.Sin(u)) * 4 y3 = CSng(Math.Cos(u)) * 4 Dim limeBrush As New SolidBrush(Color.Lime) aigPoints(0).X = x1 + 60 aigPoints(0).Y = y1 + 60 aigPoints(1).X = x2 + 60 aigPoints(1).Y = y2 + 60 aigPoints(2).X = x0 + 60 aigPoints(2).Y = y0 + 60 aigPoints(3).X = x3 + 60 aigPoints(3).Y = y3 + 60 e.Graphics.FillPolygon(limeBrush, aigPoints) e.Graphics.DrawPolygon(blackPen, aigPoints) limeBrush.Dispose() '----------------------- ' Aiguille des Heures n = d.Hour * 200 / 12 + d.Minute * 200 / 60 / 12 z = n / 100 * 3.14159F u = (n + 50) / 100 * 3.14159F x0 = CSng(Math.Sin(z)) * 35 y0 = CSng(-Math.Cos(z)) * 35 x1 = CSng(-Math.Sin(z)) * 10 y1 = CSng(Math.Cos(z)) * 10 x2 = CSng(Math.Sin(u)) * 4 y2 = CSng(-Math.Cos(u)) * 4 x3 = CSng(-Math.Sin(u)) * 4 y3 = CSng(Math.Cos(u)) * 4 Dim yellowBrush As New SolidBrush(Color.Yellow) aigPoints(0).X = x1 + 60 aigPoints(0).Y = y1 + 60 aigPoints(1).X = x2 + 60 aigPoints(1).Y = y2 + 60 aigPoints(2).X = x0 + 60 aigPoints(2).Y = y0 + 60 aigPoints(3).X = x3 + 60 aigPoints(3).Y = y3 + 60 e.Graphics.FillPolygon(yellowBrush, aigPoints) e.Graphics.DrawPolygon(blackPen, aigPoints) yellowBrush.Dispose() '----------------------- ' Chiffres Dim drawFont As New Font("Arial", 8) Dim drawBrush As New SolidBrush(Color.Black) e.Graphics.DrawString("12", drawFont, drawBrush, 55, 6) e.Graphics.DrawString("6", drawFont, drawBrush, 58, 101) e.Graphics.DrawString("3", drawFont, drawBrush, 105, 53) e.Graphics.DrawString("9", drawFont, drawBrush, 8, 53) '----------------------- drawFont.Dispose() drawBrush.Dispose() blackPen.Dispose() MyBase.OnPaint(e) End Sub 'OnPaint '-------------------------------------------------------- Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing And Not (t Is Nothing) Then t.Dispose() t = Nothing End If MyBase.Dispose(disposing) End Sub 'Dispose End Class 'HorlogeEnd Namespace 'Composant
Article publié le 05 Janvier 2012 Mise à jour le Samedi, 17 Décembre 2022 15:52 par GC Team