vb监控鼠标状态,在屏幕任何地方单击鼠标,text1就会添加此时的时间?

 我来答
greystar_cn
2015-07-13 · 知道合伙人软件行家
greystar_cn
知道合伙人软件行家
采纳数:16407 获赞数:17260
本人主要从事.NET C#方向的技术开发工作,具有10多年的各类架构开发工作经验。

向TA提问 私信TA
展开全部
'==================窗体代码=================
Private Sub Form_Load()
hHook = SetWindowsHookEx(WH_MOUSE_LL, AddressOf MouseHookProc, App.hInstance, 0)
If hHook <> 0 Then
MsgBox "已钩住鼠标事件,请按本窗体右上方的X按钮关闭窗体,不要使用VB调试工具栏的“结束”按钮,以免VB崩溃"
End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
UnhookWindowsHookEx hHook
End Sub

'=============模块代码==================
Public Const WH_MOUSE = 7 '本地钩子
Public Const WH_MOUSE_LL = 14 '全局钩子
Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Public Const WM_LBUTTONDOWN = &H201 '窗口中按下鼠标左键
Public Const WM_LBUTTONUP = &H202 '窗口中松开鼠标左键
Public Const WM_MOUSEMOVE = &H200 '窗口中移动鼠标
Public Const WM_RBUTTONDOWN = &H204 '窗口中按下鼠标右键
Public Const WM_RBUTTONUP = &H205 '窗口中松开鼠标右键
Public Const WM_MOUSEWHEEL = &H20A '鼠标滚轮
Public Const WM_NCLBUTTONDOWN = &HA1 '窗口标题栏中按下鼠标左键
Public Const WM_NCLBUTTONUP = &HA2 '窗口标题栏中左开鼠标左键
Public Const WM_NCMOUSEMOVE = &HA0 '窗口标题栏中移动鼠标
Public Const WM_NCRBUTTONDOWN = &HA4 '窗口标题栏中按下鼠标右键
Public Const WM_NCRBUTTONUP = &HA5 '窗口标题栏中松开鼠标右键

Public hHook As Long
Public Type POINTAPI
x As Long
y As Long
End Type

Type MSLLHOOKSTRUCT
pt As POINTAPI '相对于屏幕左上角的坐标x,y
mouseData As Long '鼠标数据
flags As Long '标记
time As Long '时间戳
dwExtraInfo As Long '其他信息
End Type

Type MOUSEHOOKSTRUCT
pt As POINTAPI '相对于屏幕左上角的坐标x,y
hwnd As Long '鼠标光标下窗口的句柄
wHitTestCode As Long '鼠标光标在窗口中的位置,标题栏、左边框、右边框,下边框。。。
dwExtraInfo As Long '其他信息,通常为0
End Type
Dim oMouseHookStruct As MSLLHOOKSTRUCT

Public Function MouseHookProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
CopyMemory oMouseHookStruct, ByVal lParam, Len(oMouseHookStruct)
Debug.Print "当前鼠标位置-x:" & oMouseHookStruct.pt.x & "; y:" & oMouseHookStruct.pt.y
Select Case wParam
Case WM_LBUTTONDOWN, WM_NCLBUTTONDOWN
Debug.Print "左键按下"
Case WM_LBUTTONUP, WM_NCLBUTTONUP
Debug.Print "左键弹起"
Case WM_RBUTTONDOWN, WM_NCRBUTTONDOWN
Debug.Print "右键按下"
Case WM_RBUTTONUP, WM_NCRBUTTONUP
Debug.Print "右键弹起"
Case WM_MOUSEMOVE, WM_NCMOUSEMOVE
Debug.Print "鼠标移动"
Case WM_MOUSEWHEEL
Debug.Print "鼠标滚轮"
End Select
MouseHookProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
End Function
追问
能不用钩子吗
微测检测5.10
2023-05-10 广告
您好!建议咨 深圳市微测检测有限公司,已建立起十余个专业实验室,企业通过微测检测就可以获得一站式的测试与认 证解决方案;(EMC、RF、MFi、BQB、QI、USB、安全、锂电池、快充、汽车电子EMC、汽车手机互 联、语音通话质量),认证遇... 点击进入详情页
本回答由微测检测5.10提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式