End ClassDevComponents besitze ich nicht!
Hier ein Winforms-Beispiel:
Public Class frmMove
Dim Moveables As New List(Of Control)
Private Sub FillMoveable()
Dim cWidth As Integer = 60, cheight As Integer = 30
For i As Integer = 1 To 5
For k As Integer = 1 To 5
Moveables.Add(New Button With _
{.Left = (i - 1) * CInt(1.2 * cWidth), _
.Top = (k - 1) * CInt(1.2 * cheight), _
.Width = cWidth, .Height = cheight, _
.Text = i.ToString + k.ToString,
.Parent = Me})
AddHandler Moveables(Moveables.Count - 1).MouseDown, _
AddressOf moveable_mousedown
AddHandler Moveables(Moveables.Count - 1).MouseUp, _
AddressOf moveable_mouseup
Next k
Next i
End Sub
Private Sub moveable_mousedown(sender As Object, e As MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Right Then
xmove = e.X : ymove = e.Y
End If
End Sub
Private Sub moveable_mouseup(sender As Object, e As MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Right Then
moveit(e.X - xmove, e.Y - ymove)
xmove = -1 : ymove = -1
End If
End Sub
Private Sub frmMove_Load(sender As System.Object, _
e As System.EventArgs) Handles MyBase.Load
FillMoveable()
End Sub
Dim xmark% = -1, ymark% = -1, mark_rect As Rectangle
Dim mark_color As Color = Me.BackColor
Dim xmove% = -1, ymove% = -1
Private Sub frmMove_MouseDown(sender As Object, _
e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
xmark = e.X : ymark = e.Y : mark_color = Color.Red
ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
xmove = e.X : ymove = e.Y
End If
End Sub
Private Sub frmMove_MouseMove(sender As Object, _
e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim w, h, t, l As Integer
w = Math.Abs(xmark - e.X)
h = Math.Abs(ymark - e.Y)
l = Math.Min(xmark, e.X)
t = Math.Min(ymark, e.Y)
mark_rect = New Rectangle(l, t, w, h)
If xmark >= 0 Then
Me.Invalidate()
Mark_Moveables()
End If
End If
End Sub
Private Sub Mark_Moveables()
For i As Integer = 0 To Moveables.Count - 1
Dim control_rect As New Rectangle(Moveables(i).Location, Moveables( _
i).Size)
Dim r As Rectangle = Rectangle.Intersect(control_rect, mark_rect)
If Not (r.Width = 0 Or r.Height = 0) Then
If Moveables(i).Tag Is Nothing Then
Moveables(i).Tag = Moveables(i).BackColor
Moveables(i).BackColor = Color.Blue
End If
ElseIf Moveables(i).Tag IsNot Nothing Then
Moveables(i).BackColor = DirectCast(Moveables(i).Tag, Color)
Moveables(i).Tag = Nothing
End If
Next i
End Sub
Private Sub moveit(dx As Integer, dy As Integer)
For i As Integer = 0 To Moveables.Count - 1
If Moveables(i).Tag IsNot Nothing Then
Moveables(i).Top += dy
Moveables(i).Left += dx
End If
Next i
Mark_Moveables()
End Sub
Private Sub frmMove_MouseUp(sender As Object, _
e As System.Windows.Forms.MouseEventArgs) _
Handles Me.MouseUp
If e.Button = Windows.Forms.MouseButtons.Left Then
mark_color = Me.BackColor
xmark = -1 : ymark = -1 : mark_rect = Nothing
Me.Invalidate()
ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
moveit(e.X - xmove, e.Y - ymove)
xmove = -1 : ymove = -1
End If
End Sub
Private Sub frmMove_Paint(sender As Object, _
e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
If xmark > 0 Then
Using p As New Pen(mark_color)
e.Graphics.DrawRectangle(p, mark_rect)
End Using
End If
End Sub
Beitrag wurde zuletzt am 28.11.17 um 13:24:39 editiert. |