-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxLogic.ApplicationMessageManager.pas
More file actions
745 lines (677 loc) · 19.6 KB
/
MaxLogic.ApplicationMessageManager.pas
File metadata and controls
745 lines (677 loc) · 19.6 KB
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
unit MaxLogic.ApplicationMessageManager;
{
Version: 1;.2
how to use:
for auto scrolling of scrollboxes do the following:
fRedirectMouseWheel := TAppMessagehandlerRedirectMouseWheel.Create;
// no registration is needed, as the handlers will auto register themselves
}
interface
uses
winApi.windows, system.classes, vcl.controls, system.sysUtils, messages, vcl.forms, generics.collections;
type
// forward declarations
TApplicationMessageHandler = class;
TApplicationMessageManager = class;
// singelton, there may onle be one. it is attached to the application.onmessage event
TApplicationMessageManager = class
private
class var fInstance: TApplicationMessageManager;
private
FAppMessageHandlers: TList<TApplicationMessageHandler>;
procedure AppMessage(var Msg: TMsg; var Handled: Boolean);
class function GetSingeltonInstance: TApplicationMessageManager; static;
class Destructor DestroyClass;
class Procedure RegisterHandler(aHandler: TApplicationMessageHandler);
class Procedure UnRegisterHandler(aHandler: TApplicationMessageHandler);
public
Constructor Create;
Destructor Destroy; override;
class function Receivername(wnd: HWnd): string;
property AppMessageHandlers: TList<TApplicationMessageHandler> read FAppMessageHandlers;
class property instance: TApplicationMessageManager read GetSingeltonInstance;
end;
// is the base class for all the handlers
TApplicationMessageHandler = class
private
fParent: TApplicationMessageManager;
PROTECTED
procedure AppMessage(var Msg: TMsg; var Handled: Boolean); VIRTUAL; ABSTRACT;
procedure MoveToTopParent(var f: TForm);
public
constructor Create;
Destructor Destroy; override;
class function MessageToText(aMsg: Cardinal): string;
property Parent: TApplicationMessageManager read fParent;
end;
TAppMessagehandlerRedirectMouseWheel = class(TApplicationMessageHandler)
private
FAutoScrollScrollBoxes: Boolean;
procedure SetAutoScrollScrollBoxes(const Value: Boolean);
private
procedure DoScroll(sb: TScrollbox; var Msg: TMsg);
PROTECTED
procedure AppMessage(var Msg: TMsg; var Handled: Boolean); override;
public
constructor Create;
property AutoScrollScrollBoxes: Boolean read FAutoScrollScrollBoxes write SetAutoScrollScrollBoxes;
end;
TAppMessageHandlerLogKey = class(TApplicationMessageHandler)
PROTECTED
procedure AppMessage(var Msg: TMsg; var Handled: Boolean); override;
procedure ProcessKeyMessage(var Msg: TMsg; var Handled: Boolean); virtual;
public
class function DisplayKeyMsg(const Msg: TMsg): string;
class function KeyName(Keydata: longint): string;
// Keydata is the Msg.lparam
class function RepeatCount(Keydata: lparam): lparam;
// lParam is a nativeInt
class function Scancode(Keydata: lparam): lparam;
class function Flags(Keydata: lparam): string;
end;
THybernateAppMessageHandler = class(TApplicationMessageHandler)
private
fOnGoToHybernate, fOnWakeUpFromHybernate: TNotifyEvent;
PROTECTED
procedure AppMessage(var Msg: TMsg; var Handled: Boolean); override;
public
constructor Create(aOnGoToHybernate, aOnWakeUpFromHybernate: TNotifyEvent);
end;
implementation
{ TApplicationMessageManager }
procedure TApplicationMessageManager.AppMessage(var Msg: TMsg; var Handled: Boolean);
var
H: TApplicationMessageHandler;
begin
for H in FAppMessageHandlers do
begin
H.AppMessage(Msg, Handled);
if Handled then
BREAK;
end;
end;
class function TApplicationMessageManager.Receivername(wnd: HWnd): string;
var
ctrl: TWinControl;
begin
ctrl := FindControl(wnd);
if Assigned(ctrl) then
Result := ctrl.Name
else
Result := IntToHex(Cardinal(wnd), 8);
end;
class procedure TApplicationMessageManager.RegisterHandler(aHandler: TApplicationMessageHandler);
begin
aHandler.fParent := GetSingeltonInstance;
GetSingeltonInstance.FAppMessageHandlers.add(aHandler);
end;
class procedure TApplicationMessageManager.UnRegisterHandler(aHandler: TApplicationMessageHandler);
var
i: Integer;
begin
i := fInstance.FAppMessageHandlers.indexof(aHandler);
if i <> -1 then
fInstance.FAppMessageHandlers.delete(i);
end;
constructor TApplicationMessageManager.Create;
begin
inherited Create;
FAppMessageHandlers := TList<TApplicationMessageHandler>.Create;
Application.OnMessage := AppMessage;
end;
destructor TApplicationMessageManager.Destroy;
begin
Application.OnMessage := nil;
FAppMessageHandlers.free;
inherited;
end;
class destructor TApplicationMessageManager.DestroyClass;
begin
if Assigned(fInstance) then
FreeAndNIL(fInstance);
end;
class function TApplicationMessageManager.GetSingeltonInstance: TApplicationMessageManager;
begin
if not Assigned(fInstance) then
fInstance := TApplicationMessageManager.Create;
Result := fInstance;
end;
{ TAppMessageHandlerLogKey }
procedure TAppMessageHandlerLogKey.AppMessage(var Msg: TMsg; var Handled: Boolean);
begin
// the only messages that we are interesting in are the key events.
case Msg.message of
WM_KEYFIRST .. WM_KEYLAST:
ProcessKeyMessage(Msg, Handled);
// end of case
end;
end;
{ : Display the information contained in the message parameters for a key message.
@param Msg is the original message record.
@desc See the MS Platform SDK docs for one of the key messsages for a description of the message parameters. The decoding of the individual parts is left to helper methods. }
class function TAppMessageHandlerLogKey.DisplayKeyMsg(const Msg: TMsg): string;
var
PWmKey: ^TWMKey;
{ : Returns the key label, usually matching the text shown on the key caps, or the character created by the key. }
function KeyAsstring: string;
begin
case Msg.message of
WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, WM_SYSKEYUP:
Result := KeyName(PWmKey^.Keydata);
else
Result := Chr(PWmKey^.CharCode);
end; { case }
end;
{ : Returns a prefix to use in the display string for key or character messages. }
function CharOrKey: string;
begin
case Msg.message of
WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, WM_SYSKEYUP:
Result := 'Key';
else
Result := 'Char';
end; { case }
end;
var
ScancodeStr: string;
begin
Result := '';
PWmKey := @Msg.message;
Try
ScancodeStr := '$' + IntToHex(Scancode(Msg.lparam), 1);
except
ScancodeStr := '[$' + IntToHex(Msg.lparam, 1) + ']';
end;
Result := format(
'Message: %s, Receiver: %s, %scode: $%x (%s), ' +
'Repeat count %d, Scancode %s, Flags: %s',
[MessageToText(Msg.message),
TApplicationMessageManager.Receivername(Msg.HWnd),
CharOrKey,
PWmKey^.CharCode,
KeyAsstring,
RepeatCount(Msg.lparam),
ScancodeStr,
Flags(Msg.lparam)]);
end;
class function TAppMessageHandlerLogKey.Flags(Keydata: lparam): string;
var
FlagByte: Byte;
sl: TStringlist;
begin
{$R-}{$Q-}
FlagByte := Hi(Hiword(Keydata));
if FlagByte <> 0 then
begin
sl := TStringlist.Create;
try
{ Bit 0 identifies an extended key like the right Ctrl and Alt }
if (FlagByte and 1) <> 0 then
sl.add('ExtendedKey');
{ Bit 5 is set when the Alt key is down }
if (FlagByte and $20) <> 0 then
sl.add('AltDown');
{ Bit 6 is the previous state of the key, down (0) or up (1) }
if (FlagByte and $40) <> 0 then
sl.add('KeyIsDown');
{ Bit 7 is the transition state, 0 if the key went down, 1 if it
went up. }
if (FlagByte and $80) <> 0 then
sl.add('KeyUp');
Result := format('[%s]', [sl.Commatext]);
finally
sl.free;
end; { finally }
end { if }
else
Result := '[]';
{$R+}{$Q+}
end;
class function TAppMessageHandlerLogKey.KeyName(Keydata: Integer): string;
var
Buffer: array [0 .. 64] of char;
begin
Buffer[0] := #0;
GetKeyNameText(Keydata, Buffer, Sizeof(Buffer));
Result := Buffer;
end;
procedure TAppMessageHandlerLogKey.ProcessKeyMessage(var Msg: TMsg; var Handled: Boolean);
var
s: string;
begin
s := DisplayKeyMsg(Msg);
end;
class function TAppMessageHandlerLogKey.RepeatCount(Keydata: lparam): lparam;
begin
Result := Keydata and $FFFF;
end;
class function TAppMessageHandlerLogKey.Scancode(Keydata: lparam): lparam;
begin
{$R-}{$Q-}
Result := Hiword(Keydata) and $FF;
{$R+}{$Q+}
end;
{ TApplicationMessageHandler }
destructor TApplicationMessageHandler.Destroy;
begin
if fParent <> nil then
begin
fParent.UnRegisterHandler(self);
fParent := nil;
end;
inherited;
end;
class function TApplicationMessageHandler.MessageToText(aMsg: Cardinal): string;
{ : Convert a message constant to a display string. We only expect key and mouse messages here, anything else will just return the message constants value as a hexadecimal representation. }
const
WM_XBUTTONDOWN = $020B;
WM_XBUTTONUP = $020C;
WM_XBUTTONDBLCLK = $020D;
begin
case aMsg of
WM_COMMAND:
Result := 'wm_COMMAND';
WM_SYSCOMMAND:
Result := 'WM_SYSCOMMAND';
WM_KEYDOWN:
Result := 'WM_KEYDOWN';
WM_SYSKEYDOWN:
Result := 'WM_SYSKEYDOWN';
WM_CHAR:
Result := 'WM_CHAR';
WM_SYSCHAR:
Result := 'WM_SYSCHAR';
WM_KEYUP:
Result := 'WM_KEYUP';
WM_SYSKEYUP:
Result := 'WM_SYSKEYUP';
WM_SYSDEADCHAR:
Result := 'WM_SYSDEADCHAR';
WM_DEADCHAR:
Result := 'WM_DEADCHAR';
WM_UNICHAR:
Result := 'WM_UNICHAR';
WM_NCMOUSEMOVE:
Result := 'WM_NCMOUSEMOVE';
WM_NCLBUTTONDOWN:
Result := 'WM_NCLBUTTONDOWN';
WM_NCLBUTTONUP:
Result := 'WM_NCLBUTTONUP';
WM_NCLBUTTONDBLCLK:
Result := 'WM_NCLBUTTONDBLCLK';
WM_NCRBUTTONDOWN:
Result := 'WM_NCRBUTTONDOWN';
WM_NCRBUTTONUP:
Result := 'WM_NCRBUTTONUP';
WM_NCRBUTTONDBLCLK:
Result := 'WM_NCRBUTTONDBLCLK';
WM_NCMBUTTONDOWN:
Result := 'WM_NCMBUTTONDOWN';
WM_NCMBUTTONUP:
Result := 'WM_NCMBUTTONUP';
WM_NCMBUTTONDBLCLK:
Result := 'WM_NCMBUTTONDBLCLK';
WM_NCXBUTTONDOWN:
Result := 'WM_NCXBUTTONDOWN';
WM_NCXBUTTONUP:
Result := 'WM_NCXBUTTONUP';
WM_NCXBUTTONDBLCLK:
Result := 'WM_NCXBUTTONDBLCLK';
WM_MOUSEMOVE:
Result := 'WM_MOUSEMOVE';
WM_LBUTTONDOWN:
Result := 'WM_LBUTTONDOWN';
WM_LBUTTONUP:
Result := 'WM_LBUTTONUP';
WM_LBUTTONDBLCLK:
Result := 'WM_LBUTTONDBLCLK';
WM_RBUTTONDOWN:
Result := 'WM_RBUTTONDOWN';
WM_RBUTTONUP:
Result := 'WM_RBUTTONUP';
WM_RBUTTONDBLCLK:
Result := 'WM_RBUTTONDBLCLK';
WM_MBUTTONDOWN:
Result := 'WM_MBUTTONDOWN';
WM_MBUTTONUP:
Result := 'WM_MBUTTONUP';
WM_MBUTTONDBLCLK:
Result := 'WM_MBUTTONDBLCLK';
WM_MOUSEWHEEL:
Result := 'WM_MOUSEWHEEL';
WM_XBUTTONDOWN:
Result := 'WM_XBUTTONDOWN';
WM_XBUTTONUP:
Result := 'WM_XBUTTONUP';
WM_XBUTTONDBLCLK:
Result := 'WM_XBUTTONDBLCLK';
WM_MOUSEHOVER:
Result := 'WM_MOUSEHOVER';
WM_MOUSELEAVE:
Result := 'WM_MOUSELEAVE';
WM_NCMOUSEHOVER:
Result := 'WM_NCMOUSEHOVER';
WM_NCMOUSELEAVE:
Result := 'WM_NCMOUSELEAVE';
WM_TIMER:
Result := 'WM_TIMER';
WM_GETDLGCODE:
Result := 'WM_GETDLGCODE';
CM_ACTIVATE:
Result := 'CM_ACTIVATE';
CM_DEACTIVATE:
Result := 'CM_DEACTIVATE';
CM_GOTFOCUS:
Result := 'CM_GOTFOCUS';
CM_LOSTFOCUS:
Result := 'CM_LOSTFOCUS';
CM_CANCELMODE:
Result := 'CM_CANCELMODE';
CM_DIALOGKEY:
Result := 'CM_DIALOGKEY';
CM_DIALOGCHAR:
Result := 'CM_DIALOGCHAR';
CM_FOCUSCHANGED:
Result := 'CM_FOCUSCHANGED';
CM_PARENTFONTCHANGED:
Result := 'CM_PARENTFONTCHANGED';
CM_PARENTCOLORCHANGED:
Result := 'CM_PARENTCOLORCHANGED';
CM_HITTEST:
Result := 'CM_HITTEST';
CM_VISIBLECHANGED:
Result := 'CM_VISIBLECHANGED';
CM_ENABLEDCHANGED:
Result := 'CM_ENABLEDCHANGED';
CM_COLORCHANGED:
Result := 'CM_COLORCHANGED';
CM_FONTCHANGED:
Result := 'CM_FONTCHANGED';
CM_CURSORCHANGED:
Result := 'CM_CURSORCHANGED';
CM_CTL3DCHANGED:
Result := 'CM_CTL3DCHANGED';
CM_PARENTCTL3DCHANGED:
Result := 'CM_PARENTCTL3DCHANGED';
CM_TEXTCHANGED:
Result := 'CM_TEXTCHANGED';
CM_MOUSEENTER:
Result := 'CM_MOUSEENTER';
CM_MOUSELEAVE:
Result := 'CM_MOUSELEAVE';
CM_MENUCHANGED:
Result := 'CM_MENUCHANGED';
CM_APPKEYDOWN:
Result := 'CM_APPKEYDOWN';
CM_APPSYSCOMMAND:
Result := 'CM_APPSYSCOMMAND';
CM_BUTTONPRESSED:
Result := 'CM_BUTTONPRESSED';
CM_SHOWINGCHANGED:
Result := 'CM_SHOWINGCHANGED';
CM_ENTER:
Result := 'CM_ENTER';
CM_EXIT:
Result := 'CM_EXIT';
CM_DESIGNHITTEST:
Result := 'CM_DESIGNHITTEST';
CM_ICONCHANGED:
Result := 'CM_ICONCHANGED';
CM_WANTSPECIALKEY:
Result := 'CM_WANTSPECIALKEY';
CM_INVOKEHELP:
Result := 'CM_INVOKEHELP';
CM_WINDOWHOOK:
Result := 'CM_WINDOWHOOK';
CM_RELEASE:
Result := 'CM_RELEASE';
CM_SHOWHINTCHANGED:
Result := 'CM_SHOWHINTCHANGED';
CM_PARENTSHOWHINTCHANGED:
Result := 'CM_PARENTSHOWHINTCHANGED';
CM_SYSCOLORCHANGE:
Result := 'CM_SYSCOLORCHANGE';
CM_WININICHANGE:
Result := 'CM_WININICHANGE';
CM_FONTCHANGE:
Result := 'CM_FONTCHANGE';
CM_TIMECHANGE:
Result := 'CM_TIMECHANGE';
CM_TABSTOPCHANGED:
Result := 'CM_TABSTOPCHANGED';
CM_UIACTIVATE:
Result := 'CM_UIACTIVATE';
CM_UIDEACTIVATE:
Result := 'CM_UIDEACTIVATE';
CM_DOCWINDOWACTIVATE:
Result := 'CM_DOCWINDOWACTIVATE';
CM_CONTROLLISTCHANGE:
Result := 'CM_CONTROLLISTCHANGE';
CM_GETDATALINK:
Result := 'CM_GETDATALINK';
CM_CHILDKEY:
Result := 'CM_CHILDKEY';
CM_DRAG:
Result := 'CM_DRAG';
CM_HINTSHOW:
Result := 'CM_HINTSHOW';
CM_DIALOGHANDLE:
Result := 'CM_DIALOGHANDLE';
CM_ISTOOLCONTROL:
Result := 'CM_ISTOOLCONTROL';
CM_RECREATEWND:
Result := 'CM_RECREATEWND';
CM_INVALIDATE:
Result := 'CM_INVALIDATE';
CM_SYSFONTCHANGED:
Result := 'CM_SYSFONTCHANGED';
CM_CONTROLCHANGE:
Result := 'CM_CONTROLCHANGE';
CM_CHANGED:
Result := 'CM_CHANGED';
CM_DOCKCLIENT:
Result := 'CM_DOCKCLIENT';
CM_UNDOCKCLIENT:
Result := 'CM_UNDOCKCLIENT';
CM_FLOAT:
Result := 'CM_FLOAT';
CM_BORDERCHANGED:
Result := 'CM_BORDERCHANGED';
CM_BIDIMODECHANGED:
Result := 'CM_BIDIMODECHANGED';
CM_PARENTBIDIMODECHANGED:
Result := 'CM_PARENTBIDIMODECHANGED';
CM_ALLCHILDRENFLIPPED:
Result := 'CM_ALLCHILDRENFLIPPED';
CM_ACTIONUPDATE:
Result := 'CM_ACTIONUPDATE';
CM_ACTIONEXECUTE:
Result := 'CM_ACTIONEXECUTE';
CM_HINTSHOWPAUSE:
Result := 'CM_HINTSHOWPAUSE';
CM_DOCKNOTIFICATION:
Result := 'CM_DOCKNOTIFICATION';
CM_MOUSEWHEEL:
Result := 'CM_MOUSEWHEEL';
CM_ISSHORTCUT:
Result := 'CM_ISSHORTCUT';
CM_INVALIDATEDOCKHOST:
Result := 'CM_INVALIDATEDOCKHOST';
CM_SETACTIVECONTROL:
Result := 'CM_SETACTIVECONTROL';
CM_POPUPHWNDDESTROY:
Result := 'CM_POPUPHWNDDESTROY';
CM_CREATEPOPUP:
Result := 'CM_CREATEPOPUP';
CM_DESTROYHANDLE:
Result := 'CM_DESTROYHANDLE';
CM_MOUSEACTIVATE:
Result := 'CM_MOUSEACTIVATE';
CM_CONTROLLISTCHANGING:
Result := 'CM_CONTROLLISTCHANGING';
CM_BUFFEREDPRINTCLIENT:
Result := 'CM_BUFFEREDPRINTCLIENT';
CM_UNTHEMECONTROL:
Result := 'CM_UNTHEMECONTROL';
CN_CHARTOITEM:
Result := 'CN_CHARTOITEM';
CN_COMMAND:
Result := 'CN_COMMAND';
CN_COMPAREITEM:
Result := 'CN_COMPAREITEM';
CN_CTLCOLORBTN:
Result := 'CN_CTLCOLORBTN';
CN_CTLCOLORDLG:
Result := 'CN_CTLCOLORDLG';
CN_CTLCOLOREDIT:
Result := 'CN_CTLCOLOREDIT';
CN_CTLCOLORLISTBOX:
Result := 'CN_CTLCOLORLISTBOX';
CN_CTLCOLORMSGBOX:
Result := 'CN_CTLCOLORMSGBOX';
CN_CTLCOLORSCROLLBAR:
Result := 'CN_CTLCOLORSCROLLBAR';
CN_CTLCOLORSTATIC:
Result := 'CN_CTLCOLORSTATIC';
CN_DELETEITEM:
Result := 'CN_DELETEITEM';
CN_DRAWITEM:
Result := 'CN_DRAWITEM';
CN_HSCROLL:
Result := 'CN_HSCROLL';
CN_MEASUREITEM:
Result := 'CN_MEASUREITEM';
CN_PARENTNOTIFY:
Result := 'CN_PARENTNOTIFY';
CN_VKEYTOITEM:
Result := 'CN_VKEYTOITEM';
CN_VSCROLL:
Result := 'CN_VSCROLL';
CN_KEYDOWN:
Result := 'CN_KEYDOWN';
CN_KEYUP:
Result := 'CN_KEYUP';
CN_CHAR:
Result := 'CN_CHAR';
CN_SYSKEYDOWN:
Result := 'CN_SYSKEYDOWN';
CN_SYSCHAR:
Result := 'CN_SYSCHAR';
CN_NOTIFY:
Result := 'CN_NOTIFY';
else
Result := format('$%4.4X', [aMsg]);
end; { case }
end;
procedure TApplicationMessageHandler.MoveToTopParent(var f: TForm);
var
wc: TWinControl;
begin
wc := f;
while wc.Parent <> nil do
begin
wc := wc.Parent;
if wc is TForm then
f := wc as TForm;
end;
end;
constructor TApplicationMessageHandler.Create;
begin
inherited Create;
TApplicationMessageManager.RegisterHandler(self);
end;
{ TAppMessagehandlerRedirectMouseWheel }
procedure TAppMessagehandlerRedirectMouseWheel.AppMessage(var Msg: TMsg; var Handled: Boolean);
var
form: TForm;
FirstIteration, res: Boolean;
s: string;
control: TControl;
begin
// mouse wheel scrolling for the control under the mouse
if not AutoScrollScrollBoxes then
exit;
if Msg.message <> WM_MOUSEWHEEL then
exit;
form := screen.ActiveForm;
if form = nil then
begin
Handled := true;
exit;
end;
MoveToTopParent(form);
if (form.activecontrol = nil) or (form.activecontrol.Handle <> Msg.HWnd) then
begin
// ControlAtPos() gets the immediate child, so if the control is in panel, it returns the panel.
// FindVCLWindow(Mouse.CursorPos) returns the correct control.
// control := form.ControlAtPos(form.ScreenToClient(MousePos), False, True, True);
control := FindVCLWindow(Mouse.CursorPos);
if control <> nil then
begin
Handled := control.Perform(CM_MOUSEWHEEL, Msg.WParam, Msg.lparam) = 0;
// go up to find a TScrollBox
while control <> nil do
begin
if control is TScrollbox then
begin
DoScroll(control as TScrollbox, Msg);
Handled := true;
BREAK;
end
else
control := control.Parent;
end;
end;
end;
end;
constructor TAppMessagehandlerRedirectMouseWheel.Create;
begin
inherited;
FAutoScrollScrollBoxes := true;
end;
procedure TAppMessagehandlerRedirectMouseWheel.DoScroll(sb: TScrollbox; var Msg: TMsg);
var
WheelDelta: ShortInt;
i, NewScrollPos: Integer;
begin
{$R-}{$Q-}
WheelDelta := Hiword(Msg.WParam);
{$R+}{$Q+}
for i := 1 to Mouse.WheelScrollLines do
try
if WheelDelta > 0 then
sb.Perform(WM_VSCROLL, SB_LINEUP, 0)
else
sb.Perform(WM_VSCROLL, SB_LINEDOWN, 0);
finally
sb.Perform(WM_VSCROLL, SB_ENDSCROLL, 0);
end;
end;
procedure TAppMessagehandlerRedirectMouseWheel.SetAutoScrollScrollBoxes(const Value: Boolean);
begin
FAutoScrollScrollBoxes := Value;
end;
{ THybernateAppMessageHandler }
constructor THybernateAppMessageHandler.Create(aOnGoToHybernate,
aOnWakeUpFromHybernate: TNotifyEvent);
begin
inherited Create;
fOnGoToHybernate := aOnGoToHybernate;
fOnWakeUpFromHybernate := aOnWakeUpFromHybernate;
end;
procedure THybernateAppMessageHandler.AppMessage(var Msg: TMsg;
var Handled: Boolean);
begin
if Msg.message = WM_POWERBROADCAST then
begin
case Msg.WParam of
PBT_APMSUSPEND:
if Assigned(fOnGoToHybernate) then
fOnGoToHybernate(self);
PBT_APMRESUMESUSPEND:
if Assigned(fOnWakeUpFromHybernate) then
fOnWakeUpFromHybernate(self);
end;
end;
end;
end.