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
|
Public Class FormCarte
Private MyCustomCursor As Cursor
Private ListCartes As New List(Of PictureBox)
Private pnlCarte As Panel
Private sizePanel As Integer
Private NbRanges As Integer = 5
Private margeRange As Integer = 50 'suivant X et Y
Private N As Integer = NbRanges - 1
Private widthCarte As Integer = 50
Private spacing As Integer = 10
Private Sub FormCarte_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
pnlCarte = New Panel
sizePanel = NbRanges * (widthCarte + spacing + margeRange)
pnlCarte.Size = New Size(sizePanel, sizePanel)
pnlCarte.MaximumSize = New Size(sizePanel, sizePanel)
pnlCarte.MinimumSize = MaximumSize
pnlCarte.BackColor = Color.LightGray
pnlCarte.BorderStyle = BorderStyle.FixedSingle
Me.Controls.Add(pnlCarte)
CreateBoard()
End Sub
Private Sub CreateBoard()
Dim X, Y As Integer
ListCartes.Clear()
pnlCarte.Controls.Clear()
Dim carte As PictureBox
For row As Integer = N To 0 Step -1
For col As Integer = N To 0 Step -1
X = margeRange + col * (widthCarte + spacing)
Y = margeRange + row * (widthCarte + spacing)
carte = New PictureBox()
carte.Name = "PIC" + row.ToString() + col.ToString()
carte.Size = New Size(widthCarte, widthCarte)
Select Case row
Case 0, 1
carte.Image = GetImage(carte, Color.Blue)
Case 2
carte.Image = GetImage(carte, Color.PaleGoldenrod)
Case 3, 4
carte.Image = GetImage(carte, Color.Green)
End Select
carte.BorderStyle = Windows.Forms.BorderStyle.FixedSingle
carte.Location = New Point(X, Y)
carte.AllowDrop = True
AddHandler carte.MouseDown, AddressOf carte_MouseDown
'secret
AddHandler carte.GiveFeedback, AddressOf carte_GiveFeedBack
AddHandler carte.MouseUp, AddressOf carte_MouseUp
AddHandler carte.DragOver, AddressOf pnl_DragOver
AddHandler carte.DragEnter, AddressOf pnl_DragEnter
AddHandler carte.DragDrop, AddressOf pnl_DragDrop
ListCartes.Add(carte)
Next
Next
pnlCarte.Controls.AddRange(ListCartes.ToArray)
End Sub
Private Function GetImage(pic As PictureBox, clr As Color) As Bitmap
Dim bmp As New Bitmap(pic.Width, pic.Height)
Using grfx As Graphics = Graphics.FromImage(bmp)
grfx.Clear(clr)
grfx.DrawString(pic.Name, pic.Font, Brushes.White, 0, 0)
End Using
Return bmp
End Function
'Private pic As PictureBox
Private Sub carte_MouseDown(sender As Object, e As MouseEventArgs)
Dim pic As PictureBox = CType(sender, PictureBox)
pic.DoDragDrop(pic, DragDropEffects.Move)
End Sub
'secret
Private Sub carte_GiveFeedBack(sender As Object, e As GiveFeedbackEventArgs)
Dim pic As PictureBox = CType(sender, PictureBox)
e.UseDefaultCursors = False
If ((e.Effect Or DragDropEffects.Move) = DragDropEffects.Move) Then
Dim bmp As Bitmap = New Bitmap(pic.Image, pic.Width, pic.Height)
MyCustomCursor = New Cursor(bmp.GetHicon())
Cursor.Current = MyCustomCursor
Else
Cursor.Current = Cursors.Default
End If
End Sub
Private Sub carte_MouseUp(sender As Object, e As MouseEventArgs)
Cursor.Current = Cursors.Default
End Sub
Private Sub pnl_DragOver(sender As System.Object, e As System.Windows.Forms.DragEventArgs)
If Not (e.Data.GetDataPresent(GetType(PictureBox))) Then
e.Effect = DragDropEffects.None
Return
End If
End Sub
Private Sub pnl_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs)
If e.Data.GetDataPresent(GetType(PictureBox)) Then
e.Effect = DragDropEffects.Move
End If
End Sub
Private Sub pnl_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs)
Dim picDst As PictureBox = CType(sender, PictureBox)
If e.Data.GetDataPresent(GetType(PictureBox)) Then
Dim picSrc As PictureBox = CType(e.Data.GetData(GetType(PictureBox)), PictureBox)
' Perform drag-and-drop, depending upon the effect.
If (e.Effect = DragDropEffects.Move) Then
'' Insert the item.
'Dim pos As New Point(e.X, e.Y)
'pos = pnlCarte.PointToClient(pos)
'picSource.Location = pos
SwapCartes(picDst, picSrc)
Cursor.Current = Cursors.Default
End If
End If
End Sub
Private Sub SwapCartes(dst As PictureBox, src As PictureBox)
Dim tempLoc As Point = src.Location
src.Location = dst.Location
dst.Location = tempLoc
End Sub
End Class |
Partager