允许最终用户移动控件

时间:2011-04-06 17:09:42

标签: vb.net user-interface end-user

我发现了一个很好的样本Here,但我遇到了一些问题。  1.由于控制器很大,所以没有将控制器放在鼠标离开的正确位置。

  1. 它可以被推出屏幕..我希望它应该保持在屏幕边界内。
  2. 这是我的代码:

       Public Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyControl.MouseMove
        If Not _capturingMoves Then
            Return
        End If
        X = e.X
        Y = e.Y
    End Sub
    
    Public Sub Form1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyControl.MouseUp
        If _capturingMoves Then
            ' Do any final placement
            MyControl.Location = New Point(X, Y)
            _capturingMoves = False
        End If
    End Sub
    

1 个答案:

答案 0 :(得分:6)

我在网页上拖了div来写这样的东西......

一般方法是保存mousedown上的坐标,获取mouseup上的坐标,并按差异移动对象的位置。

以下是一些示例代码:

我创建了一个DragInfo类来保持初始鼠标坐标和初始位置。然后我将其中一个人存放在mousedown事件的控件Tag中:

Public Class DragInfo
    Public Property InitialMouseCoords As Point
    Public Property InitialLocation As Point

    Public Sub New(ByVal MouseCoords As Point, ByVal Location As Point)
        InitialMouseCoords = MouseCoords
        InitialLocation = Location
    End Sub

    Public Function NewLocation(ByVal MouseCoords As Point) As Point
        Dim loc As New Point(InitialLocation.X + (MouseCoords.X - InitialMouseCoords.X), InitialLocation.Y + (MouseCoords.Y - InitialMouseCoords.Y))
        Return loc
    End Function
End Class

我的测试控件只是我从工具箱中放入的面板。它可能是我猜的任何东西。以下是面板(Panel1)的mousedown,mousemove和mouseup事件处理程序:

Private Sub Panel1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
    Panel1.Tag = New DragInfo(Form.MousePosition, Panel1.Location)
End Sub

Private Sub Panel1_MouseMove(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
    If Panel1.Tag IsNot Nothing Then
        Dim info As DragInfo = CType(Panel1.Tag, DragInfo)
        Dim newLoc As Point = info.NewLocation(Form.MousePosition)
        If Me.ClientRectangle.Contains(New Rectangle(newLoc, Panel1.Size)) Then Panel1.Location = newLoc
    End If
End Sub

Private Sub Panel1_MouseUp(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp
    Panel1.Tag = Nothing
End Sub
你去吧!这样可行。请注意,mousemove方法检查控件是否在表单的clientrectangle中。

或者,更通用的方法:

Private Sub MakeDraggable(ByVal Control As Control)
    AddHandler Control.MouseDown, Sub(sender As Object, e As MouseEventArgs) StartDrag(Control)
    AddHandler Control.MouseMove, Sub(sender As Object, e As MouseEventArgs) Drag(Control)
    AddHandler Control.MouseUp, Sub(sender As Object, e As MouseEventArgs) StopDrag(Control)
End Sub
Private Sub StartDrag(ByVal Control As Control)
    Control.Tag = New DragInfo(Form.MousePosition, Control.Location)
End Sub
Private Sub Drag(ByVal Control As Control)
    If Control.Tag IsNot Nothing AndAlso TypeOf Control.Tag Is DragInfo Then
        Dim info As DragInfo = CType(Control.Tag, DragInfo)
        Dim newLoc As Point = info.NewLocation(Form.MousePosition)
        If Me.ClientRectangle.Contains(New Rectangle(newLoc, Control.Size)) Then Control.Location = newLoc
    End If
End Sub
Private Sub StopDrag(ByVal Control As Control)
    Control.Tag = Nothing
End Sub

现在您可以使用MakeDraggable(Panel1)或任何其他控件使其可拖动!

编辑:现在这两个示例都使控件不会被拖出界限。