unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
const
NombreDLL = '
HookMouse.dll';
CM_MANDA_DATOS = WM_USER + $1000;
type
TCompartido =
record
Receptor,
wHitTestCode,
x,y,
Ventana : hwnd;
end;
PCompartido =^TCompartido;
THookMouse=procedure;
stdcall;
type
TForm1 =
class(TForm)
Memo1: TMemo;
Panel1: TPanel;
Label1: TLabel;
Label2: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
FicheroM : THandle;
Compartido : PCompartido;
HandleDLL : THandle;
HookOn,
HookOff : THookMouse;
procedure LlegaDelHook(
var message: TMessage);
message CM_MANDA_DATOS;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
HandleDLL:=LoadLibrary( PChar(ExtractFilePath(Application.Exename)+
NombreDLL ) );
if HandleDLL = 0
then raise Exception.Create('
No se pudo cargar la DLL');
@HookOn :=GetProcAddress(HandleDLL, '
HookOn');
@HookOff:=GetProcAddress(HandleDLL, '
HookOff');
if not assigned(HookOn)
or
not assigned(HookOff)
then
raise Exception.Create('
No se encontraron las funciones en la DLL'+#13+
'
Cannot find the required DLL functions');
{Creamos el fichero de memoria}
FicheroM:=CreateFileMapping( $FFFFFFFF,
nil,
PAGE_READWRITE,
0,
SizeOf(Compartido),
'
ElReceptor');
{Si no se creó el fichero, error}
if FicheroM=0
then
raise Exception.Create( '
Error al crear el fichero'+
'
/Error while create file');
{Direccionamos nuestra estructura al fichero de memoria}
Compartido:=MapViewOfFile(FicheroM,FILE_MAP_WRITE,0,0,0);
{Escribimos datos en el fichero de memoria}
Compartido^.Receptor:=
Handle;
HookOn;
end;
procedure TForm1.LlegaDelHook(
var message: TMessage);
var
DatosMouse : PMouseHookStruct;
NombreVentana :
array [0..200]
of char;
Accion :
string;
begin
with Compartido^
do
begin
{Coordenadas del raton}
{Mouse coordinates}
Label1.caption:='
['+IntToStr(x)+'
:'+IntToStr(y)+'
]';
end;
{Nombre de la ventana donde esta el raton}
{Window Name}
GetWindowText(Compartido^.Ventana,@NombreVentana,200);
Label2.Caption:=NombreVentana;
case Message.wParam
of
WM_LBUTTONDBLCLK : Accion:='
WM_LBUTTONDBLCLK ';
WM_LBUTTONDOWN : Accion:='
WM_LBUTTONDOWN ';
WM_LBUTTONUP : Accion:='
WM_LBUTTONUP ';
WM_MBUTTONDBLCLK : Accion:='
WM_MBUTTONDBLCLK ';
WM_MBUTTONDOWN : Accion:='
WM_MBUTTONDOWN ';
WM_MBUTTONUP : Accion:='
WM_MBUTTONUP ';
WM_MOUSEMOVE : Accion:='
WM_MOUSEMOVE ';
WM_NCHITTEST : Accion:='
WM_NCHITTEST ';
WM_NCLBUTTONDBLCLK : Accion:='
WM_NCLBUTTONDBLCLK';
WM_NCLBUTTONDOWN : Accion:='
WM_NCLBUTTONDOWN ';
WM_NCLBUTTONUP : Accion:='
WM_NCLBUTTONUP ';
WM_NCMBUTTONDBLCLK : Accion:='
WM_NCMBUTTONDBLCLK';
WM_NCMBUTTONDOWN : Accion:='
WM_NCMBUTTONDOWN ';
WM_NCMBUTTONUP : Accion:='
WM_NCMBUTTONUP ';
WM_NCMOUSEMOVE : Accion:='
WM_NCMOUSEMOVE ';
WM_NCRBUTTONDBLCLK : Accion:='
WM_NCRBUTTONDBLCLK';
WM_NCRBUTTONDOWN : Accion:='
WM_NCRBUTTONDOWN ';
WM_NCRBUTTONUP : Accion:='
WM_NCRBUTTONUP ';
WM_RBUTTONDBLCLK : Accion:='
WM_RBUTTONDBLCLK ';
WM_RBUTTONDOWN : Accion:='
WM_RBUTTONDOWN ';
WM_RBUTTONUP : Accion:='
WM_RBUTTONUP ';
end;
Memo1.Lines.Append(Accion);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
{Desactivamos el Hook}
{Uninstall the Hook}
if Assigned(HookOff)
then HookOff;
{Liberamos la DLL}
{Free the DLL}
if HandleDLL<>0
then
FreeLibrary(HandleDLL);
{Cerramos la vista del fichero y el fichero}
{Close the memfile and the View}
if FicheroM<>0
then
begin
UnmapViewOfFile(Compartido);
CloseHandle(FicheroM);
end;
end;
end.