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.

ExerciceVB-id2108


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.ComponentModel
Namespace 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 'Horloge
End Namespace 'Composant
 
**********************************************************************************************
 
 
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.ComponentModel
Namespace 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 'Horloge
End Namespace 'Composant