Ezi*_*Ezi 2 vb.net user-interface end-user
我在这里找到了一个很好的样本,但我遇到了一些问题.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
Run Code Online (Sandbox Code Playgroud)
我写了这样的东西来拖动网页上的div ...
一般方法是保存mousedown上的坐标,获取mouseup上的坐标,并根据差异移动对象的位置.
这是一些示例代码:
我做了一个DragInfo保持初始鼠标坐标和初始位置的课程.然后我将其中一个人存放Tag在mousedown事件的控件中:
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
Run Code Online (Sandbox Code Playgroud)
我的测试控件只是我从工具箱中放入的一个面板.它可能是我猜的任何东西.这是面板(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
Run Code Online (Sandbox Code Playgroud)
你去!这样可行.请注意,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
Run Code Online (Sandbox Code Playgroud)
现在你可以使用MakeDraggable(Panel1)或任何其他控件使其可拖动!
编辑:现在这两个示例都使控件不被拖出范围.
| 归档时间: |
|
| 查看次数: |
9556 次 |
| 最近记录: |