forked from TurboPack/Orpheus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathO32MouseMon.pas
155 lines (139 loc) · 5.5 KB
/
O32MouseMon.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
{*********************************************************}
{* O32MOUSEMON.PAS 4.0 *}
{*********************************************************}
{* ***** BEGIN LICENSE BLOCK ***** *}
{* Version: MPL 1.1 *}
{* *}
{* The contents of this file are subject to the Mozilla Public License *}
{* Version 1.1 (the "License"); you may not use this file except in *}
{* compliance with the License. You may obtain a copy of the License at *}
{* http://www.mozilla.org/MPL/ *}
{* *}
{* Software distributed under the License is distributed on an "AS IS" basis, *}
{* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License *}
{* for the specific language governing rights and limitations under the *}
{* License. *}
{* *}
{* The Original Code is TurboPower Orpheus *}
{* *}
{* The Initial Developer of the Original Code is TurboPower Software *}
{* *}
{* Portions created by TurboPower Software Inc. are Copyright (C)1995-2002 *}
{* TurboPower Software Inc. All Rights Reserved. *}
{* *}
{* Contributor(s): *}
{* *}
{* ***** END LICENSE BLOCK ***** *}
{$I OVC.INC}
unit O32MouseMon;
{ Orpheus Mouse Monitor }
interface
uses
Windows, Classes, SysUtils, Messages, OvcMisc;
type
{ - HWnd changed to TOvcHWnd for BCB Compatibility}
TMouseMonHandler = procedure(const MouseMessage, wParam, lParam: Integer;
const ScreenPt: TPoint; const MouseWnd: TOvcHWnd{hWnd}) of object;
procedure StartMouseMonitor(Callback: TMouseMonHandler);
procedure StopMouseMonitor(Callback: TMouseMonHandler);
implementation
type
TMouseMonEntry = class
protected
FHandler: TMouseMonHandler;
public
property Handler: TMouseMonHandler read FHandler write FHandler;
end;
var
MouseMonList: TList = nil;
ghhk: HHook = 0;
mhhk: HHook = 0;
function MessageHookProc(nCode: Integer; wParam, lParam: Integer): Integer; stdcall;
var
i: Integer;
begin
if nCode = 0 then begin
if (nCode = HC_ACTION) and (PMsg(lParam).message = WM_MOUSEWHEEL) then
for i := 0 to MouseMonList.Count - 1 do
TMouseMonEntry(MouseMonList[i]).Handler(
PMsg(lParam).message,
PMsg(lParam).wParam,
PMsg(lParam).lParam,
PMsg(lParam).pt,
PMsg(lParam).hwnd);
end;
Result := CallNextHookEx(ghhk, nCode, wParam, lParam);
end;
function MouseHookProc(nCode: Integer; wParam, lParam: Integer): Integer; stdcall;
var
i: Integer;
begin
if (nCode = HC_ACTION) and (wParam <> WM_MOUSEWHEEL) then
for i := 0 to MouseMonList.Count - 1 do
TMouseMonEntry(MouseMonList[i]).Handler(
wParam,
0,
0,
PMouseHookStruct(lParam).pt,
PMouseHookStruct(lParam).hwnd);
Result := CallNextHookEx(mhhk, nCode, wParam, lParam);
end;
procedure InstallHook;
begin
mhhk := SetWindowsHookEx(WH_MOUSE, TFNHookProc(@MouseHookProc), 0, GetCurrentThreadID);
ghhk := SetWindowsHookEx(WH_GETMESSAGE, TFNHookProc(@MessageHookProc), 0, GetCurrentThreadID);
end;
procedure StartMouseMonitor(Callback: TMouseMonHandler);
var
NewEntry: TMouseMonEntry;
i: Integer;
begin
if MouseMonList = nil then
MouseMonList := TList.Create;
for i := 0 to MouseMonList.Count - 1 do
if (TMethod(TMouseMonEntry(MouseMonList[i]).Handler).Code = TMethod(Callback).Code)
and (TMethod(TMouseMonEntry(MouseMonList[i]).Handler).Data = TMethod(Callback).Data) then begin
raise Exception.Create('Only one handler per window at a time, please');
//exit;
end;
NewEntry := TMouseMonEntry.Create;
NewEntry.Handler := Callback;
MouseMonList.Add(NewEntry);
if mhhk = 0 then
InstallHook;
end;
procedure ClearMonList;
begin
if MouseMonList = nil then exit;
while MouseMonList.Count > 0 do begin
TMouseMonEntry(MouseMonList[0]).Free;
MouseMonList.Delete(0);
end;
MouseMonList.Free;
MouseMonList := nil;
if mhhk <> 0 then begin
UnhookWindowsHookEx(mhhk);
mhhk := 0;
UnhookWindowsHookEx(ghhk);
ghhk := 0;
end;
end;
procedure StopMouseMonitor(Callback: TMouseMonHandler);
var
i: Integer;
begin
if MouseMonList = nil then exit;
for i := 0 to MouseMonList.Count - 1 do
if (TMethod(TMouseMonEntry(MouseMonList[i]).Handler).Code = TMethod(Callback).Code)
and (TMethod(TMouseMonEntry(MouseMonList[i]).Handler).Data = TMethod(Callback).Data) then begin
TMouseMonEntry(MouseMonList[i]).Free;
MouseMonList.Delete(i);
if MouseMonList.Count = 0 then
ClearMonList;
exit;
end;
end;
initialization
finalization
ClearMonList;
end.