sd007 发表于 2009-3-23 21:51:30

关于截获鼠标消息的钩子耗系统资源的问题

本帖最后由 sd007 于 2009-5-16 21:36 编辑

截获鼠标消息可以用两种方法:(以下两个例子都是超级版主pcbar发的)
1.用SetWindowsHookEx钩子技术,例子:http://www.autoitx.com/attachment.php?aid=410&k=10943da8290560d3f8ac93f8eed673e0&t=1237813540,这种方法在CPU资源紧张时(比如打开一个有flash动画的网页时),能明显感觉到鼠标移动反应有点迟钝,它的优点是可以中断鼠标消息的传递

2.用hook.dll,例子:http://www.autoitx.com/attachment.php?aid=412&k=8782fa6495d6c50b1ffbb0e7141d1bd6&t=1237813540,用这种方法在CPU资源紧张时,不会感觉到鼠标移动反应有迟钝,但好像不能中断鼠标消息的传递。

请问用什么方法能截获鼠标消息,并中断消息的传递,同时又不会耗CPU资源?

我的系统是WIN2003,CPU:P4 3.3G,内存:2G

[ 本帖最后由 sd007 于 2009-3-23 21:55 编辑 ]

ceoguang 发表于 2009-3-23 23:32:37

第一个问题:
反应快与慢是看SetWindowsHookEx执行后与CallNextHookEx执行前的代码,当中的代码执行肯定需要些时间,你说的在CPU吃紧时的问题是存在的
第二:
如果要截获的话应该是在收到消息后不执行CallNextHookEx就可以了吧,至少我是这样理解的.
BTW:以上仅为新手意见,希望不会误导任何人.

sd007 发表于 2009-3-24 00:12:37

回复 2# ceoguang 的帖子

1.SetWindowsHookEx执行后与CallNextHookEx执行前,之间就是没有执行代码,鼠标移动反应还是一样有点迟钝
2..用hook.dll没有CallNextHookEx函数

akmm88 发表于 2009-3-26 02:10:31

AU3的执行速度只能这样.

sd007 发表于 2009-4-14 16:42:50

用第2种方法丝毫不会感觉到鼠标移动反应有迟钝,有没有hook.dll的相关资料?用hook.dll能中断鼠标消息的传递吗?
我把应用hook.dll的源码贴出来,请指教:
Const $WH_KEYBOARD = 2
Const $WH_CBT = 5
Const $WH_MOUSE = 7

Const $WM_AUTOITLBUTTONDOWN = 0x1400 + 0x0A30
Const $WM_AUTOITRBUTTONDOWN = 0x1400 + 0x0A31
Const $WM_AUTOITMBUTTONDOWN = 0x1400 + 0x0A32
Const $WM_AUTOITXBUTTONDOWN1 = 0x1400 + 0x0A33
Const $WM_AUTOITXBUTTONDOWN2 = 0x1400 + 0x0A34
Const $WM_AUTOITLBUTTONUP = 0x1400 + 0x0B30
Const $WM_AUTOITRBUTTONUP = 0x1400 + 0x0B31
Const $WM_AUTOITMBUTTONUP = 0x1400 + 0x0B32
Const $WM_AUTOITXBUTTONUP1 = 0x1400 + 0x0B33
Const $WM_AUTOITXBUTTONUP2 = 0x1400 + 0x0B34
Const $WM_AUTOITLDBLCLK = 0x1400 + 0x0C30
Const $WM_AUTOITRDBLCLK = 0x1400 + 0x0C31
Const $WM_AUTOITMDBLCLK = 0x1400 + 0x0C32
Const $WM_AUTOITXDBLCLK1 = 0x1400 + 0x0C33
Const $WM_AUTOITXDBLCLK2 = 0x1400 + 0x0C34
Const $WM_AUTOITMOUSEWHEELUP = 0x1400 + 0x0D30
Const $WM_AUTOITMOUSEWHEELDOWN = 0x1400 + 0x0D31
Const $WM_AUTOITMOUSEMOVE = 0x1400 + 0x0F30

Const $HCBT_SETFOCUS = 0x1400 + 0x1A30
Const $HCBT_ACTIVATE = 0x1400 + 0x1A31
Const $HCBT_CREATEWND = 0x1400 + 0x1A32
Const $HCBT_DESTROYWND = 0x1400 + 0x1A33
Const $HCBT_MINMAX = 0x1400 + 0x1A34

Const $WM_KEYDOWN = 0x0400 + 0x0A30
Const $WM_KEYUP = 0x0400 + 0x0A31

Global $n,$msg,$buffer = ""

Global $mouseldown=0

HotKeySet("{ESC}","GoAway")
TrayTip("","ESC键退出!!",8)
$gui = GUICreate("test")

Global $DLLinst = DLLCall("kernel32.dll","hwnd","LoadLibrary","str",".\hook.dll")
Global $mouseHOOKproc = DLLCall("kernel32.dll","hwnd","GetProcAddress","hwnd",$DLLInst,"str","MouseProc")

Global $hhMouse = DLLCall("user32.dll","hwnd","SetWindowsHookEx","int",$WH_MOUSE, _
      "hwnd",$mouseHOOKproc,"hwnd",$DLLinst,"int",0)


DLLCall(".\hook.dll","int","SetValuesMouse","hwnd",$gui,"hwnd",$hhMouse)

GUIRegisterMsg($WM_AUTOITLDBLCLK,"myfunc")
GUIRegisterMsg($WM_AUTOITLBUTTONDOWN,"ldown")
GUIRegisterMsg($WM_AUTOITLBUTTONUP,"lup")
GUIRegisterMsg($WM_AUTOITRBUTTONDOWN,"rdown")
GUIRegisterMsg($WM_AUTOITRBUTTONUP,"rup")

While 1
    $msg = GUIGetMsg()
    If $msg = -3 Then ExitLoop
WEnd

Func MyFunc($hWndGUI, $MsgID, $WParam, $LParam)
                TrayTip("","您刚才双击了鼠标左键.",5)
EndFunc

Func GoAway()
    Exit
EndFunc

Func OnAutoItExit()
    DLLCall("user32.dll","int","UnhookWindowsHookEx","hwnd",$hhMouse)
    DLLCall("kernel32.dll","int","FreeLibrary","hwnd",$DLLinst)
EndFunc

Func ldown()
      ToolTip("左键按下")
EndFunc
Func lup()
ToolTip("左键弹起")
EndFunc

Func rdown()
      ToolTip("右键按下")
EndFunc
Func rup()
ToolTip("右键弹起")
EndFunc

sd007 发表于 2009-5-16 21:34:46

自行解决,详见:http://www.autoitx.com//forum.php?mod=viewthread&tid=7092

asdasdasd 发表于 2011-7-31 15:30:45

谢谢分享。。。

qtyscaicai 发表于 2013-2-11 20:06:23

不错,很好的思路!谢谢

ncxj 发表于 2015-12-29 15:48:54

鼠标钩子,很复杂的样子,mark
页: [1]
查看完整版本: 关于截获鼠标消息的钩子耗系统资源的问题