HOOK钩子函数求教高手:

ahbc123 2004-03-08 03:04:34
HOOK钩子函数求教高手:
FORM1:
Private Sub Form_Load()
Move 0, 0, Screen.Width, Screen.Height '让form1全屏显示
'Hidemouse '隐藏鼠标

ShockwaveFlash1.Top = Form1.ScaleTop
ShockwaveFlash1.Left = Form1.ScaleLeft
ShockwaveFlash1.Width = Form1.ScaleWidth
ShockwaveFlash1.Height = Form1.ScaleHeight

ShockwaveFlash1.Movie = "C:\My Documents\db.swf"
Call EnableKBDHook
End Sub


Private Sub Form_Unload(Cancel As Integer)
Call UnHookKBD
End Sub

模块:
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
Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Long) As Long
Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, _
ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long

Public hnexthookproc As Long
Public Const HC_ACTION = 0
Public Const WH_KEYBOARD = 2

Public Sub UnHookKBD()
If hnexthookproc <> 0 Then
UnhookWindowsHookEx hnexthookproc
hnexthookproc = 0
End If
End Sub
Public Function EnableKBDHook()
If hnexthookproc <> 0 Then
Exit Function
End If
hnexthookproc = SetWindowsHookEx(WH_KEYBOARD, AddressOf _
MyKBHFunc, App.hInstance, 0)

If hnexthookproc <> 0 Then
EnableKBDHook = hnexthookproc
End
End If
End Function
Public Function MyKBHFunc(ByVal iCode As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
'这三个叁数是固定的,不能动,而MyKBHFunc这个名称只要和
'SetWindowsHookex()中 AddressOf後的名称一样便可,不一定叫什麽
'wParam 是传入按了哪个key的virtual-key code

'如果您将以下的两行unmark则所有键盘的输入皆没有作用
MyKBHFunc = 1 '吃掉讯息

Exit Function

' MyKBHFunc = 0 '讯息要处理
' If iCode < 0 Then
' ' MyKBHFunc = CallNextHookEx(hnexthookproc, iCode, wParam, lParam)
' Exit Function
' End If
' If wParam = vbKeySnapshot Then '侦测 有没有按到PrintScreen键
' MyKBHFunc = 1 '在这个Hook便吃掉这个讯息
' Debug.Print "haha"
' Else
' Call CallNextHookEx(hnexthookproc, iCode, wParam, lParam)
' End If
End Function

1: 在上面代码里,如何能检测到鼠标的活动
2: 如何使在键盘,鼠标没有活动的情况下,使FLASH能正常播放(我的上面代码,一运行,就关闭了,谢谢!!!!
...全文
59 点赞 收藏 6
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
pigpag 2004-03-16
先做一个标记即可,因为程序已启动马上就会调用一次MouseMove的。
Private iX As Single, iY As Single, bNotInit As Boolean

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Not bNotInit Then
iX = X
iY = Y
bNotInit = True
Exit Sub
End If
If Abs(iX-X)<60 And Abs(iY-Y)<60 Then Exit Sub '微小移动忽略不计
Load Form2
DoEvents
ReleaseCapture
End Sub
回复
ahbc123 2004-03-16
pigpag(噼里啪啦 - 努力开发狂看书) :
谢谢您,谢谢!!!
但有一个问题:
如何捕捉鼠标移动信息,我用
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Load Form2
DoEvents
ReleaseCapture
End Sub
form2:
Private Sub Form_Load()
End '必须要得
End Sub

程序只要一启动,就退出.,我想要得结果是,程序运行后,监测鼠标是否移动,如移动鼠标,就退出,如鼠标未移动,程序就一直运行.
请你再次予以帮助,谢谢!!!!
回复
pigpag 2004-03-16
1. SetCapture和ReleaseCapture是API,要声明一下:)
2. ReleaseCapture是取消SetCapture的作用。
回复
pigpag 2004-03-16
好像有点小题大做了。

试试用SetCapture。

Private Sub Form_Load()
SetCapture Me.hWnd
End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Msgbox "按下"
ReleaseCapture
End Sub

这样即使在窗体外按下鼠标,也会出发MouseDown事件
回复
kmzs 2004-03-16
一定要允许鼠标微小移动,你可以找到几个vb做的屏保去参考一下
回复
ahbc123 2004-03-16
jlctt(金) :高手,我用您的方法无法实现得到鼠标消息,我想把程序发给您看看,好吗,如行,请您给个邮箱,我将程序发送给您,谢谢!!!!
回复
相关推荐
发帖
VB基础类
创建于2007-09-28

7518

社区成员

VB 基础类
申请成为版主
帖子事件
创建了帖子
2004-03-08 03:04
社区公告
暂无公告