Bonjour à tous,
Actuellement je m’amuse, si je puis dire, à créer des objets graphique un peu plus évolué dans VB.
Le but étant d'avoir un label plus joli certes, mais j'ai plusieurs zone avec un nombre de label variant suivant la configuration du système sur lequel mon logiciel sera connecté.
Voila ce que j'ai fait pour faire mon label:
Il y a encore pas mal d’amélioration bien-sur et à le nettoyer car j'ai créé des paramètres qui existe déjà de base avec le Usercontrol.
Je n'est pas hérité du label, mais du usercontrol car part le dessin je peux créer une ombre au texte. Je m'en sert pour alerté l'utilisateur par exemple sur une information qui ne convient pas.
Voila mon label:
Pour ce label j'ai créer une classe herité donc de usercontrol. (RoundedLabel)
Et j'ai recréé un control (classe MultiRoundLabel) qui appel par défaut un RoundedLabel, mais peut en appeler jusqu’à 4 au final (j'ai pas encore mis de borne).
Voila avec 4 roundedlabel:
Mais si je remet 2 RoundedLabel, j'ai bien le texte qui est enlevé, mais pas l'image du rectangle avec arrondie.
L'utilisateur peut changer de système à interroger sans fermer le logiciel. Donc il faut vraiment que la suppression soit sur tout l'objet effacé
Voici la classe RoundedLabel:
La classe MultiRoundLabel
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 Public Class RoundedLabel : Inherits UserControl Public _FillBrush As Brush '= Brushes.White Public _BorderPen As Pen '= Pens.Black Private _Radius As Single = 20 Private _Text As String Private _TextColor As Brush Private _TextShadowColor As Brush Private _TextFont As Font Private _Shadow As Boolean Private _Name As String Private _Visible As Boolean Private saveGraphic As GraphicsState = Nothing Private ClearGraph As Boolean = False Public Property m_Name() As String Get Return _Name End Get Set(value As String) _Name = value End Set End Property Public Property m_ShadowColor() As Color Get Return New Pen(_TextShadowColor).Color End Get Set(value As Color) _TextShadowColor = New SolidBrush(value) Refresh() End Set End Property Public Property m_Shadow() As Boolean Get Return _Shadow End Get Set(value As Boolean) _Shadow = value Refresh() End Set End Property Public Property m_TextFont() As Font Get Return _TextFont End Get Set(value As Font) _TextFont = value Refresh() End Set End Property Public Property m_TextColor() As Color Get Return New Pen(_TextColor).Color End Get Set(value As Color) _TextColor = New SolidBrush(value) Refresh() End Set End Property Public Property m_FillBrush() As Color Get Return New Pen(_FillBrush).Color End Get Set(value As Color) _FillBrush = New SolidBrush(value) Refresh() End Set End Property Public Property m_BorderPen() As Color '= Pens.Black Get Return _BorderPen.Color End Get Set(value As Color) _BorderPen = New Pen(value) Refresh() End Set End Property Public Property m_Radius As Single Get Return _Radius End Get Set(value As Single) _Radius = value Me.Invalidate() Refresh() End Set End Property Public Property m_Text() As String Get Return _Text End Get Set(value As String) _Text = value Refresh() End Set End Property Public Property m_Visible() As Boolean Get Return _Visible End Get Set(value As Boolean) _Visible = value Refresh() End Set End Property Public Sub New() Dim f As New Font("Microsoft Sans Serif", 14, FontStyle.Bold) _TextFont = f _TextColor = Brushes.Black _TextShadowColor = Brushes.Yellow ' New SolidBrush(Color.FromArgb(187, 187, 185)) _FillBrush = New SolidBrush(Color.FromArgb(70, 68, 68)) _BorderPen = Pens.Black _Text = "MonText" Me.BackColor = Color.Transparent _Visible = True End Sub Protected Overrides Sub OnParentChanged(e As System.EventArgs) Static oldparent As Control If _Name = "DSPMeters22" Then Debug.Print("") End If If _Visible = True Then If Me.Parent IsNot Nothing Then If oldparent IsNot Nothing Then RemoveHandler oldparent.Paint, AddressOf DrawRoundedRectangle AddHandler Me.Parent.Paint, AddressOf DrawRoundedRectangle oldparent = Me.Parent End If MyBase.OnParentChanged(e) End If End Sub Protected Overrides Sub DestroyHandle() If Me.Parent IsNot Nothing Then RemoveHandler Me.Parent.Paint, AddressOf DrawRoundedRectangle MyBase.DestroyHandle() End Sub Public Sub DrawRoundedRectangle(sender As Object, e As PaintEventArgs) Dim sm As Drawing2D.SmoothingMode = e.Graphics.SmoothingMode 'save the previous smoothing mode If IsNothing(saveGraphic) Then saveGraphic = e.Graphics.Save End If e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality Dim rect As Rectangle = Me.Bounds rect.Inflate(-1, -1) Dim path As Drawing2D.GraphicsPath = GetRoundedRectangle(rect, _Radius) If _FillBrush IsNot Nothing Then e.Graphics.FillPath(_FillBrush, path) End If If _BorderPen IsNot Nothing Then e.Graphics.DrawPath(_BorderPen, path) End If e.Graphics.SmoothingMode = sm 'restore the previous smoothing mode End Sub Public Sub ClearGraphic() ClearGraph = True Refresh() End Sub Private Function GetRoundedRectangle(rect As Rectangle, radius As Single) As Drawing2D.GraphicsPath Dim gp As New Drawing2D.GraphicsPath With rect gp.AddArc(.X + .Width - radius, .Y, radius, radius, 270, 90) gp.AddArc(.X + .Width - radius, .Y + .Height - radius, radius, radius, 0, 90) gp.AddArc(.X, .Y + .Height - radius, radius, radius, 90, 90) gp.AddArc(.X, .Y, radius, radius, 180, 90) gp.CloseFigure() End With Return gp End Function Protected Overrides Sub OnPaint(pevent As System.Windows.Forms.PaintEventArgs) MyBase.OnPaint(pevent) If _Name = "DSPMeters22" Then Debug.Print("") End If If ClearGraph Then pevent.Graphics.Restore(saveGraphic) End If If _Visible = True Then Dim G As Graphics = pevent.Graphics Dim m As SizeF = G.MeasureString(_Text, _TextFont) Dim x As Single = (Width / 2) - m.Width / 2 Dim y As Single = (Height / 2) - m.Height / 2 Dim pf As New PointF(x, y) Dim pf2 As New PointF(x + 1, y + 1) If _Shadow Then G.DrawString(_Text, _TextFont, _TextShadowColor, pf2) End If G.DrawString(_Text, _TextFont, _TextColor, pf) Else Debug.Print("") End If End Sub End Class
J'ai essayé avec le graphic.clear(), mais je ne vois pas trop comment l'utiliser dans mon cas, j'ai vu aussi le save et restore, mais la encore je ne dois pas l'utiliser correctement car cela ne fonctionne pas.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 Public Class MultiRoundLabel : Inherits UserControl #Region "variable" Public _NbLalel As Integer Public _Espace As Integer Public _Height As Integer Public _MesLabels() As RoundedLabel Public _FillBrush As Color '= Brushes.White Public _BorderPen As Color '= Pens.Black Private _Radius As Single = 20 Private _Text() As String Private _TextColor As Color Private _TextShadowColor As Color Private _TextFont As Font Private _Shadow() As Boolean #End Region #Region "constructeur" Public Sub New() Dim f As New Font("Microsoft Sans Serif", 14, FontStyle.Bold) _NbLalel = 1 _Espace = 3 _TextFont = f _Height = 25 _TextColor = Color.Black _TextShadowColor = Color.Yellow ' New SolidBrush(Color.FromArgb(187, 187, 185)) _FillBrush = Color.FromArgb(70, 68, 68) _BorderPen = Color.Black ReDim _Text(0) _Text(0) = "- - -" _Radius = 20 Me.BackColor = Color.Transparent ReDim _MesLabels(0) ReDim _Shadow(0) _Shadow(0) = False _MesLabels(0) = New RoundedLabel _MesLabels(0).Width = Me.Width _MesLabels(0).Top = 0 _MesLabels(0).Left = 0 _MesLabels(0).Height = _Height _MesLabels(0).m_Radius = _Radius _MesLabels(0).m_Text = _Text(0) _MesLabels(0).m_TextColor = _TextColor _MesLabels(0).m_TextFont = _TextFont _MesLabels(0).m_BorderPen = _BorderPen _MesLabels(0).m_FillBrush = _FillBrush _MesLabels(0).m_Shadow = _Shadow(0) _MesLabels(0).m_ShadowColor = _TextShadowColor Me.Controls.Add(_MesLabels(0)) End Sub #End Region #Region "Property" Public Property m_Nblabel() As Integer Get Return _NbLalel End Get Set(value As Integer) Dim i As Integer If value <= 0 Then value = 0 End If If value < _NbLalel Then For i = 0 To UBound(_MesLabels) _MesLabels(i).ClearGraphic() Me.Controls.Remove(_MesLabels(i)) Next ReDim _MesLabels(value - 1) ReDim Preserve _Shadow(value - 1) ReDim Preserve _Text(value - 1) Else ReDim Preserve _MesLabels(value - 1) ReDim Preserve _Shadow(value - 1) ReDim Preserve _Text(value - 1) End If _NbLalel = value For i = 0 To UBound(_MesLabels) If IsNothing(_MesLabels(i)) Then _MesLabels(i) = New RoundedLabel _MesLabels(i).Width = Me.Width _MesLabels(i).Top = 0 _MesLabels(i).Left = 0 If i <> 0 Then _MesLabels(i).Top = i * (_Espace + _Height) End If _MesLabels(i).Height = _Height _MesLabels(i).m_Radius = _Radius _MesLabels(i).m_Text = _Text(i) _MesLabels(i).m_TextColor = _TextColor _MesLabels(i).m_TextFont = _TextFont _MesLabels(i).m_BorderPen = _BorderPen _MesLabels(i).m_FillBrush = _FillBrush _MesLabels(i).m_Shadow = _Shadow(i) _MesLabels(i).m_ShadowColor = _TextShadowColor Me.Controls.Add(_MesLabels(i)) End If Next Refresh() End Set End Property Public Property m_Espace() As Integer Get Return _Espace End Get Set(value As Integer) _Espace = value Refresh() End Set End Property Public Property m_ShadowColor() As Color Get Return _TextShadowColor End Get Set(value As Color) _TextShadowColor = (value) For i As Integer = 0 To UBound(_MesLabels) _MesLabels(i).m_ShadowColor = _TextShadowColor Next Refresh() End Set End Property Public Property m_Shadow() As Boolean() Get Return _Shadow End Get Set(value As Boolean()) _Shadow = value For i As Integer = 0 To UBound(_MesLabels) _MesLabels(i).m_Shadow = _Shadow(i) Next Refresh() End Set End Property Public Property m_TextFont() As Font Get Return _TextFont End Get Set(value As Font) _TextFont = value Refresh() End Set End Property Public Property m_TextColor() As Color Get Return New Pen(_TextColor).Color End Get Set(value As Color) _TextColor = value For i As Integer = 0 To UBound(_MesLabels) _MesLabels(i).m_TextColor = _TextColor Next Refresh() End Set End Property Public Property m_FillBrush() As Color Get Return New Pen(_FillBrush).Color End Get Set(value As Color) _FillBrush = value For i As Integer = 0 To UBound(_MesLabels) _MesLabels(i).m_FillBrush = _FillBrush Next Refresh() End Set End Property Public Property m_BorderPen() As Color '= Pens.Black Get Return _BorderPen End Get Set(value As Color) _BorderPen = value For i As Integer = 0 To UBound(_MesLabels) _MesLabels(i).m_BorderPen = _BorderPen Next Refresh() End Set End Property Public Property m_Radius As Single Get Return _Radius End Get Set(value As Single) _Radius = value For i As Integer = 0 To UBound(_MesLabels) _MesLabels(i).m_Radius = _Radius Next Me.Invalidate() Refresh() End Set End Property Public Property m_Text() As String() Get Return _Text End Get Set(value As String()) _Text = value For i As Integer = 0 To UBound(_MesLabels) _MesLabels(i).m_Text = _Text(i) Next Refresh() End Set End Property #End Region #Region "event" Protected Overrides Sub OnResize(e As System.EventArgs) MyBase.OnResize(e) UpdateParameter() End Sub Protected Overrides Sub OnParentChanged(e As System.EventArgs) MyBase.OnParentChanged(e) End Sub Private Sub UpdateParameter() Dim i As Integer For i = 0 To UBound(_MesLabels) _MesLabels(i).Width = Me.Width _MesLabels(i).Height = _Height _MesLabels(i).m_Radius = _Radius _MesLabels(i).m_TextColor = _TextColor _MesLabels(i).m_TextFont = _TextFont _MesLabels(i).m_BorderPen = _BorderPen _MesLabels(i).m_FillBrush = _FillBrush _MesLabels(i).m_Shadow = _Shadow(i) _MesLabels(i).m_ShadowColor = _TextShadowColor Next End Sub #End Region End Class
Merci de votre aide.
Partager