允许最终用户移动控件

发布于 2024-10-30 23:12:58 字数 739 浏览 1 评论 0原文

我找到了一个很好的示例 这里,但我有一些问题。 1. 由于控件太大,没有将控件放置到鼠标离开的正确位置。

  1. 它可以被推出屏幕。我希望它应该保持在屏幕边界内。

这是我的代码:

   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

I found a good sample Here, but I have a few problems with it.
1. It is not placing the control to the correct position where the mouse left off becase the control is a big one.

  1. It could be pushed out of the screen.. I want it should stay within the screen boundaries.

This is my code:

   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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦回旧景 2024-11-06 23:12:58

我写了这样的东西来在网页上拖动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) 或任何其他控件来使其可拖动!

编辑:两个示例现在都可以防止控件被拖出边界。

I wrote something like this for dragging divs on a webpage...

The general approach was to save the coordinates on mousedown, get the coordinates on mouseup, and shift the object's location by the difference.

Here's some example code:

I made a DragInfo class that keeps the initial mouse coords and initial location. I then store one of these guys in the control's Tag on the mousedown event:

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

My test control is just a panel that I put from the toolbox. It could be anything I guess. Here are my mousedown, mousemove, and mouseup event handlers for the panel (Panel1):

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

There you go! That works. Note that the mousemove method checks if the control is within the form's clientrectangle.

Or, a more generic way to go:

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

Now you can just use MakeDraggable(Panel1) or any other control to make it draggable!

Edit: Both examples now keep the control from being dragged out of bounds.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文