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
|
Public Class frmButtons
Private listeButtons As New List(Of Button)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub
Private Sub frmButtons_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'mets les boutons droppes dans listeButtons
For Each item As Control In Me.Controls
If TypeOf (item) Is Button Then
listeButtons.Add(item)
End If
Next
'handler des boutons droppes
For Each item As Button In listeButtons
AddHandler item.MouseDown, AddressOf Button_MouseDown
AddHandler item.MouseMove, AddressOf Button_MouseMove
AddHandler item.MouseUp, AddressOf Button_MouseUp
Next
End Sub
' button selectionne
Private selectedButton As Control = Nothing
'pour dessin
Private isDragging As Boolean = False
' Memorise position ou l'user a clicke sur le controle.
Private clickOffsetX, clickOffsetY As Integer
Private Sub Button_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
selectedButton = CType(sender, Button)
If e.Button = MouseButtons.Left Then
clickOffsetX = e.X
clickOffsetY = e.Y
'Dragging
isDragging = True
End If
End Sub
Private Sub Button_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
selectedButton = CType(sender, Button)
If isDragging Then
' deplace button.
selectedButton.Left = e.X + selectedButton.Left - clickOffsetX
selectedButton.Top = e.Y + selectedButton.Top - clickOffsetY
'boucle
For Each item As Button In listeButtons
If item Is selectedButton Then Continue For
If Collision(selectedButton, item) Then
SetLocation(selectedButton, item)
End If
Next
End If
End Sub
Private Sub Button_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
isDragging = False
selectedButton = Nothing
End Sub
Public Function Collision(ByVal selBtn As Button, ByVal itemBtn As Button) As Boolean
If selBtn.Bounds.IntersectsWith(itemBtn.Bounds) Then
Return True
End If
Return False
End Function
Public Sub SetLocation(ByVal selBtn As Button, ByVal itemBtn As Button)
' positionnement à gauche
Dim msg As String = String.Empty
If selBtn.Bounds.IntersectsWith(itemBtn.Bounds) Then
selBtn.Left = itemBtn.Left - selBtn.Width
selBtn.Top = itemBtn.Top
msg = "gauche"
End If
' ou à droite positionnement droite
If selBtn.Bounds.IntersectsWith(itemBtn.Bounds) Then
selBtn.Left = itemBtn.Right
selBtn.Top = itemBtn.Top
msg = "droite"
End If
Me.ToolTip1.SetToolTip(selBtn, msg)
End Sub
End Class |
Partager