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
|
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)
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.Text = "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())
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.Text, 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
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
foundItem.BackColor = Color.Magenta
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
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