Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
Mouse working only in your application Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
02-Nov-02
Category
VCL-Forms
Language
Delphi 2.x
Views
43
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Radikal Q3

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:

1   function CallBackDelHook(Code: Integer;
2     wParam: WPARAM;
3     lParam: LPARAM
4     ): LRESULT; stdcall;
5   
6   var
7     DatosMouse: PMouseHookStruct;
8     Intentos: integer;
9   
10    {Esta es la funcion CallBack a la cual llamará el hook.}
11    {This is the CallBack function called by he Hook}
12  begin
13    {Si hay un nuevo evento de raton...}
14    {if there is a new mouse event...}
15    if code = HC_ACTION then
16    begin
17      {Miramos si existe el fichero}
18      {if the mapfile exists}
19      FicheroM := OpenFileMapping(FILE_MAP_READ, False, 'ElReceptor');
20      {Si no existe, no enviamos nada a la aplicacion receptora}
21      {If dont, send nothing to receiver application}
22      if FicheroM <> 0 then
23      begin
24        Compartido := MapViewOfFile(FicheroM, FILE_MAP_READ, 0, 0, 0);
25  
26        {Apuntamos hacia los datos del evento del raton}
27        DatosMouse := Pointer(lparam);
28  
29        UnmapViewOfFile(Compartido);
30        CloseHandle(FicheroM);
31      end;
32    end;
33    {Llamamos al siguiente hook de la cadena}
34    {call to next hook of the chain}
35    if Compartido^ <> DatosMouse^.hwnd then
36      Result := 1
37    else
38      Result := CallNextHookEx(HookDeMouse, Code, wParam, lParam);
39  end;
40  
41  //and the relevant part is this:
42  
43  if Compartido^ <> DatosMouse^.hwnd then
44    Result := 1
45  else
46    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

47  library HookMouse;
48  
49  {Demo de Hook de Ratón a nivel de sistema, Radikal.}
50  
51  uses Windows, Messages;
52  
53  type
54    PCompartido = ^THandle;
55  
56  var
57    HookDeMouse: HHook;
58    FicheroM: THandle;
59    Compartido: PCompartido;
60  
61  function CallBackDelHook(Code: Integer;
62    wParam: WPARAM;
63    lParam: LPARAM
64    ): LRESULT; stdcall;
65  
66  var
67    DatosMouse: PMouseHookStruct;
68    Intentos: integer;
69  
70    {Esta es la funcion CallBack a la cual llamará el hook.}
71    {This is the CallBack function called by he Hook}
72  begin
73    {Si hay un nuevo evento de raton...}
74    {if there is a new mouse event...}
75    if code = HC_ACTION then
76    begin
77      {Miramos si existe el fichero}
78      {if the mapfile exists}
79      FicheroM := OpenFileMapping(FILE_MAP_READ, False, 'ElReceptor');
80      {Si no existe, no enviamos nada a la aplicacion receptora}
81      {If dont, send nothing to receiver application}
82      if FicheroM <> 0 then
83      begin
84        Compartido := MapViewOfFile(FicheroM, FILE_MAP_READ, 0, 0, 0);
85  
86        {Apuntamos hacia los datos del evento del raton}
87        DatosMouse := Pointer(lparam);
88  
89        UnmapViewOfFile(Compartido);
90        CloseHandle(FicheroM);
91      end;
92    end;
93    {Llamamos al siguiente hook de la cadena}
94    {call to next hook of the chain}
95    if Compartido^ <> DatosMouse^.hwnd then
96      Result := 1
97    else
98      Result := CallNextHookEx(HookDeMouse, Code, wParam, lParam);
99  end;
100 
101 procedure HookOn; stdcall;
102 {Procedure que instala el hook}
103 {procedure for install the hook}
104 begin
105   HookDeMouse := SetWindowsHookEx(WH_MOUSE, @CallBackDelHook, HInstance, 0);
106 end;
107 
108 // stops this type of watch
109 
110 procedure HookOff; stdcall;
111 begin
112   {procedure para desinstalar el hook}
113   {procedure to uninstall the hook}
114   UnhookWindowsHookEx(HookDeMouse);
115 end;
116 
117 exports
118   {Exportamos las procedures...}
119   {Export the procedures}
120   HookOn,
121   HookOff;
122 
123 begin
124 end.


Demo application

In a clean form, put this code in its unit:

125 unit Unit1;
126 
127 interface
128 
129 uses
130   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
131   StdCtrls, ExtCtrls;
132 
133 const
134   NombreDLL = 'HookMouse.dll';
135 
136 type
137   PCompartido = ^THandle;
138   THookMouse = procedure; stdcall;
139 
140 type
141   TForm1 = class(TForm)
142     procedure FormCreate(Sender: TObject);
143     procedure FormDestroy(Sender: TObject);
144   private
145     { Private declarations }
146     FicheroM: THandle;
147     Compartido: PCompartido;
148     HandleDLL: THandle;
149     HookOn,
150       HookOff: THookMouse;
151 
152   public
153     { Public declarations }
154   end;
155 
156 var
157   Form1: TForm1;
158 
159 implementation
160 
161 {$R *.DFM}
162 
163 procedure TForm1.FormCreate(Sender: TObject);
164 begin
165   HandleDLL := LoadLibrary(PChar(ExtractFilePath(Application.Exename) +
166     NombreDLL));
167   if HandleDLL = 0 then
168     raise Exception.Create('No se pudo cargar la DLL');
169 
170   @HookOn := GetProcAddress(HandleDLL, 'HookOn');
171   @HookOff := GetProcAddress(HandleDLL, 'HookOff');
172 
173   if not assigned(HookOn) or
174     not assigned(HookOff) then
175     raise Exception.Create('No se encontraron las funciones en la DLL' + #13 +
176       'Cannot find the required DLL functions');
177 
178   {Creamos el fichero de memoria}
179   FicheroM := CreateFileMapping($FFFFFFFF,
180     nil,
181     PAGE_READWRITE,
182     0,
183     SizeOf(THandle),
184     'ElReceptor');
185 
186   {Si no se creó el fichero, error}
187   if FicheroM = 0 then
188     raise Exception.Create('Error al crear el fichero' +
189       '/Error while create file');
190 
191   {Direccionamos nuestra estructura al fichero de memoria}
192   Compartido := MapViewOfFile(FicheroM, FILE_MAP_WRITE, 0, 0, 0);
193 
194   {Escribimos datos en el fichero de memoria}
195   Compartido^ := Handle;
196   HookOn;
197 end;
198 
199 procedure TForm1.FormDestroy(Sender: TObject);
200 begin
201   {Desactivamos el Hook}
202   {Uninstall the Hook}
203   if Assigned(HookOff) then
204     HookOff;
205 
206   {Liberamos la DLL}
207   {Free the DLL}
208   if HandleDLL <> 0 then
209     FreeLibrary(HandleDLL);
210 
211   {Cerramos la vista del fichero y el fichero}
212   {Close the memfile and the View}
213   if FicheroM <> 0 then
214   begin
215     UnmapViewOfFile(Compartido);
216     CloseHandle(FicheroM);
217   end;
218 end;
219 
220 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...
 



			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC