Advertisement
Borrisholt

WinEventHook

Sep 18th, 2019
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.11 KB | None | 0 0
  1. unit WinEventHook;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Generics.Collections;
  7.  
  8. TYPE
  9.   TWinEventHookHandle = THandle;
  10.  
  11.   TWinEventStruct = record
  12.     Hook: TWinEventHookHandle;
  13.     Event: DWORD;
  14.     hWnd: THandle;
  15.     idObject: Longint;
  16.     idChild: Longint;
  17.     idEventThread: DWORD;
  18.     dwmsEventTime: DWORD;
  19.     EventTime: TDateTime;
  20.     EventClass: String;
  21.   end;
  22. {$M+}
  23.  
  24. Type
  25.   TWinEventHook = class;
  26.   TWinEventHookNotify = reference to procedure(Hook: TWinEventHook; const HookMsg: TWinEventStruct);
  27.   TWinEventHookProcs = TDictionary<TWinEventHookHandle, TWinEventHook>;
  28.  
  29.   TWinEventHook = class
  30.   private
  31.     class var FWinEventHookProcs: TWinEventHookProcs;
  32.   private
  33.     FHook: hHook;
  34.     FOnExecute: TWinEventHookNotify;
  35.     FActive: Boolean;
  36.     FEventMin: DWORD;
  37.     FEventMax: DWORD;
  38.     FContext: DWORD;
  39.     procedure SetActive(const Value: Boolean);
  40.   published
  41.     property Active: Boolean read FActive write SetActive;
  42.   public
  43.     constructor Create(eventMin: DWORD; eventMax: DWORD = 0; aContext: DWORD = WINEVENT_OUTOFCONTEXT); reintroduce;
  44.     destructor Destroy; override;
  45.     class constructor Create;
  46.     class Destructor Destroy;
  47.     property OnExecute: TWinEventHookNotify read FOnExecute write FOnExecute;
  48.   end;
  49.  
  50. implementation
  51.  
  52. uses
  53.   System.SysUtils, System.DateUtils;
  54.  
  55. { TWinEventHook }
  56.  
  57. procedure WinEventProcCallBack(HWINEVENTHOOK: THandle; Event: DWORD; hWnd: hWnd; idObject, idChild: Longint; idEventThread, dwmsEventTime: DWORD); stdcall;
  58. var
  59.   WinEventHook: TWinEventHook;
  60.   WinEventStruct: TWinEventStruct;
  61.   ClassName: String;
  62. begin
  63.   if not TWinEventHook.FWinEventHookProcs.TryGetValue(HWINEVENTHOOK, WinEventHook) then
  64.     exit;
  65.  
  66.   if not WinEventHook.Active then
  67.     exit;
  68.  
  69.   WinEventStruct.Hook := HWINEVENTHOOK;
  70.   WinEventStruct.Event := Event;
  71.   WinEventStruct.hWnd := hWnd;
  72.   WinEventStruct.idObject := idObject;
  73.   WinEventStruct.idChild := idChild;
  74.   WinEventStruct.idEventThread := idEventThread;
  75.   WinEventStruct.dwmsEventTime := dwmsEventTime;
  76.  
  77.   WinEventStruct.EventTime := IncMilliSecond(Now, dwmsEventTime - GetTickCount);
  78.  
  79.   SetLength(ClassName, 255);
  80.   SetLength(ClassName, GetClassName(hWnd, pChar(ClassName), 255));
  81.   WinEventStruct.EventClass := ClassName;
  82.  
  83.   WinEventHook.FOnExecute(WinEventHook, WinEventStruct);
  84. end;
  85.  
  86. destructor TWinEventHook.Destroy;
  87. begin
  88.   Active := False;
  89.   inherited;
  90. end;
  91.  
  92. constructor TWinEventHook.Create(eventMin: DWORD; eventMax: DWORD = 0; aContext: DWORD = WINEVENT_OUTOFCONTEXT);
  93. begin
  94.   inherited Create;
  95.   FHook := 0;
  96.   FActive := False;
  97.   FEventMin := eventMin;
  98.   if eventMax = 0 then
  99.     eventMax := eventMin;
  100.  
  101.   FEventMax := eventMax;
  102.   FContext := aContext;
  103. end;
  104.  
  105. class destructor TWinEventHook.Destroy;
  106. begin
  107.   FWinEventHookProcs.Free;
  108. end;
  109.  
  110. procedure TWinEventHook.SetActive(const Value: Boolean);
  111. begin
  112.   if FActive = Value then
  113.     exit;
  114.  
  115.   FActive := Value;
  116.  
  117.   case FActive of
  118.     True:
  119.       begin
  120.         FHook := SetWinEventHook(FEventMin, FEventMax, 0, WinEventProcCallBack, 0, 0, FContext);
  121.         if (FHook = 0) then
  122.         begin
  123.           FActive := False;
  124.           raise Exception.Create(ClassName + ' CREATION FAILED!');
  125.         end;
  126.         FWinEventHookProcs.Add(FHook, Self);
  127.       end;
  128.  
  129.     False:
  130.       begin
  131.         if (FHook <> 0) then
  132.         begin
  133.           UnhookWinEvent(FHook);
  134.           FWinEventHookProcs.Remove(FHook);
  135.         end;
  136.         FHook := 0;
  137.       end;
  138.   end;
  139. end;
  140.  
  141. class constructor TWinEventHook.Create;
  142. begin
  143.   FWinEventHookProcs := TDictionary<TWinEventHookHandle, TWinEventHook>.Create();
  144. end;
  145.  
  146. end.
  147.  
  148. **** EXAMPLE OF USE ***
  149.  
  150. unit Unit1;
  151.  
  152. interface
  153.  
  154. uses
  155.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  156.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, WinEventHook;
  157.  
  158. type
  159.   TForm1 = class(TForm)
  160.     Memo1: TMemo;
  161.     procedure FormCreate(Sender: TObject);
  162.     procedure FormDestroy(Sender: TObject);
  163.   private
  164.     { Private declarations }
  165.     FStartDragHook: TWinEventHook;
  166.     FEndDragHook: TWinEventHook;
  167.     procedure HookProc(Hook: TWinEventHook; const HookMsg: TWinEventStruct);
  168.   public
  169.     { Public declarations }
  170.  
  171.   end;
  172.  
  173. var
  174.   Form1: TForm1;
  175.  
  176. implementation
  177.  
  178. {$R *.dfm}
  179.  
  180. uses
  181.   DateUtils;
  182.  
  183. procedure TForm1.FormCreate(Sender: TObject);
  184. begin
  185.   FStartDragHook := TWinEventHook.Create(EVENT_OBJECT_CREATE);
  186.  
  187.   with FStartDragHook do
  188.   begin
  189.     OnExecute := HookProc;
  190.     Active := True;
  191.   end;
  192.  
  193.   FEndDragHook := TWinEventHook.Create(EVENT_OBJECT_DESTROY);
  194.  
  195.   with FEndDragHook do
  196.   begin
  197.     OnExecute := HookProc;
  198.     Active := True;
  199.   end;
  200. end;
  201.  
  202. procedure TForm1.FormDestroy(Sender: TObject);
  203. begin
  204.   FStartDragHook.Free;;
  205.   FEndDragHook.Free;
  206. end;
  207.  
  208. procedure TForm1.HookProc(Hook: TWinEventHook; const HookMsg: TWinEventStruct);
  209. begin
  210.   if pChar(HookMsg.EventClass) = 'SysDragImage' then
  211.   begin
  212.     if HookMsg.Event = EVENT_OBJECT_CREATE then
  213.       Memo1.Lines.Add(TimeToStr(HookMsg.EventTime) + ': Drag Start')
  214.     else
  215.       Memo1.Lines.Add(TimeToStr(HookMsg.EventTime) + ': Drag End');
  216.   end;
  217. end;
  218.  
  219. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement