Bonsoir,

Je recherche une aide pour gérer le déplacement par la souris ou les flèches de 2 contrôles créés dans l'application. Ces contrôles sont destinés à simuler des paquets placés sur une palette.

L'application doit gérer les éventuelles collisions entre paquets, avec l'outil de préhension et fournir les coordonnées de chaque paquet posé sur la palette.

Le principe est le suivant :

l'opérateur place chaque paquet (ControlRgn) sur la palette (panel1)

mes problèmes sont les suivants :

_ comment gérer le déplacement du paquet par la souris (éviter la déformation, suivre exactement le curseur)

_ comment détecter les collisions entre régions GDI

_ comment ajouter un paquet sur la palette (Panel1.Controls.Add(paquet)) ne "mémorise" pas le paquet

Voici une partie de mon code, si vous pouvez m'aider.....

merci
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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
Imports System.Drawing.Drawing2D
Public Class ControlRgn
    Inherits System.Windows.Forms.Control
    Public Sub New()
 
        ' Cet appel est requis par le concepteur.
        'InitializeComponent()
 
        ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
 
        'evite le scintimment
        Me.SetStyle(ControlStyles.UserPaint Or ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or
         ControlStyles.OptimizedDoubleBuffer, True)
    End Sub
 
    'border du control
    Private rectBorder As Rectangle
    Private penBorder As New Pen(Brushes.Magenta, 2.0)
    Private offsetBorderX As Integer = 0 '5
    Private offsetBorderY As Integer = 0 '5
    'dessin
 
    Private ptCentre As New Point()
    Private m_draw As Boolean = True
 
    'geometries
    Private pathMaster As GraphicsPath
    Private rectMaster As Rectangle
    Private pathSlave1 As GraphicsPath
    Private rectSlave1 As Rectangle
    Private pathDos As GraphicsPath
    Private rectDos As Rectangle
 
 
    'offset pour centrer les figures
    Private offsetRgnX As Integer = 0 '50
    Private offsetRgnY As Integer = 0 '50
    Private centre As New Point()
 
    'position paquet au sein de la palette
    Private x As Integer
    Private y As Integer
    Private z As Integer
    Private rz As Integer
    Private NumPaq As Integer
 
    'dimensions paquet
    Private iLargPaq As Integer
    Private iLongPaq As Integer
    Private iDepLargPaq As Integer
    Private iDepLongPaq As Integer
    Private iColorPaq As Integer
    Private iColorDepPaq As Integer
 
    Private iEpDos As Integer = 10
    Private iColorDos As Integer
 
    Private xCollision As Boolean
 
    Public Property Collision() As Boolean
        Get
            Return xCollision
        End Get
        Set(ByVal value As Boolean)
            xCollision = value
            RefreshPath()
            Me.Invalidate()
        End Set
    End Property
 
    Public Property Draw() As Boolean
        Get
            Return m_draw
        End Get
        Set(ByVal value As Boolean)
            m_draw = value
            RefreshPath()
            Me.Invalidate()
        End Set
    End Property
 
    Private m_rgn1 As New Region
    Public Property Region1() As Region
 
        Get
            Return m_rgn1
        End Get
        Set(ByVal value As Region)
            m_rgn1 = value
        End Set
    End Property
 
    Private m_rgn2 As New Region
    Public Property Region2() As Region
 
        Get
            Return m_rgn2
        End Get
        Set(ByVal value As Region)
            m_rgn2 = value
        End Set
    End Property
 
    Private m_rgn3 As New Region
    Public Property Region3() As Region
 
        Get
            Return m_rgn3
        End Get
        Set(ByVal value As Region)
            m_rgn3 = value
        End Set
    End Property
 
    Public Property posx() As Integer
        Get
            Return x
        End Get
        Set(ByVal value As Integer)
            x = value
        End Set
    End Property
 
    Public Property posy() As Integer
        Get
            Return y
        End Get
        Set(ByVal value As Integer)
            y = value
        End Set
    End Property
 
    Public Property posz() As Integer
        Get
            Return z
        End Get
        Set(ByVal value As Integer)
            z = value
        End Set
    End Property
 
    Public Property posrz() As Integer
        Get
            Return rz
        End Get
        Set(ByVal value As Integer)
            rz = value
            RefreshPath()
            Me.Invalidate()
        End Set
    End Property
 
    Public Property LargeurPaquet() As Integer
        Get
            Return iLargPaq
        End Get
        Set(ByVal value As Integer)
            iLargPaq = value
            RefreshPath()
            Me.Invalidate()
        End Set
    End Property
 
    Public Property LongueurPaquet() As Integer
        Get
            Return iLongPaq
        End Get
        Set(ByVal value As Integer)
            iLongPaq = value
            RefreshPath()
            Me.Invalidate()
        End Set
    End Property
 
    Public Property NumPaquet() As Integer
        Get
            Return NumPaq
        End Get
        Set(ByVal value As Integer)
            NumPaq = value
            RefreshPath()
            Me.Invalidate()
        End Set
    End Property
 
    Public Property CouleurPaquet() As Integer
        Get
            Return iColorPaq
        End Get
        Set(ByVal value As Integer)
            iColorPaq = value
        End Set
    End Property
 
    Public Property CouleurDos() As Integer
        Get
            Return iColorDos
        End Get
        Set(ByVal value As Integer)
            iColorDos = value
        End Set
    End Property
 
 
    Public Property DepassantLargeurPaquet() As Integer
        Get
            Return iDepLargPaq
        End Get
        Set(ByVal value As Integer)
            iDepLargPaq = value
            RefreshPath()
            Me.Invalidate()
        End Set
    End Property
 
    Public Property DepassantLongueurPaquet() As Integer
        Get
            Return iDepLongPaq
        End Get
        Set(ByVal value As Integer)
            iDepLongPaq = value
            RefreshPath()
            Me.Invalidate()
        End Set
    End Property
 
    Public Property CouleurDepassant() As Integer
        Get
            Return iColorDepPaq
        End Get
        Set(ByVal value As Integer)
            iColorDepPaq = value
        End Set
    End Property
 
    Private Sub RefreshPath()
        If pathMaster IsNot Nothing Then pathMaster.Dispose()
        pathMaster = New GraphicsPath()
        If pathSlave1 IsNot Nothing Then pathSlave1.Dispose()
        pathSlave1 = New GraphicsPath()
        If pathDos IsNot Nothing Then pathDos.Dispose()
        pathDos = New GraphicsPath()
        Dim wM As Integer
        Dim hM As Integer
        wM = Me.Width
        hM = Me.Height
 
        Dim pt1 As New Point(0, 0)
        rectMaster = New Rectangle(pt1, New Size(wM, hM))
        pathMaster.AddRectangle(rectMaster)
        m_rgn1 = New Region(pathMaster)
 
        Dim pt2 As New Point(iDepLongPaq / 2, iDepLargPaq / 2)
        rectSlave1 = New Rectangle(pt2, New Size(wM - iDepLongPaq, hM - iDepLargPaq))
        pathSlave1.AddRectangle(rectSlave1)
        m_rgn2 = New Region(pathSlave1)
        m_rgn1.Exclude(m_rgn2)
        ptCentre = New Point(rectMaster.Left + rectMaster.Width / 2, rectMaster.Top + rectMaster.Height / 2)
 
        Dim pt3 As New Point(0, 0)
        Select Case rz
            Case Is = 0
                pt3 = pt2
                rectDos = New Rectangle(pt3, New Size(rectSlave1.Width, iEpDos))
            Case Is = 90
                pt3 = pt2
                rectDos = New Rectangle(pt3, New Size(iEpDos, rectSlave1.Height))
            Case Is = 180
                pt3 = New Point(rectSlave1.Left, rectSlave1.Top + rectSlave1.Height - iEpDos)
                rectDos = New Rectangle(pt3, New Size(rectSlave1.Width, iEpDos))
            Case Is = 270
                pt3 = New Point(rectSlave1.Left + rectSlave1.Width - iEpDos, rectSlave1.Top)
                rectDos = New Rectangle(pt3, New Size(iEpDos, rectSlave1.Height))
        End Select
        pathDos.AddRectangle(rectDos)
        m_rgn3 = New Region(pathDos)
        m_rgn1.Union(m_rgn3)
    End Sub
 
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)
 
        'Ajoutez ici votre code de dessin personnalisé
        'dessin du border d'abord
        Dim gr As Graphics = e.Graphics
        rectBorder = New Rectangle(offsetBorderX, offsetBorderY, Me.Bounds.Width - offsetBorderX, Me.Bounds.Height - offsetBorderY)
        gr.DrawRectangle(penBorder, rectBorder)
 
 
        'dessin du path
 
 
        If pathMaster IsNot Nothing Then
 
            gr.SmoothingMode = SmoothingMode.AntiAlias
 
            'fill les regions
            gr.FillRegion(Brushes.Yellow, m_rgn1) 'paquet avec marges
            If Not xCollision Then
                gr.FillRegion(Brushes.Green, m_rgn2) 'paquet nominal
            Else
                gr.FillRegion(Brushes.Red, m_rgn2) 'paquet nominal
            End If
            gr.FillRegion(Brushes.Blue, m_rgn3) 'dos paquet
 
            'les contours des paths
            gr.DrawPath(Pens.Black, pathMaster)
            gr.DrawPath(Pens.Black, pathSlave1)
            gr.DrawPath(Pens.Black, pathDos)
        End If
 
 
 
    End Sub
 
    'exemple de customization integre au control
    'sur click le graphics fait tourner tout ce qui est dessine
    Protected Overrides Sub OnMouseClick(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseClick(e)
        'code à écrire
        Me.Invalidate()
    End Sub
 
 
    Private Sub ControlRgn_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
 
        RefreshPath()
        Me.Invalidate()
    End Sub
End Class
 
 
Option Explicit On
Imports System.Drawing.Drawing2D
Public Class ClsOutil
    Inherits System.Windows.Forms.Control
    Public Sub New()
 
        ' Cet appel est requis par le concepteur.
        'InitializeComponent()
 
        ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
 
        'evite le scintimment
        'Me.SetStyle(ControlStyles.UserPaint Or ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or
        'ControlStyles.OptimizedDoubleBuffer, True)
    End Sub
 
    'border du control
    Private rectBorder As Rectangle
    Private penBorder As New Pen(Brushes.Magenta, 2.0)
    Private offsetBorderX As Integer = 0 '5
    Private offsetBorderY As Integer = 0 '5
 
    'dessin
    Private m_draw As Boolean = True
 
    'geometries
    Private pathMaster As GraphicsPath
    Private rectMaster As Rectangle
 
    'dimensions outil
    Private rz As Integer
    Private iLargOutil As Integer
    Private iLongOutil As Integer
    Private iColorOutil As Integer
 
    Public Property Draw() As Boolean
        Get
            Return m_draw
        End Get
        Set(ByVal value As Boolean)
            m_draw = value
            RefreshPath()
            Me.Invalidate()
        End Set
    End Property
 
    Private m_rgn1 As New Region
    Public Property Region1() As Region
 
        Get
            Return m_rgn1
        End Get
        Set(ByVal value As Region)
            m_rgn1 = value
        End Set
    End Property
 
    Public Property posrz() As Integer
        Get
            Return rz
        End Get
        Set(ByVal value As Integer)
            rz = value
            RefreshPath()
            Me.Invalidate()
        End Set
    End Property
 
    Public Property LargeurOutil() As Integer
        Get
            Return iLargOutil
        End Get
        Set(ByVal value As Integer)
            iLargOutil = value
            RefreshPath()
            Me.Invalidate()
        End Set
    End Property
 
    Public Property LongueurOutil() As Integer
        Get
            Return iLongOutil
        End Get
        Set(ByVal value As Integer)
            iLongOutil = value
        End Set
    End Property
 
    Public Property CouleurOutil() As Integer
        Get
            Return iColorOutil
        End Get
        Set(ByVal value As Integer)
            iColorOutil = value
        End Set
    End Property
 
    Private Sub RefreshPath()
        If pathMaster IsNot Nothing Then pathMaster.Dispose()
        pathMaster = New GraphicsPath()
 
        Dim pt1 As New Point
        Select Case rz
            Case Is = 0
                pt1 = New Point(0, 0)
                rectMaster = New Rectangle(pt1, New Size(Me.Width, Me.Height))
            Case Is = 90
                pt1 = New Point(0, 0)
                rectMaster = New Rectangle(pt1, New Size(Me.Width, Me.Height))
            Case Is = 180
                pt1 = New Point(0, 0)
                rectMaster = New Rectangle(pt1, New Size(Me.Width, Me.Height))
            Case Is = 270
                pt1 = New Point(0, 0)
                rectMaster = New Rectangle(pt1, New Size(Me.Width, Me.Height))
        End Select
        pathMaster.AddRectangle(rectMaster)
        m_rgn1 = New Region(pathMaster)
    End Sub
 
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)
 
        'Ajoutez ici votre code de dessin personnalisé
        'dessin du border d'abord
        Dim gr As Graphics = e.Graphics
        rectBorder = New Rectangle(offsetBorderX, offsetBorderY, Me.Bounds.Width - offsetBorderX, Me.Bounds.Height - offsetBorderY)
        gr.DrawRectangle(penBorder, rectBorder)
 
 
        'dessin du path
        Dim ColorOutil As Color
        ColorOutil = Color.FromArgb(iColorOutil)
        Dim BrushO As New SolidBrush(ColorOutil)
 
 
 
        If pathMaster IsNot Nothing Then
 
            gr.SmoothingMode = SmoothingMode.AntiAlias
 
            'fill les regions
            gr.FillRegion(Brushes.Yellow, m_rgn1)
            'gr.FillRegion(BrushO, m_rgn1)
 
            'les contours des paths
            gr.DrawPath(Pens.Black, pathMaster)
        End If
    End Sub
 
    'exemple de customization integre au control
    'sur click le graphics fait tourner tout ce qui est dessine
    Protected Overrides Sub OnMouseClick(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseClick(e)
        'code à écrire
        Me.Invalidate()
    End Sub
 
 
    Private Sub ControlRgn_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
        RefreshPath()
        Me.Invalidate()
    End Sub
End Class
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
Option Explicit On
Imports System.IO
Imports System
Imports System.Drawing.Drawing2D
 
Public Class FrmMain
    Private WithEvents PaqModele As ControlRgn
    Private WithEvents Outil As ClsOutil
    Private WithEvents PaqMobile As ControlRgn
    Private WithEvents OutilMobile As ClsOutil
    Private RgnTemp As New [Region]
    Private RgnTempVide As Boolean
    Private ContourPanel As New [Region]
    Private Panel As New [Region]
    Private Marge1LongPal As New [Region]
    Private Marge2LongPal As New [Region]
    Private Marge3LargPal As New [Region]
    Private Marge4LargPal As New [Region]
 
   'Déplacement souris au dessus du paqmobile
    Dim deltaX As Integer = 0
    Dim deltaY As Integer = 0
 
    Dim memoDsLargeur
    Dim memoDsHauteur
    Dim increm As Integer = 4
    Private Sub Paqmobile_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PaqMobile.MouseMove
        Dim i As Integer
        Static j As Integer
        Static memx As Integer
        Static memy As Integer
        If memx <> 0 Or memy <> 0 Then
            deltaX = memx - e.X
            deltaY = memy - e.Y
            If deltaX < -40 Then
                deltaX = -40
            End If
            If deltaX > 40 Then
                deltaX = 40
            End If
            If deltaY < -40 Then
                deltaY = -40
            End If
            If deltaY > 40 Then
                deltaY = 40
            End If
        End If
 
        If accroche And (deltaX <> 0 Or deltaY <> 0) Then
            PaqMobile.Visible = True
            If deltaX > 0 Then
                PaqMobile.Left = PaqMobile.Left - deltaX '- offX
 
                memx = e.X + deltaX
            End If
            If deltaY > 0 Then
                PaqMobile.Top = PaqMobile.Top - deltaY 'deltaY '- offY
                memy = e.Y + deltaY
            End If
            If deltaX < 0 Then
                PaqMobile.Left = PaqMobile.Left + Math.Abs(deltaX) 'deltaX '- offX
                memx = e.X - Math.Abs(deltaX)
            End If
            If deltaY < 0 Then
                PaqMobile.Top = PaqMobile.Top + Math.Abs(deltaY) 'deltaY '- offY
                memy = e.Y - Math.Abs(deltaY)
            End If
            j = j + 1
 
            Timer2.Interval = 100
            Timer2.Enabled = True
            Timer2.Start()
            Timer3.Interval = 2
            Timer3.Enabled = True
            Timer3.Start()
            Dessin()
 
        End If
        TextBox12.Text = Ecartx & " " & Ecarty
        memy = e.Y
 
    End Sub
 
    Private Sub Timer2_Tick(sender As Object, e As System.EventArgs) Handles Timer2.Tick
        Timer2.Enabled = False
        List1.Items.Add(";" & deltaX.ToString & ";" & deltaY.ToString)
        TextBox13.Text = deltaX & " " & deltaY
    End Sub
 
    Private Sub Timer3_Tick(sender As Object, e As System.EventArgs) Handles Timer3.Tick
        Timer3.Enabled = False
        RazDessin()
    End Sub
 
    Public Sub Dessin()
        PaqMobile.Draw = True
        OutilMobile.Left = PaqMobile.Left - Ecartx
        OutilMobile.Top = PaqMobile.Top - Ecarty
        OutilMobile.Draw = True
    End Sub
 
    Public Sub RazDessin()
        PaqMobile.Draw = False
        OutilMobile.Draw = False
        PaqMobile.BringToFront()
        OutilMobile.BringToFront()
    End Sub