以前一直用c++builder,现在资料越来越少,很多资料都只能用delphi里面的转换过来,前阵子刚看到一个delphi版本的日值钩子,刚好借鉴一下。
delphi 版本
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Memo1: TMemo;
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.
c++builder 版本
//---------------------------------------------------------------------------
#include <vcl.h>
#include <windows.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
EVENTMSG EventArr[1000];
int EventLog;
int PlayLog;
HHOOK hHook,hPlay;
int recOK;
int canPlay;
bool bDelay;
int Result;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------
LRESULT CALLBACK PlayProc(int iCode,WPARAM wParam,LPARAM lParam)
{
canPlay=1;
if (iCode< 0)
{ return CallNextHookEx(hPlay,iCode,wParam,lParam);}
if(iCode==HC_SYSMODALON)
{ canPlay=0 ; }
if(iCode== HC_SYSMODALOFF)
{ canPlay=1 ; }
if(iCode==HC_GETNEXT)
{ if(canPlay==1)
{ bDelay=false;
Sleep(100);
EVENTMSG* pem = (EVENTMSG*)lParam;
*pem=EventArr[PlayLog];
}
}
if(iCode==HC_SKIP)
{ if(canPlay==1)
{ bDelay=true;
PlayLog+=1;
}
if (PlayLog>=EventLog)
{
UnhookWindowsHookEx(hPlay);
}
}
return 0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Button1->Caption="纪录";
Button2->Caption="停止";
Button3->Caption="回放";
Button4->Caption="范例";
Button2->Enabled=false;
Button3->Enabled=false;
}
//---------------------------------------------------
LRESULT CALLBACK HookProc(int iCode,WPARAM wParam,LPARAM lParam)
{
recOK=1;
if(iCode< 0)
{ Result=CallNextHookEx(hHook,iCode,wParam,lParam); }
if(iCode==HC_SYSMODALON )
recOK=0;
if(iCode==HC_SYSMODALOFF)
recOK=1;
if(iCode==HC_ACTION)
{ if(recOK>0)
{EVENTMSG* pm = (EVENTMSG*)lParam;
EventArr[EventLog]=*pm;
EventLog+=1;
if (EventLog>=1000)
UnhookWindowsHookEx(hHook);
}
}
return 0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
UnhookWindowsHookEx(hHook);
hHook=NULL;
Button1->Enabled=true;
Button2->Enabled=false;
Button3->Enabled=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
PlayLog=0;
hPlay=SetWindowsHookEx(WH_JOURNALPLAYBACK,(HOOKPROC)PlayProc,
HInstance,0);
Button3->Enabled=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
EventLog=0;
hHook=SetWindowsHookEx(WH_JOURNALRECORD,(HOOKPROC)HookProc,HInstance,0);
Button2->Enabled=true;
Button1->Enabled=false;
}
//---------------------------------------------------------------------------