问题描述
我不想使用 mouseup 事件,因为我想允许用户拖动到控件之外.我需要通过计时器检查状态.我依稀记得以前使用
I don't want to use the mouseup event because I want to allow the user to drag outside the control. I need to check the state via a timer. I vaguely remember doing this before at some point by using
If MouseButtons = MouseButtons.Left Then...
但现在它说 MouseButtons 是一种类型,不能用作表达式(这是真的,它是一个枚举类型).
But now it says that MouseButtons is a type and cannot be used as an expression (which is true, its an enum type).
也许他们改变了一些东西,或者也许我只是记错了......不管怎样,我如何检查按钮是否仍然按下?
Maybe they changed things or maybe I just remember wrong.. either way, how would I check if the button is still down?
推荐答案
这是经过试验和测试的.我创建了一个新类:testmouseclass 并创建了一个共享函数,您可以随时使用它来确定 LeftMouseButton
是否已关闭.这在 GetAsyncKeyState
调用的帮助下是可能的.
This is tried and tested. I created a new class: testmouseclass and created a shared function you can use anytime to determine if the LeftMouseButton
is down. This is possible with the help of GetAsyncKeyState
call.
Imports System.Runtime.InteropServices 'Need to import...
Public Class testmouseclass
<DllImport("user32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)> Public Shared Function GetAsyncKeyState(ByVal vkey As Long) As Long
End Function
Public Shared Function LeftMouseIsDown() As Boolean
Return GetAsyncKeyState(Keys.LButton) > 0 And &H8000
End Function
End Class
示例用法
Private Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
If testmouseclass.LeftMouseIsDown() Then MsgBox("It IS!")
End Sub
根据计时器滴答,这也可能令人头疼.不知道你是如何使用它的,但在我的例子中,我有 3 秒的计时器,当我按下 LeftMouseButton
时,会弹出一条消息,当我再次单击它时,由于计时器和鼠标左键按下.此外,这项工作甚至在您的应用程序之外...
Depending on the timer tick's this can be a headache as well. Not sure how you are using this, but in my example I had the timer at 3 seconds and when I held the LeftMouseButton
down a message popped up and when I clicked it again it popped up again because of the timer and the left mouse was down. Also this work's even outside of your application...
这篇关于在 vb.net 中检测鼠标按钮是否按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!