我模仿别人源码写了个记录鼠标事件的程序如下:
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
var
MsgList:TList;
Msgcount:integer;
recordhand,playhand:THandle;
playing,recording:Boolean;function Play(code:integer;wParam,lParam:LongInt):LongInt;stdcall;
begin
Result:=0;
playing:=True;
if code=HC_GETNEXT then
begin
if MsgCount>=MsgList.Count then begin
UnhookwindowsHookEx(playHand);
exit;
MsgList.Free;
playing:=False; end else
PEventMsg(lParam)^:=TEventMsg(MsgList.Items[MsgCount]);
end
else if code=HC_SKIP then
MsgCount:=Msgcount+1
else
Result:=CallNextHookEx(playHand,Code,wParam,lParam);
end;function records(code:integer;wParam,lParam:LongInt):LongInt;stdcall;
begin
Result:=0;
recording:=True;
if code=HC_ACTION then begin
MsgList.Add(TEventMsg(PEventMsg(lParam)^));
MsgCount:=Msgcount+1;
end
else
Result:=CallNextHookEx(recordhand,code,wParam,lParam);
if MsgCount>1000 then begin
UnHookWindowsHookEx(recordhand);
recording:=False;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
recordhand:=SetWindowsHookEx(WH_JOURNALRECORD,records,hInstance,0);//开始记录事件;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
if recording then begin
UnHookWindowsHookEx(recordhand);//停止记录事件;
recording:=False;
end;
MsgCount:=0;
end;procedure TForm1.Button3Click(Sender: TObject);
begin
playhand:=SetWindowsHookEx(WH_JOURNALPLAYBACK,play,hInstance,0);//回放事件;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
if playing then
UnHookWindowsHookEx(playhand);
MsgList.Clear;
MsgList.Free;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
recordhand:=0;
playhand:=0;
playing:=false;
recording:=false;
MsgList:=TList.Create;
end;
这段代码在回放时终看不到效果,请版主或其他高手指点一二.
小弟谢过!
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
var
MsgList:TList;
Msgcount:integer;
recordhand,playhand:THandle;
playing,recording:Boolean;function Play(code:integer;wParam,lParam:LongInt):LongInt;stdcall;
begin
Result:=0;
playing:=True;
if code=HC_GETNEXT then
begin
if MsgCount>=MsgList.Count then begin
UnhookwindowsHookEx(playHand);
exit;
MsgList.Free;
playing:=False; end else
PEventMsg(lParam)^:=TEventMsg(MsgList.Items[MsgCount]);
end
else if code=HC_SKIP then
MsgCount:=Msgcount+1
else
Result:=CallNextHookEx(playHand,Code,wParam,lParam);
end;function records(code:integer;wParam,lParam:LongInt):LongInt;stdcall;
begin
Result:=0;
recording:=True;
if code=HC_ACTION then begin
MsgList.Add(TEventMsg(PEventMsg(lParam)^));
MsgCount:=Msgcount+1;
end
else
Result:=CallNextHookEx(recordhand,code,wParam,lParam);
if MsgCount>1000 then begin
UnHookWindowsHookEx(recordhand);
recording:=False;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
recordhand:=SetWindowsHookEx(WH_JOURNALRECORD,records,hInstance,0);//开始记录事件;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
if recording then begin
UnHookWindowsHookEx(recordhand);//停止记录事件;
recording:=False;
end;
MsgCount:=0;
end;procedure TForm1.Button3Click(Sender: TObject);
begin
playhand:=SetWindowsHookEx(WH_JOURNALPLAYBACK,play,hInstance,0);//回放事件;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
if playing then
UnHookWindowsHookEx(playhand);
MsgList.Clear;
MsgList.Free;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
recordhand:=0;
playhand:=0;
playing:=false;
recording:=false;
MsgList:=TList.Create;
end;
这段代码在回放时终看不到效果,请版主或其他高手指点一二.
小弟谢过!
解决方案 »
- 弱弱的问:如果你的CS两层或三层结构的程序,在网络十分不稳定的情况下,怎么工作。
- TREEVIEW問題
- 高分求教delphi读取 outlook express 通讯簿记录的代码
- 将Component Palette 里的component 隐藏后,如何再将其显示在相应的组里?
- 怎样截获数据包?
- 各位大哥帮帮小弟,怎么实现像QQ里那样将图片插入到编辑框中呀,用richedit能实现吗?
- 对 cg1120(代码最优化-§雨后看见彩虹……§)的代码的问题,大家帮看一下.
- 怎样写下载百分比?
- 我动态创建了listbox我怎样在单击后Destroy listbox
- 请问各位老手问题 format c: /s 在DELPHI中!!!
- 急救!!!工程文件打不开了。
- dbengine error: invalid blob length?
在Delphi中建立一个工程,在Form1上添加3个按钮用于程序操作。另外再添加一个按钮控件和一
个Edit控件用于验证操作。
下面是Form1的全部代码unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Edit1: TEdit;
Button4: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1; EventArr:array[0..1000]of EVENTMSG;
EventLog:Integer;
PlayLog:Integer;
hHook,hPlay:Integer;
recOK:Integer;
canPlay:Integer;
bDelay:Bool;
implementation{$R *.DFM}
Function PlayProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;
begin
canPlay:=1;
Result:=0;
if iCode < 0 then //必须将消息传递到消息链的下一个接受单元
Result := CallNextHookEx(hPlay,iCode,wParam,lParam)
else if iCode = HC_SYSMODALON then
canPlay:=0
else if iCode = HC_SYSMODALOFF then
canPlay:=1
else if ((canPlay =1 )and(iCode=HC_GETNEXT)) then begin
if bDelay then begin
bDelay:=False;
Result:=50;
end;
pEventMSG(lParam)^:=EventArr[PlayLog];
end
else if ((canPlay = 1)and(iCode = HC_SKIP))then begin
bDelay := True;
PlayLog:=PlayLog+1;
end;
if PlayLog>=EventLog then begin
UNHookWindowsHookEx(hPlay);
end;
end;function HookProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;
begin
recOK:=1;
Result:=0;
if iCode < 0 then
Result := CallNextHookEx(hHook,iCode,wParam,lParam)
else if iCode = HC_SYSMODALON then
recOK:=0
else if iCode = HC_SYSMODALOFF then
recOK:=1
else if ((recOK>0) and (iCode = HC_ACTION)) then begin
EventArr[EventLog]:=pEventMSG(lParam)^;
EventLog:=EventLog+1; if EventLog>=1000 then begin
UnHookWindowsHookEx(hHook);
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Button1.Caption:='纪录';
Button2.Caption:='停止';
Button3.Caption:='回放';
Button4.Caption:='范例';
Button2.Enabled:=False;
Button3.Enabled:=False;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
EventLog:=0;
//建立键盘鼠标操作消息纪录链
hHook:=SetwindowsHookEx(WH_JOURNALRECORD,HookProc,HInstance,0);
Button2.Enabled:=True;
Button1.Enabled:=False;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
UnHookWindowsHookEx(hHook);
hHook:=0;
Button1.Enabled:=True;
Button2.Enabled:=False;
Button3.Enabled:=True;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
PlayLog:=0;
//建立键盘鼠标操作消息纪录回放链
hPlay:=SetwindowsHookEx(WH_JOURNALPLAYBACK,PlayProc,
HInstance,0);
Button3.Enabled:=False;
end;
end.
代码添加完毕后,运行程序,点击“纪录”按钮开始纪录操作,这时你可以在文本控件中输入一些文字或者点击
“范例”按钮,然后点击“停止”按钮停止纪录,再点击“回放”按钮就可以讲先前所做的操作回放。
在上面的程序中,HookProc是纪录操作的消息函数,每当有鼠标键盘消息发生时,系统都会调用该函数,消息信
息就保存在地址lParam中,我们可以讲消息保存在一个数组中。PlayProc是消息回放函数,当系统可以执行消息回放
时调用该函数,程序就将先前纪录的消息值返回到lParam指向的区域中,系统就会执行该消息,从而实现了消息回放。
我想麻烦高手看看我的那个回放钩子函数(play)哪里有错.