2004. március 14., vasárnap
Mouse working only in your application
Problem/Question/Abstract:
I suppose that to somebody it will be he useful this trick.
Have I outlined it to me as a simple exercise of those that you tell yourself to yourself ' that would happen if? '.
It is that the mouse only works in your application. Neither in the Windows desktop, neither in the window's taskbar, neither in any
other place that in your form...
Of course, care with him... if you are happened to print something and the driver of your printer it jumps you with the typical
message ' papaer empty' you won't be able to close the dialogue that it appears... in short, better you prove it and you see if it is
good you for something:
Answer:
function CallBackDelHook(Code: Integer;
wParam: WPARAM;
lParam: LPARAM
): LRESULT; stdcall;
var
DatosMouse: PMouseHookStruct;
Intentos: integer;
{Esta es la funcion CallBack a la cual llamar� el hook.}
{This is the CallBack function called by he Hook}
begin
{Si hay un nuevo evento de raton...}
{if there is a new mouse event...}
if code = HC_ACTION then
begin
{Miramos si existe el fichero}
{if the mapfile exists}
FicheroM := OpenFileMapping(FILE_MAP_READ, False, 'ElReceptor');
{Si no existe, no enviamos nada a la aplicacion receptora}
{If dont, send nothing to receiver application}
if FicheroM <> 0 then
begin
Compartido := MapViewOfFile(FicheroM, FILE_MAP_READ, 0, 0, 0);
{Apuntamos hacia los datos del evento del raton}
DatosMouse := Pointer(lparam);
UnmapViewOfFile(Compartido);
CloseHandle(FicheroM);
end;
end;
{Llamamos al siguiente hook de la cadena}
{call to next hook of the chain}
if Compartido^ <> DatosMouse^.hwnd then
Result := 1
else
Result := CallNextHookEx(HookDeMouse, Code, wParam, lParam);
end;
and the relevant part is this:
if Compartido^ <> DatosMouse^.hwnd then
Result := 1
else
Result := CallNextHookEx(HookDeMouse, Code, wParam, lParam);
that is to say, if the recipient of the message that has captured the hook is our application, we call to the following mouse hook that
has installed in the chain, if it is not this way, because we give the message like treaty, miscarrying this way his processed later for
the application that is...
Well, like in any hook at system level, we will build ourselves first a DLL that is where anger the procedure CallBack of the hook,
and then an example application that will communicate somehow with the DLL.
Therefore:
Hook's DLL
library HookMouse;
{Demo de Hook de Rat�n a nivel de sistema, Radikal.}
uses Windows, Messages;
type
PCompartido = ^THandle;
var
HookDeMouse: HHook;
FicheroM: THandle;
Compartido: PCompartido;
function CallBackDelHook(Code: Integer;
wParam: WPARAM;
lParam: LPARAM
): LRESULT; stdcall;
var
DatosMouse: PMouseHookStruct;
Intentos: integer;
{Esta es la funcion CallBack a la cual llamar� el hook.}
{This is the CallBack function called by he Hook}
begin
{Si hay un nuevo evento de raton...}
{if there is a new mouse event...}
if code = HC_ACTION then
begin
{Miramos si existe el fichero}
{if the mapfile exists}
FicheroM := OpenFileMapping(FILE_MAP_READ, False, 'ElReceptor');
{Si no existe, no enviamos nada a la aplicacion receptora}
{If dont, send nothing to receiver application}
if FicheroM <> 0 then
begin
Compartido := MapViewOfFile(FicheroM, FILE_MAP_READ, 0, 0, 0);
{Apuntamos hacia los datos del evento del raton}
DatosMouse := Pointer(lparam);
UnmapViewOfFile(Compartido);
CloseHandle(FicheroM);
end;
end;
{Llamamos al siguiente hook de la cadena}
{call to next hook of the chain}
if Compartido^ <> DatosMouse^.hwnd then
Result := 1
else
Result := CallNextHookEx(HookDeMouse, Code, wParam, lParam);
end;
procedure HookOn; stdcall;
{Procedure que instala el hook}
{procedure for install the hook}
begin
HookDeMouse := SetWindowsHookEx(WH_MOUSE, @CallBackDelHook, HInstance, 0);
end;
// stops this type of watch
procedure HookOff; stdcall;
begin
{procedure para desinstalar el hook}
{procedure to uninstall the hook}
UnhookWindowsHookEx(HookDeMouse);
end;
exports
{Exportamos las procedures...}
{Export the procedures}
HookOn,
HookOff;
begin
end.
Demo application
In a clean form, put this code in its unit:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
const
NombreDLL = 'HookMouse.dll';
type
PCompartido = ^THandle;
THookMouse = procedure; stdcall;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
FicheroM: THandle;
Compartido: PCompartido;
HandleDLL: THandle;
HookOn,
HookOff: THookMouse;
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(THandle),
'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^ := Handle;
HookOn;
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.
Note: If you use this technique in an application with several forms, you will put in ' Compartido' the handle of the form that you will
activate, but it won't receive mouse events. For example, you can make it in the event OnShow of the form...
Feliratkozás:
Megjegyzések küldése (Atom)
Nincsenek megjegyzések:
Megjegyzés küldése