我想拖动面板内部的控件。所以首先我尝试更改控制面板的父级,然后在拖动它之后鼠标向下形成。但它并没有顺利拖延。如果控件是在窗体中并且拖动控件它的平滑拖动有什么区别?怎么解决?。
我的代码是:
Private cachedMousePos As Point
Private cachedControlPos As Point
Private isMouseDown As Boolean
Private Sub HandleDraggableControlMouseDown(ByVal sender As Object, ByVal e As
MouseEventArgs) Handles Button1.MouseDown
Dim target As Control = TryCast(sender, Control)
If (Not target Is Nothing) Then
Dim pt As Point = Me.PointToClient(target.PointToScreen(Point.Empty))
target.Dock = DockStyle.None
target.Parent = Me
target.BringToFront()
target.Location = pt
Me.isMouseDown = True
Me.cachedControlPos = pt
Me.cachedMousePos = Control.MousePosition
End If
End Sub
Private Sub HandleDraggableControlMouseMove(ByVal sender As Object, ByVal e As
MouseEventArgs) Handles Button1.MouseMove
If (Me.isMouseDown) Then
Dim target As Control = TryCast(sender, Control)
If (Not target Is Nothing) Then
Dim x As Integer = (Me.cachedControlPos.X + (Control.MousePosition.X -
Me.cachedMousePos.X))
Dim y As Integer = (Me.cachedControlPos.Y + (Control.MousePosition.Y -
Me.cachedMousePos.Y))
target.Location = New Point(x, y)
'c2 = (c1 + (m2 - m1))
End If
End If
End Sub
Private Sub HandleDraggableControlMouseUp(ByVal sender As Object, ByVal e As
MouseEventArgs) Handles Button1.MouseUp
Me.cachedControlPos = Point.Empty
Me.cachedMousePos = Point.Empty
Me.isMouseDown = False
End Sub