vb.net 鼠标点击并按住事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19283450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mouse click and hold event?
提问by Milot25
Does anyone have an idea how to handle a mouse click and hold event in VB.NET?
有没有人知道如何在 VB.NET 中处理鼠标单击并保持事件?
Suppose I want to click and hold in a button and do some stuff behind the code like change the button BackColor or close a window faster( just like when you click in the ms office 2013 file menu then use a left-arrow to close that menu).
假设我想单击并按住一个按钮并在代码后面执行一些操作,例如更改按钮 BackColor 或更快地关闭窗口(就像单击 ms office 2013 文件菜单然后使用左箭头关闭该菜单一样)。
Hope you know what I mean
希望你明白我的意思
Thank you
谢谢
回答by Leopold Stotch
You could create a timer that is defined globally that begins when MouseDown is called, then ends on Mouse Up. You can then set a condition on how many milliseconds need to pass before you deem it a 'long click'. See example code below:
您可以创建一个全局定义的计时器,该计时器在调用 MouseDown 时开始,然后在 Mouse Up 时结束。然后,您可以设置一个条件,即在您将其视为“长按”之前需要经过多少毫秒。请参阅下面的示例代码:
Public Class Form1
Dim WithEvents timer As New Timer
Dim milliseconds As Integer
Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown
timer.Start()
End Sub
Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles MyBase.MouseUp
timer.Stop()
Label1.Text = "Button held down for: " & milliseconds & " milliseconds"
If milliseconds >= 10 then 'Mouse has been down for one second
DoSomething()
End If
End Sub
Private Sub EggTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer.Tick
milliseconds += 1
End Sub
End Class
回答by Jason Hughes
MouseDown is what you are looking for
MouseDown 正是您要找的

