1 | //***********************************************************
|
---|
2 | // EwbMouseHook unit *
|
---|
3 | // *
|
---|
4 | // For Delphi 5 to 2009 *
|
---|
5 | // Freeware Component *
|
---|
6 | // by *
|
---|
7 | // (smot) *
|
---|
8 | // *
|
---|
9 | // Documentation and updated versions: *
|
---|
10 | // *
|
---|
11 | // http://www.bsalsa.com *
|
---|
12 | //***********************************************************
|
---|
13 | {*******************************************************************************}
|
---|
14 | {LICENSE:
|
---|
15 | THIS SOFTWARE IS PROVIDED TO YOU "AS IS" WITHOUT WARRANTY OF ANY KIND,
|
---|
16 | EITHER EXPRESSED OR IMPLIED INCLUDING BUT NOT LIMITED TO THE APPLIED
|
---|
17 | WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
|
---|
18 | YOU ASSUME THE ENTIRE RISK AS TO THE ACCURACY AND THE USE OF THE SOFTWARE
|
---|
19 | AND ALL OTHER RISK ARISING OUT OF THE USE OR PERFORMANCE OF THIS SOFTWARE
|
---|
20 | AND DOCUMENTATION. BSALSA PRODUCTIONS DOES NOT WARRANT THAT THE SOFTWARE IS ERROR-FREE
|
---|
21 | OR WILL OPERATE WITHOUT INTERRUPTION. THE SOFTWARE IS NOT DESIGNED, INTENDED
|
---|
22 | OR LICENSED FOR USE IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE CONTROLS,
|
---|
23 | INCLUDING WITHOUT LIMITATION, THE DESIGN, CONSTRUCTION, MAINTENANCE OR
|
---|
24 | OPERATION OF NUCLEAR FACILITIES, AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS,
|
---|
25 | AIR TRAFFIC CONTROL, AND LIFE SUPPORT OR WEAPONS SYSTEMS. BSALSA PRODUCTIONS SPECIFICALLY
|
---|
26 | DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR SUCH PURPOSE.
|
---|
27 |
|
---|
28 | You may use, change or modify the component under 4 conditions:
|
---|
29 | 1. In your website, add a link to "http://www.bsalsa.com"
|
---|
30 | 2. In your application, add credits to "Embedded Web Browser"
|
---|
31 | 3. Mail me (bsalsa@gmail.com) any code change in the unit
|
---|
32 | for the benefit of the other users.
|
---|
33 | 4. Please, consider donation in our web site!
|
---|
34 | {*******************************************************************************}
|
---|
35 |
|
---|
36 | unit EwbMouseHook;
|
---|
37 |
|
---|
38 | interface
|
---|
39 |
|
---|
40 | {$I EWB.inc}
|
---|
41 |
|
---|
42 | uses
|
---|
43 | Windows, Messages, Classes, Forms, Controls;
|
---|
44 |
|
---|
45 | // -- TEWBMouseHook ------------------------------------------------------------
|
---|
46 |
|
---|
47 | type
|
---|
48 | TFNMouseProc = function(nCode: Integer; wp: WPARAM; lp: WParam): LRESULT
|
---|
49 | stdcall;
|
---|
50 | TFNMouseMethod = function(nCode: Integer; wp: WPARAM; lp: WParam): LRESULT
|
---|
51 | stdcall of object;
|
---|
52 | TMouseWheelEvent = procedure(Point: TPoint; hwndFromPoint: HWND; lp: LPARAM;
|
---|
53 | var Handled: Boolean) of object;
|
---|
54 |
|
---|
55 | type
|
---|
56 | TEWBMouseHook = class(TObject)
|
---|
57 | private
|
---|
58 | FMouseHook: HHOOK;
|
---|
59 | FMouseHookProc: TFNMouseProc;
|
---|
60 | FMouseHookMethod: TFNMouseMethod;
|
---|
61 | function LocalMouseProc(nCode: Integer; wp: WPARAM; lp: LPARAM): LRESULT
|
---|
62 | stdcall;
|
---|
63 | public
|
---|
64 | OnMouseWheel: TMouseWheelEvent;
|
---|
65 | FActiveFormOnly: Boolean;
|
---|
66 | class function NewInstance: TObject; override;
|
---|
67 | procedure FreeInstance; override;
|
---|
68 | constructor Create;
|
---|
69 | destructor Destroy; override;
|
---|
70 | procedure Activate;
|
---|
71 | procedure Deactivate;
|
---|
72 | end;
|
---|
73 |
|
---|
74 | var
|
---|
75 | EWBEnableMouseWheelFix: Boolean = True; // DO NOT CHANGE HERE
|
---|
76 |
|
---|
77 | implementation
|
---|
78 |
|
---|
79 | uses
|
---|
80 | EWBCoreTools;
|
---|
81 |
|
---|
82 | {$IFDEF DELPHI5}
|
---|
83 | type
|
---|
84 | TMethod = record
|
---|
85 | Code, Data: Pointer;
|
---|
86 | end;
|
---|
87 | {$ENDIF}
|
---|
88 |
|
---|
89 | var
|
---|
90 | GEWBMouseHook: TEWBMouseHook = nil;
|
---|
91 | GRefCount: Integer = 0;
|
---|
92 |
|
---|
93 | // MakeStdcallCallback (thunk to use stdcall method as static callback)
|
---|
94 |
|
---|
95 | function MakeStdcallCallback(const Method: TMethod): Pointer;
|
---|
96 | type
|
---|
97 | PCallbackCode = ^TCallbackCode;
|
---|
98 | TCallbackCode = packed record
|
---|
99 | Ops1: array[0..2] of Longword;
|
---|
100 | Val1: Pointer;
|
---|
101 | Ops2: array[0..1] of Longword;
|
---|
102 | Val2: Pointer;
|
---|
103 | end;
|
---|
104 | begin
|
---|
105 | Result := VirtualAlloc(nil, $100, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
|
---|
106 | if Assigned(Result) then
|
---|
107 | try
|
---|
108 | with PCallbackCode(Result)^ do
|
---|
109 | begin
|
---|
110 | Ops1[0] := $448B5050;
|
---|
111 | Ops1[1] := $44890824;
|
---|
112 | Ops1[2] := $058B0424;
|
---|
113 | Val1 := Addr(Method.Data);
|
---|
114 | Ops2[0] := $08244489;
|
---|
115 | Ops2[1] := $25FF9058;
|
---|
116 | Val2 := Addr(Method.Code);
|
---|
117 | end;
|
---|
118 | except
|
---|
119 | VirtualFree(Result, 0, MEM_RELEASE);
|
---|
120 | Result := nil;
|
---|
121 | end;
|
---|
122 | end;
|
---|
123 |
|
---|
124 | procedure FreeCallback(Callback: Pointer);
|
---|
125 | begin
|
---|
126 | if Assigned(Callback) then
|
---|
127 | VirtualFree(Callback, 0, MEM_RELEASE);
|
---|
128 | end;
|
---|
129 |
|
---|
130 | function TEWBMouseHook.LocalMouseProc(nCode: Integer; wp: WPARAM; lp: LPARAM):
|
---|
131 | LRESULT stdcall;
|
---|
132 | var
|
---|
133 | bHandled, bCancel: Boolean;
|
---|
134 | mhs: TMouseHookStruct;
|
---|
135 | hwndCurrentFocus: HWND;
|
---|
136 | P: TPoint;
|
---|
137 | hwndFromPoint, hwndFocusShellEmbedding, hwndWFPShellEmbedding: HWND;
|
---|
138 | WinControl: TWinControl;
|
---|
139 | ParentForm: TCustomForm;
|
---|
140 | begin
|
---|
141 | if nCode < 0 then
|
---|
142 | Result := CallNextHookEx(FMouseHook, nCode, wp, LPARAM(lp))
|
---|
143 | else
|
---|
144 | begin
|
---|
145 | bHandled := False;
|
---|
146 | if (wp = WM_MOUSEWHEEL) and (nCode = HC_ACTION) then
|
---|
147 | begin
|
---|
148 | mhs := PMouseHookStruct(lp)^;
|
---|
149 | P := Point(mhs.pt.X, mhs.pt.Y);
|
---|
150 | hwndFromPoint := WindowFromPoint(P);
|
---|
151 | // Handle OnMouseWheel
|
---|
152 | bCancel := False;
|
---|
153 | if Assigned(OnMouseWheel) then
|
---|
154 | OnMouseWheel(P, hwndFromPoint, lp, bCancel);
|
---|
155 | // Handle ActiveFormOnly
|
---|
156 | if FActiveFormOnly then
|
---|
157 | begin
|
---|
158 | WinControl := FindVCLWindow(P);
|
---|
159 | if Assigned(WinControl) then
|
---|
160 | begin
|
---|
161 | ParentForm := GetParentForm(WinControl);
|
---|
162 | if Assigned(ParentForm) then
|
---|
163 | bCancel := ParentForm <> Screen.ActiveForm;
|
---|
164 | end;
|
---|
165 | end;
|
---|
166 |
|
---|
167 | if not bCancel then
|
---|
168 | if hwndFromPoint <> 0 then
|
---|
169 | begin
|
---|
170 | hwndCurrentFocus := GetFocus;
|
---|
171 | if hwndCurrentFocus <> 0 then
|
---|
172 | begin
|
---|
173 | hwndWFPShellEmbedding := GetParentWinByClass(hwndFromPoint,
|
---|
174 | 'Shell Embedding');
|
---|
175 | if (hwndWFPShellEmbedding <> 0) then
|
---|
176 | // Parent of WindowFromPoint is WB
|
---|
177 | begin
|
---|
178 | hwndFocusShellEmbedding :=
|
---|
179 | GetParentWinByClass(hwndCurrentFocus,
|
---|
180 | 'Shell Embedding');
|
---|
181 | if (hwndWFPShellEmbedding <> hwndFocusShellEmbedding) or
|
---|
182 | (GetWinClass(hwndCurrentFocus) =
|
---|
183 | 'Shell DocObject View') then
|
---|
184 | // Only handle if WB has not the focus or other WB has the focus
|
---|
185 | begin
|
---|
186 | bHandled := True;
|
---|
187 | Windows.SetFocus(hwndFromPoint);
|
---|
188 | end;
|
---|
189 | end;
|
---|
190 | end;
|
---|
191 | end;
|
---|
192 | end;
|
---|
193 | if bHandled then
|
---|
194 | Result := HC_SKIP
|
---|
195 | else
|
---|
196 | Result := CallNextHookEx(FMouseHook, nCode, wp, lp);
|
---|
197 | end;
|
---|
198 | end;
|
---|
199 |
|
---|
200 | class function TEWBMouseHook.NewInstance: TObject;
|
---|
201 | begin
|
---|
202 | if not Assigned(GEWBMouseHook) then
|
---|
203 | begin
|
---|
204 | GEWBMouseHook := TEWBMouseHook(inherited NewInstance);
|
---|
205 | GEWBMouseHook.FActiveFormOnly := False;
|
---|
206 | end;
|
---|
207 | Result := GEWBMouseHook;
|
---|
208 | Inc(GRefCount);
|
---|
209 | end;
|
---|
210 |
|
---|
211 | procedure TEWBMouseHook.FreeInstance;
|
---|
212 | begin
|
---|
213 | Dec(GRefCount);
|
---|
214 | if GRefCount = 0 then
|
---|
215 | begin
|
---|
216 | GEWBMouseHook := nil;
|
---|
217 | inherited FreeInstance;
|
---|
218 | end;
|
---|
219 | end;
|
---|
220 |
|
---|
221 | procedure TEWBMouseHook.Activate;
|
---|
222 | begin
|
---|
223 | if (FMouseHook = 0) and EWBEnableMouseWheelFix then
|
---|
224 | begin
|
---|
225 | FMouseHookMethod := LocalMouseProc;
|
---|
226 | FMouseHookProc :=
|
---|
227 | TFNMouseProc(MakeStdcallCallback(TMethod(FMouseHookMethod)));
|
---|
228 | FMouseHook := SetWindowsHookEx(WH_MOUSE, TFNHookProc(FMouseHookProc), 0,
|
---|
229 | GetCurrentThreadID);
|
---|
230 | end;
|
---|
231 | end;
|
---|
232 |
|
---|
233 | procedure TEWBMouseHook.Deactivate;
|
---|
234 | begin
|
---|
235 | if FMouseHook <> 0 then
|
---|
236 | begin
|
---|
237 | if UnhookWindowsHookEx(FMouseHook) then
|
---|
238 | FMouseHook := 0;
|
---|
239 | end;
|
---|
240 | if Assigned(FMouseHookProc) then
|
---|
241 | begin
|
---|
242 | FreeCallback(Addr(FMouseHookProc));
|
---|
243 | FMouseHookProc := nil;
|
---|
244 | end;
|
---|
245 | end;
|
---|
246 |
|
---|
247 | constructor TEWBMouseHook.Create;
|
---|
248 | begin
|
---|
249 | inherited;
|
---|
250 | // if (GRefCount = 1) then
|
---|
251 | end;
|
---|
252 |
|
---|
253 | destructor TEWBMouseHook.Destroy;
|
---|
254 | begin
|
---|
255 | inherited;
|
---|
256 | // if ((GRefCount <= 1)) then
|
---|
257 | end;
|
---|
258 |
|
---|
259 | end.
|
---|