200分求一个钩子函数 (200分)

  • 主题发起人 主题发起人 xiaodongxi
  • 开始时间 开始时间
X

xiaodongxi

Unregistered / Unconfirmed
GUEST, unregistred user!
200分求一个钩子函数 
 
unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; Button3: TButton;<br>&nbsp; &nbsp; Button4: TButton;<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; Edit2: TEdit;<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button3Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>&nbsp; EventArr:array[0..1000]of EVENTMSG;<br>&nbsp; EventLog:Integer;<br>&nbsp; PlayLog:Integer;<br>&nbsp; hHook,hPlay:Integer;<br>&nbsp; recOK:Integer;<br>&nbsp; canPlay:Integer;<br>&nbsp; bDelay:Bool;<br>implementation<br><br>{$R *.DFM}<br>Function PlayProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;<br>begin<br>&nbsp; canPlay:=1;<br>&nbsp; Result:=0;<br><br>&nbsp; if iCode &lt; 0 then //必须将消息传递到消息链的下一个接受单元<br>&nbsp; Result := CallNextHookEx(hPlay,iCode,wParam,lParam)<br>&nbsp; else if iCode = HC_SYSMODALON then<br>&nbsp; canPlay:=0<br>&nbsp; else if iCode = HC_SYSMODALOFF then<br>&nbsp; canPlay:=1<br>&nbsp; else if ((canPlay =1 )and(iCode=HC_GETNEXT)) then begin<br>&nbsp; if bDelay then begin<br>&nbsp; bDelay:=False;<br>&nbsp; Result:=50;<br>&nbsp; end;<br>&nbsp; pEventMSG(lParam)^:=EventArr[PlayLog];<br>&nbsp; end<br>&nbsp; else if ((canPlay = 1)and(iCode = HC_SKIP))then begin<br>&nbsp; bDelay := True;<br>&nbsp; PlayLog:=PlayLog+1;<br>&nbsp; end;<br>&nbsp; if PlayLog&gt;=EventLog then begin<br>&nbsp; UNHookWindowsHookEx(hPlay);<br>&nbsp; end;<br>end;<br><br>function HookProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;<br>begin<br>&nbsp; recOK:=1;<br>&nbsp; Result:=0;<br><br>&nbsp; if iCode &lt; 0 then<br>&nbsp; Result := CallNextHookEx(hHook,iCode,wParam,lParam)<br>&nbsp; else if iCode = HC_SYSMODALON then<br>&nbsp; recOK:=0<br>&nbsp; else if iCode = HC_SYSMODALOFF then<br>&nbsp; recOK:=1<br>&nbsp; else if ((recOK&gt;0) and (iCode = HC_ACTION)) then begin<br>&nbsp; EventArr[EventLog]:=pEventMSG(lParam)^;<br>&nbsp; EventLog:=EventLog+1;<br><br>&nbsp; if EventLog&gt;=1000 then begin<br>&nbsp; UnHookWindowsHookEx(hHook);<br>&nbsp; end;<br>&nbsp; end;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; Button1.Caption:='纪录';<br>&nbsp; Button2.Caption:='停止';<br>&nbsp; Button3.Caption:='回放';<br>&nbsp; Button4.Caption:='范例';<br>&nbsp; Button2.Enabled:=False;<br>&nbsp; Button3.Enabled:=False;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; EventLog:=0;<br>&nbsp; //建立键盘鼠标操作消息纪录链<br>&nbsp; hHook:=SetwindowsHookEx(WH_JOURNALRECORD,HookProc,HInstance,0);<br>&nbsp; Button2.Enabled:=True;<br>&nbsp; Button1.Enabled:=False;<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>&nbsp; UnHookWindowsHookEx(hHook);<br>&nbsp; hHook:=0;<br><br>&nbsp; Button1.Enabled:=True;<br>&nbsp; Button2.Enabled:=False;<br>&nbsp; Button3.Enabled:=True;<br>end;<br><br>procedure TForm1.Button3Click(Sender: TObject);<br>begin<br>&nbsp; PlayLog:=0;<br>&nbsp; //建立键盘鼠标操作消息纪录回放链<br>&nbsp; hPlay:=SetwindowsHookEx(WH_JOURNALPLAYBACK,PlayProc,<br>&nbsp; HInstance,0);<br><br>&nbsp; Button3.Enabled:=False;<br>end;<br><br>end.<br><br>
 
接受答案了.
 
后退
顶部