A
avant
Unregistered / Unconfirmed
GUEST, unregistred user!
资料所见,多是钱达智keyboard hook的例子.我仿了一个CBT型的HOOK,<br>想监测所有的窗体Active事件(暂设为Beep一声),可是一运行...那位有经验<br>的大虾帮我调一调?或见赐一个相关的例子?分不够我可以再加!小弟的拙作附<br>下:<br>//DLL工程文件用到的单元<br>unit Hkwaproc;<br><br>interface<br><br>uses<br> Windows, Messages,SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> ExtCtrls, StdCtrls;<br><br>var<br> hNextHookProc: HHook;<br> procSaveExit: Pointer;<br><br><br>function WinSkbdHandler(nCode: Integer;wParam: WPARAM;<br> lParam:LPARAM): LRESULT; stdcall; export;<br>function EnableWAHook: BOOL; export;<br>function DisableWAHook: BOOL; export;<br>procedure WAHookExit; far;<br><br>implementation<br><br>function WinSkbdHandler(nCode: Integer;wParam: WPARAM;<br> lParam:LPARAM): LRESULT; stdcall; export;<br>begin<br> Result := 0;<br> if nCode < 0 Then<br> begin<br> Result := CallNextHookEx(hNextHookProc, nCode, wParam, lParam);<br> Exit;<br> end;<br> if nCode = HCBT_ACTIVATE then<br> begin<br> Result:=1;<br> beep;<br> //我待加的操作<br> end;<br>end;<br><br><br>function EnableWAHook: BOOL; export;<br>begin<br> Result := False;<br> if hNextHookProc <> 0 then Exit;<br> hNextHookProc:=SetWindowsHookEx(WH_CBT,WinSkbdHandler,HInstance,0);<br> Result := hNextHookProc <> 0;<br>end;<br><br><br>function DisableWAHook: BOOL; export;<br>begin<br> if hNextHookProc <> 0 then<br> begin<br> UnhookWindowshookEx(hNextHookProc);<br> hNextHookProc := 0;<br> MessageBeep(0);<br> MessageBeep(0);<br> end;<br> Result := hNextHookProc = 0;<br>end;<br><br><br>procedure WAHookExit;<br>begin<br> if hNextHookProc <> 0 then DisableWAHook;<br> ExitProc := procSaveExit;<br>end;<br><br>end.<br><br>//---------DLL工程文件<br>library HkWinActivate;<br><br>uses<br> Hkwaproc in 'Hkwaproc.pas';<br><br>exports<br> EnableWAHook,<br> DisableWAHook;<br><br>begin<br> hNextHookProc := 0;<br> procSaveExit := ExitProc;<br> ExitProc := @WAHookExit;<br>end.<br><br>//----------------测试用例<br>unit wademo;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> StdCtrls;<br><br>type<br> TForm2 = class(TForm)<br> Button1: TButton;<br> Button2: TButton;<br> procedure Button1Click(Sender: TObject);<br> procedure Button2Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form2: TForm2;<br><br>implementation<br><br>{$R *.DFM}<br>function EnableWAHook: BOOL; external 'HKWinACTIVATE.DLL';<br>function DisableWAHook: BOOL; external 'HKWinACTIVATE.DLL';<br><br>procedure TForm2.Button1Click(Sender: TObject);<br>begin<br> if EnableWAHook then<br> ShowMessage('WAHook 启动;');<br>end;<br><br>procedure TForm2.Button2Click(Sender: TObject);<br>begin<br> if DisableWAHook then<br> ShowMessage('WA Hook已解除!');<br>end;<br><br>end.<br>