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
|
Public Class FormCartes
Private INTERVAL As Integer = 20
Private SPAWNX As Integer = 20
Private SPAWNY As Integer = 20
Private Columns As Integer = 8
Private Rows As Integer = 8
Private colWidth As Integer = 64
Private rowHeight As Integer = 64
Private Cartes As New List(Of PictureBox)
'Ajout du sosie du target pic (cible)
'c'est le proxy du mechant captain William dans Invaders
'un proxy en programmation est un objet qui ressemble à l'instance reelle
' à s'y meprendre
Private targetSosie As PictureBox = Nothing
Private Sub FormCartes_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.AutoSize = True
AddCartes()
End Sub
Private Sub AddCartes()
Me.Cartes.Clear()
Me.Controls.Clear()
Dim pic As PictureBox
Dim X, Y As Integer
For i As Integer = 0 To Rows - 1
For j As Integer = 0 To Columns - 1
X = SPAWNX + j * (colWidth + INTERVAL)
Y = SPAWNY + i * (rowHeight + INTERVAL)
pic = New PictureBox
pic.BorderStyle = BorderStyle.FixedSingle
pic.BackColor = Color.Blue
pic.Location = New Point(X, Y)
pic.Size = New Size(colWidth, rowHeight)
Cartes.Add(pic)
pic.Tag = "No " + Cartes.IndexOf(pic).ToString
AddHandler pic.Paint, AddressOf PicPaint
AddHandler pic.MouseDown, AddressOf FormMouseDown
AddHandler pic.MouseMove, AddressOf FormMouseMove
AddHandler pic.MouseUp, AddressOf FormMouseUp
Next
Next
Me.Controls.AddRange(Cartes.ToArray())
'INITIALISAION DU PROXY
targetSosie = New PictureBox()
targetSosie.Location = New Point(-100, -100)
targetSosie.Size = New Size(colWidth, rowHeight)
targetSosie.BackColor = Color.Red
targetSosie.Text = String.Empty
AddHandler targetSosie.Paint, AddressOf PicPaint
'AJOUT
Me.Controls.Add(targetSosie)
End Sub
Private Sub PicPaint(ByVal sender As Object, ByVal e As PaintEventArgs)
Dim item As PictureBox = CType(sender, PictureBox)
Dim gr As Graphics = e.Graphics
Dim rect As Rectangle = item.ClientRectangle
rect.Inflate(-15, -15)
Using fnt As New Font("Arial", 8.0)
Using br As New SolidBrush(Color.White)
gr.DrawString(item.Tag, fnt, br, item.Width / 4, item.Height / 4)
End Using
End Using
End Sub
Private isDragging As Boolean
Private ptDown As Point
Private draggedControl As PictureBox
Private P0 As Point = Point.Empty
Private P1 As Point = Point.Empty
Private Sub FormMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
draggedControl = CType(sender, PictureBox)
draggedControl.BringToFront()
If e.Button = MouseButtons.Left Then
P0 = draggedControl.Location
isDragging = True
ptDown = e.Location
End If
End Sub
Private Sub FormMouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
draggedControl = CType(sender, PictureBox)
If isDragging And e.Button = MouseButtons.Left Then
draggedControl.Left += e.Location.X - ptDown.X
draggedControl.Top += e.Location.Y - ptDown.Y
For Each item As PictureBox In Cartes
If item Is draggedControl Then Continue For
If draggedControl.Bounds.IntersectsWith(item.Bounds) Then
targetSosie.Location = New Point(item.Location.X + 25, item.Location.Y + 25)
targetSosie.Tag = item.Tag
targetSosie.Update()
targetSosie.BringToFront()
End If
Next
End If
End Sub
Private Sub FormMouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
draggedControl = CType(sender, PictureBox)
If isDragging And e.Button = MouseButtons.Right Then
Dim foundItem As PictureBox = Nothing
For Each item As PictureBox In Cartes
If item Is draggedControl Then Continue For
If draggedControl.Bounds.IntersectsWith(item.Bounds) Then
foundItem = item
Exit For
End If
' TEST COMBINE D'INTERSECTION TENANT COMPTE EN PLUS DE VALEUR
'If draggedControl.Bounds.IntersectsWith(item.Bounds) And item.Tag = valeur Then
' foundItem = item
' Exit For
'End If
Next
If foundItem IsNot Nothing Then
SwapPictures(draggedControl, foundItem)
Else
draggedControl.Location = P0
draggedControl.SendToBack()
End If
draggedControl = Nothing
ptDown = Point.Empty
P0 = Point.Empty
P1 = Point.Empty
isDragging = False
End If
'RAZ SOSIE
targetSosie.Location = New Point(-100, -100)
targetSosie.Text = String.Empty 'REMOVE
End Sub
Private Sub SwapPictures(ByVal draggedPic As PictureBox, ByVal targetPic As PictureBox)
P1 = targetPic.Location
targetPic.Location = P0
draggedPic.Location = P1
End Sub
End Class |
Partager