Kan ingewikkelder, zie maar : Omdat ik dit een uiterst interessante vraagstuk vond ben ik hiermee doorgegaan. Daarom presenteer ik dan ook een nieuw stukje software waarbij je verschillende controls kunt selecteren en verplaatsen. Ik heb dit getest met 1 button, 3 tekstvelden en een label op 1 form. Voor alle gestresseerde :
Visual Basic Code:
Public Class Form1
Private MouseX, MouseY As Integer
Private ControlSelected As Boolean = False
Private HoverOverControl As Boolean = False
Private HoverTick As Integer = 0
Private SelectedControl As SelectedControlProp
Private Structure SelectedControlProp
Dim Name As String
Dim X As Integer
Dim Y As Integer
Dim Width As Integer
Dim Height As Integer
End Structure
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mousePos.Start()
End Sub
Private Sub Form1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If ControlSelected = True Then
MouseX = e.X
MouseY = e.Y
Me.Refresh()
HighLightControl(MouseX, MouseY, SelectedControl.Width, SelectedControl.Height, Pens.Red)
End If
End Sub
Private Sub Form1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
If ControlSelected = True Then
Dim xControl As Control
For Each xControl In Me.Controls
If xControl.Name = SelectedControl.Name Then
xControl.Location = New Point(MouseX, MouseY)
End If
Next
Me.Refresh()
ControlSelected = False
HoverOverControl = False
mousePos.Start()
End If
End Sub
Private Sub Hover_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HoverMouse.Tick
HoverTick += 1
If HoverTick = 7 Then
HoverMouse.Stop()
mousePos.Stop()
ControlSelected = True
HighLightControl(SelectedControl.X, SelectedControl.Y, SelectedControl.Width, SelectedControl.Height, Pens.LawnGreen)
HoverTick = 0
End If
End Sub
Private Sub HighLightControl(ByVal X As Integer, ByVal Y As Integer, ByVal Width As Integer, ByVal Height As Integer, ByVal Color As Pen)
Dim G As Graphics = Graphics.FromHwnd(ActiveForm.Handle)
G.DrawRectangle(Color, X - 1, Y - 1, Width + 1, Height + 1)
G.Flush()
G.Dispose()
End Sub
Private Sub mousePos_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mousePos.Tick
MouseX = Windows.Forms.Cursor.Position.X - (Me.Location.X + 4)
MouseY = Windows.Forms.Cursor.Position.Y - (Me.Location.Y + 22)
If ControlSelected = False Then CheckMousePosition(MouseX, MouseY)
End Sub
Private Sub CheckMousePosition(ByVal X As Integer, ByVal Y As Integer)
Dim xControl As Control
For Each xControl In Me.Controls
If MouseX > xControl.Location.X And MouseX < (xControl.Location.X + xControl.Size.Width) Then
If MouseY > xControl.Location.Y And MouseY < (xControl.Location.Y + xControl.Size.Height) Then
If HoverOverControl = False Then
HoverMouse.Start()
HoverOverControl = True
SelectedControl.Name = xControl.Name.ToString
SelectedControl.X = xControl.Location.X
SelectedControl.Y = xControl.Location.Y
SelectedControl.Width = xControl.Width
SelectedControl.Height = xControl.Height
HighLightControl(SelectedControl.X, SelectedControl.Y, SelectedControl.Width, SelectedControl.Height, Pens.Orange)
End If
Exit Sub
End If
End If
Next
HoverMouse.Stop()
HoverOverControl = False
HoverTick = 0
Me.Refresh()
End Sub
End Class
Werkt hetzelfde zoals hierboven staat beschreven.
Je zou dit ook in een class moeten kunnen stoppen, maar daar heb ik geen tijd voor gehad.