简体   繁体   English

如何在VB.NET中跟踪鼠标单击和拖动事件?

[英]How can I track mouse click and drag events in VB.NET?

First, I want to know if the mouse is in some area. 首先,我想知道鼠标是否在某个区域。 Then, I want to check if the mouse holds the left click. 然后,我想检查鼠标是否按住左键。 I want to check as long as the left button is down, and I want to track the mouse's position. 我想检查只要左按钮关闭,我想跟踪鼠标的位置。 And finally, check when the left button is released. 最后,检查左按钮何时释放。

So, in short, where should I start for tracking mouse events in my form? 那么,简而言之,我应该从哪里开始跟踪表单中的鼠标事件?

This is a simple code for detect Drag or Click 这是一个用于检测拖动或单击的简单代码

Public IsDragging As Boolean = False, IsClick As Boolean = False
Public StartPoint, FirstPoint, LastPoint As Point
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picBook.Click
    If IsClick = True Then MsgBox("CLick")
End Sub

Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picBook.MouseDown
    StartPoint = picBook.PointToScreen(New Point(e.X, e.Y))
    FirstPoint = StartPoint
    IsDragging = True
End Sub

Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picBook.MouseMove
    If IsDragging Then
        Dim EndPoint As Point = picBook.PointToScreen(New Point(e.X, e.Y))
        IsClick = False
        picBook.Left += (EndPoint.X - StartPoint.X)
        picBook.Top += (EndPoint.Y - StartPoint.Y)
        StartPoint = EndPoint
        LastPoint = EndPoint
    End If
End Sub

Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picBook.MouseUp
    IsDragging = False
    If LastPoint = StartPoint Then IsClick = True Else IsClick = False
End Sub

Generally speaking, when the mouse down event occurs, you would need to capture the mouse. 一般来说,当发生鼠标按下事件时,您需要捕获鼠标。 Then you will receive mouse move events even if the mouse leaves the area of the control that has captured the mouse. 然后,即使鼠标离开捕获鼠标的控件区域,您也会收到鼠标移动事件。 You can calculate delta's in the mouse move events. 您可以在鼠标移动事件中计算delta。 A drag would occur the first time the delta exceeds a system-defined "drag area". 第一次增量超过系统定义的“拖动区域”时会发生拖动。 When the mouse up event is received, stop the drag operation. 收到鼠标按下事件后,停止拖动操作。

In Windows Forms, look at the MouseDown, MouseMove, and MouseUp events on the Control class. 在Windows窗体中,查看Control类上的MouseDown,MouseMove和MouseUp事件。 The MouseEventArgs will contain the X/Y coordinates. MouseEventArgs将包含X / Y坐标。 To capture or release the mouse, set the Capture property to true or false respectively. 要捕获或释放鼠标,请分别将Capture属性设置为true或false。 If you do not capture the mouse, then you will not receive the MouseMove or MouseUp events if the mouse is released outside of the bounds of the control. 如果您没有捕获鼠标,那么如果鼠标被释放到控件的边界之外,则不会收到MouseMove或MouseUp事件。

Finally, to determine the minimum "distance" the mouse should be allowed to move before starting the drag operation, look at the SystemInformation.DragSize property. 最后,要确定在开始拖动操作之前应允许鼠标移动的最小“距离”,请查看SystemInformation.DragSize属性。

Hope this helps. 希望这可以帮助。

Understandably this is old, but I ran across this post while looking to do the same thing. 可以理解这是旧的,但我在寻找同样的事情时遇到过这篇文章。 I thought there might be an actual drag event, but I guess not. 我以为可能会有一个实际的拖拽事件,但我猜不是。 Here's how I did it. 这就是我做到的。

Private Sub ContainerToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ContainerToolStripMenuItem.Click
    Dim pnl As New Panel
    pnl.Size = New Size(160, 160)
    pnl.BackColor = Color.White
    AddHandler pnl.MouseDown, AddressOf Control_DragEnter
    AddHandler pnl.MouseUp, AddressOf Control_DragLeave
    AddHandler pnl.MouseMove, AddressOf Control_Move
    Me.Controls.Add(pnl)
End Sub

Private Sub Control_DragEnter(ByVal sender As Object, ByVal e As EventArgs)
    MouseDragging = True
End Sub

Private Sub Control_DragLeave(ByVal sender As Object, ByVal e As EventArgs)
    MouseDragging = False
End Sub

Private Sub Control_Move(ByVal sender As Object, ByVal e As EventArgs)
    If MouseDragging = True Then
        sender.Location = Me.PointToClient(Control.MousePosition)
    End If
End Sub

The ContainerToolStripMenuItem is from my ToolStrip that adds a Panel on-the-fly. ContainerToolStripMenuItem来自我的ToolStrip,可以即时添加Panel。 MouseDragging is class level. MouseDragging是类级别。 Drags like a charm. 拖拉机就像一个魅力。 Also, don't use Cursor.Position as it will return the position relative to your entire Window, not the Form (or whatever container it is you're in). 另外,不要使用Cursor.Position ,因为它将返回相对于整个Window的位置,而不是Form(或者你所在的任何容器)。

The only way to go about doing this is via javascript. 这样做的唯一方法是通过javascript。

This article will explain it to you. 本文将向您解释。 http://luke.breuer.com/tutorial/javascript-drag-and-drop-tutorial.aspx http://luke.breuer.com/tutorial/javascript-drag-and-drop-tutorial.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM