-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgMouseGesture.pas
443 lines (377 loc) · 11 KB
/
gMouseGesture.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
unit gMouseGesture;
{
Mouse Gesture Component for Delphi
written by Wolfy(http://hp.vector.co.jp/authors/VA024591/)
license BSD
modified 2002/10/12
thanks to OpenJane, i had written.
}
{$IFDEF VER130}
{$ELSE}
{$WARN SYMBOL_PLATFORM OFF}
{$WARN UNIT_PLATFORM OFF}
{$ENDIF}
interface
uses
Windows, Messages, SysUtils, Classes,forms,controls;
type
TMouseGestureType = (mgUp,mgDown,mgLeft,mgRight,
mgLeftClick,mgWheelClick,mgWheelUp,mgWheelDown,
mgSide1,mgSide2,mgSide1R,mgSide2R);
TMouseGestureArray = array of TMouseGestureType;
TMouseGestureExecuteEvent = procedure(Sender: TObject; Gestures: TMouseGestureArray) of object;
TMouseGestureNotifyEvent = procedure(Sender: TObject; Gesture: TMouseGestureType) of object;
TAppMessageHook = class(TComponent)
private
FOldMessageEvent: TMessageEvent;
protected
procedure DoMessage(var Msg: TMsg; var Handled: Boolean); virtual;
procedure Loaded; override;
public
destructor Destroy; override;
end;
TgMouseGesture = class(TAppMessageHook)
private
FPrevPoint: TPoint;
FClickGestureStandby: Boolean;
FMargin: Integer;
FTempMoveGestures: TMouseGestureArray;
FEnabled: Boolean;
FTempControl: TWinControl;
FTempWndMethod: TWndMethod;
FRightClickSelect: Boolean;
FOnMouseGestureExecute: TMouseGestureExecuteEvent;
FOnMouseGestureNotify: TMouseGestureNotifyEvent;
FOnMouseGestureNotifyMove: TMouseGestureExecuteEvent;
protected
procedure DoMessage(var Msg: TMsg; var Handled: Boolean); override;
procedure DoMouseGesture(var Msg: TMsg; var Handled: Boolean); virtual;
procedure MoveGesture(pt: TPoint);
procedure GestureExecute(Gestures: TMouseGestureArray);
function AddGesture(Gesture: TMouseGestureType; Ary: TMouseGestureArray = nil): TMouseGestureArray;
procedure SubClassMessageHook(var Message: TMessage); virtual;
procedure ResetSubClass;
procedure SetSubClass(Handle: HWND);
procedure Notification(AComponent: TComponent; Operation: TOperation);override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
//マウス移動を認識する距離
property Margin: Integer read FMargin write FMargin;
//有効/無効
property Enabled: Boolean read FEnabled write FEnabled;
//ListView,TreeViewでの右クリック選択を有効
property RightClickSelect: Boolean read FRightClickSelect write FRightClickSelect;
property OnMouseGestureExecute: TMouseGestureExecuteEvent read FOnMouseGestureExecute write FOnMouseGestureExecute;
property OnMouseGestureNotify: TMouseGestureNotifyEvent read FOnMouseGestureNotify write FOnMouseGestureNotify;
property OnMouseGestureNotifyMove: TMouseGestureExecuteEvent read FOnMouseGestureNotifyMove write FOnMouseGestureNotifyMove;
end;
function MouseGestureToString(Gesture: TMouseGestureType): String;
function MouseGestureArrayToString(Gestures: TMouseGestureArray): String;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Main1', [TgMouseGesture]);
end;
const
CRLF = sLineBreak;
WM_CONTROLRECREATED = WM_USER + 020103;
WM_XBUTTONDOWN = $020B;
WM_XBUTTONUP = $020C;
WM_XBUTTONDBLCLK = $020D;
MK_XBUTTON1 = $0001;
MK_XBUTTON2 = $0002;
function MouseGestureToString(Gesture: TMouseGestureType): String;
begin
case Gesture of
mgUp: Result := '↑';
mgDown: Result := '↓';
mgLeft: Result := '←';
mgRight: Result := '→';
mgLeftClick: Result := 'LeftClick';
mgWheelClick: Result := 'WheelClick';
mgWheelUp: Result := 'WheelUp';
mgWheelDown: Result := 'WheelDown';
mgSide1: Result := 'Side1';
mgSide2: Result := 'Side2';
mgSide1R: Result := 'Side1R';
mgSide2R: Result := 'Sede2R';
end;
end;
function MouseGestureArrayToString(Gestures: TMouseGestureArray): String;
var
i: Integer;
g: TMouseGestureType;
begin
for i := 0 to Length(Gestures) - 1 do
begin
g := Gestures[i];
case g of
mgUp,mgDown,mgLeft,mgRight:
Result := Result + MouseGestureToString(g);
else
if Result = '' then
Result := MouseGestureToString(g)
else
Result := Result + CRLF + MouseGestureToString(g);
end;
end;
end;
{ TAppMessageHook }
destructor TAppMessageHook.Destroy;
//破棄
begin
//終わり
if Assigned(FOldMessageEvent) then
Application.OnMessage := FOldMessageEvent;
FOldMessageEvent := nil;
inherited;
end;
procedure TAppMessageHook.DoMessage(var Msg: TMsg; var Handled: Boolean);
//本来のメソッドを実行
begin
if Assigned(FOldMessageEvent) then
FOldMessageEvent(Msg,Handled);
end;
procedure TAppMessageHook.Loaded;
//開始前準備
begin
inherited;
//イベントを入れ替え
FOldMessageEvent := Application.OnMessage;
Application.OnMessage := DoMessage;
end;
{ TgMouseGesture }
function TgMouseGesture.AddGesture(
Gesture: TMouseGestureType; Ary: TMouseGestureArray): TMouseGestureArray;
//ジェスチャーを加える
begin
Result := Ary;
if Length(Result) = 0 then
begin
//作成する
SetLength(Result,1);
Result[0] := Gesture;
end
else begin
//追加する
if Result[High(Result)] <> Gesture then
begin
SetLength(Result,Length(Result) + 1);
Result[High(Result)] := Gesture;
end;
end;
end;
constructor TgMouseGesture.Create(AOwner: TComponent);
begin
inherited;
FMargin := 5;
FEnabled := True;
FRightClickSelect := False;
end;
destructor TgMouseGesture.Destroy;
begin
ResetSubClass;
inherited;
end;
procedure TgMouseGesture.DoMessage(var Msg: TMsg; var Handled: Boolean);
begin
if FEnabled then
DoMouseGesture(Msg,Handled);
inherited;
end;
procedure TgMouseGesture.DoMouseGesture(var Msg: TMsg;
var Handled: Boolean);
//マウスジェスチャー
begin
case Msg.message of
WM_RBUTTONDOWN: //開始
begin
FTempMoveGestures := nil;
FPrevPoint := Msg.pt;
//右クリック選択を有効にする
if FRightClickSelect then
begin
Handled := False;
//サブクラス
SetSubClass(Msg.hwnd);
end
else
Handled := True;
end;
WM_RBUTTONUP: //実行
begin
if (Length(FTempMoveGestures) > 0) and (Msg.wParam = 0) then
begin
GestureExecute(FTempMoveGestures);
Handled := True;
end
else
Handled := False;
FClickGestureStandby := False;
//リセット
ResetSubClass;
end;
WM_MOUSEMOVE: //ジェスチャーをチェック
if Msg.wParam = MK_RBUTTON then
begin
MoveGesture(Msg.pt);
Handled := True;
end;
WM_LBUTTONUP: //左ボタン
if FClickGestureStandby then
begin
FClickGestureStandby := False;
Handled := True;
GestureExecute(AddGesture(mgLeftClick));
end;
WM_MBUTTONUP: //ホイールボタン
if FClickGestureStandby then
begin
FClickGestureStandby := False;
Handled := True;
GestureExecute(AddGesture(mgWheelClick));
end;
WM_XBUTTONUP: //横ボタン
if FClickGestureStandby then
begin
FClickGestureStandby := False;
Handled := True;
if (GetKeyState(VK_RBUTTON) < 0) then
case HiWord(Msg.wParam) of
MK_XBUTTON1: GestureExecute(AddGesture(mgSide1R));
MK_XBUTTON2: GestureExecute(AddGesture(mgSide2R));
end
else begin
case HiWord(Msg.wParam) of
MK_XBUTTON1: GestureExecute(AddGesture(mgSide1));
MK_XBUTTON2: GestureExecute(AddGesture(mgSide2));
end;
end;
end;
WM_LBUTTONDOWN:
if Msg.wParam = (MK_RBUTTON + MK_LBUTTON) then
begin
FClickGestureStandby := True;
Handled := True;
end;
WM_MBUTTONDOWN:
if Msg.wParam = (MK_RBUTTON + MK_MBUTTON) then
begin
FClickGestureStandby := True;
Handled := True;
end;
WM_XBUTTONDOWN:
if (HiWord(Msg.wParam) = MK_XBUTTON1) or
(HiWord(Msg.wParam) = MK_XBUTTON2) then
begin
FClickGestureStandby := True;
Handled := (GetKeyState(VK_RBUTTON) < 0);
end;
WM_MOUSEWHEEL:
if LoWord(Msg.wParam) = MK_RBUTTON then
begin
if (Msg.wParam > 0) then
GestureExecute(AddGesture(mgWheelUp))
else
GestureExecute(AddGesture(mgWheelDown));
Handled := True;
end;
end;
end;
procedure TgMouseGesture.GestureExecute(Gestures: TMouseGestureArray);
//実行する
begin
if Assigned(FOnMouseGestureExecute) then
FOnMouseGestureExecute(Self,Gestures);
end;
procedure TgMouseGesture.MoveGesture(pt: TPoint);
//ジェスチャーをチェック
var
temp: Extended;
distance: TPoint;
arrow: TMouseGestureType;
len: Integer;
begin
distance.X := Abs(pt.X - FPrevPoint.X);
distance.Y := Abs(pt.Y - FPrevPoint.Y);
if (distance.X > FMargin) or
(distance.Y > FMargin) then
begin
temp := distance.Y / (distance.X + 0.001);
if temp >= 1 then
begin
if pt.Y > FPrevPoint.Y then
arrow := mgDown
else
arrow := mgUp;
end else
begin
if pt.X > FPrevPoint.X then
arrow := mgRight
else
arrow := mgLeft;
end;
len := Length(FTempMoveGestures);
FTempMoveGestures := AddGesture(arrow,FTempMoveGestures);
if len <> Length(FTempMoveGestures) then
begin
//イベント
if Assigned(FOnMouseGestureNotify) then
FOnMouseGestureNotify(Self,arrow);
if Assigned(FOnMouseGestureNotifyMove) then
FOnMouseGestureNotifyMove(Self,FTempMoveGestures);
end;
FPrevPoint := pt;
end;
end;
procedure TgMouseGesture.Notification(AComponent: TComponent;
Operation: TOperation);
//サブクラスの終了通知
begin
if Operation = opRemove then
ResetSubClass;
inherited;
end;
procedure TgMouseGesture.ResetSubClass;
//初期化する
begin
if Assigned(FTempControl) then
begin
//元に戻す
FTempControl.WindowProc := FTempWndMethod;
FTempControl.RemoveFreeNotification(Self);
FTempControl := nil;
FTempWndMethod := nil;
end;
end;
procedure TgMouseGesture.SetSubClass(Handle: HWND);
//新しいWndProcをセット
begin
//前のをリセット
ResetSubClass;
//コントロールを得る
FTempControl := FindControl(Handle);
if Assigned(FTempControl) then
begin
//終了通知を要求
FTempControl.FreeNotification(Self);
//WndPrco入れ替え
FTempWndMethod := FTempControl.WindowProc;
FTempControl.WindowProc := SubClassMessageHook;
end;
end;
procedure TgMouseGesture.SubClassMessageHook(var Message: TMessage);
//サブクラス
begin
case Message.Msg of
WM_CONTEXTMENU: //無効化する
if (GetKeyState(VK_RBUTTON) < 0) then
Message.Result := 1;
end;
if Assigned(FTempWndMethod) then
FTempWndMethod(Message);
end;
end.