How to track mouse click and drag events in VB.NET?

First, I want to know if the mouse is in a certain area. Then, I want to check if the mouse holds the left click. I want to check while the left button is not working, and I want to track the position of the mouse. And finally, check when the left button is released.

So, in short, where should I start tracking mouse events in my form?

+4
source share
4 answers

Generally speaking, when the mouse down event occurs, you will need to grab the mouse. You will then receive mouse movement events, even if the mouse leaves the area of ​​the control that captured the mouse. You can calculate the delta in mouse movement events. Drag and drop will occur for the first time when the delta exceeds the β€œdrag and drop” area specified by the system. When a mouse event is received, stop the drag operation.

On Windows Forms, view the MouseDown, MouseMove, and MouseUp events in the Control class. MouseEventArgs will contain the X / Y coordinates. To capture or release the mouse, set the Capture property to true or false, respectively. If you do not capture the mouse, you will not receive MouseMove or MouseUp events if the mouse extends beyond the bounds of the control.

Finally, to determine the minimum "distance", the mouse must be moved before starting the drag operation, look at the SystemInformation.DragSize property.

Hope this helps.

+4
source

This is simple code to 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(eX, eY)) 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(eX, eY)) 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 
+5
source

The only way to do this is through javascript.

This article will explain this to you. http://luke.breuer.com/tutorial/javascript-drag-and-drop-tutorial.aspx

0
source

It is clear that this is old, but I came across this message, looking at the same thing. I thought that there could be a real drag event, but I think not. Here is 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 

ContainerToolStripMenuItem from my ToolStrip, which adds a panel on the fly. MouseDragging - class level. Dragged like a charm. Also, do not use Cursor.Position , as it will return the position relative to your entire window, and not the form (or any other container you are in).

0
source

Source: https://habr.com/ru/post/1335198/


All Articles