Hook.pas
上传用户:mjqmds
上传日期:2022-05-05
资源大小:2827k
文件大小:2k
源码类别:

DirextX编程

开发平台:

Delphi

  1. unit Hook;
  2. interface
  3. uses Windows,Messages,Dialogs,Sysutils;
  4. var
  5.   hNextHookProc: HHook;
  6.   procSaveExit: Pointer;
  7.   function sethook:bool;export;
  8.   function hookproc(iCode:Integer;wParam: WPARAM;lParam: LPARAM):LRESULT; stdcall;
  9.   function endhook:bool;export;
  10.   procedure HotKeyHookExit;far;
  11. implementation
  12. uses Controller;
  13. function HookProc(iCode: integer; wParam: wParam; lParam: lParam):
  14. LResult; stdcall;
  15. var
  16.     hwnd:dword;
  17.     AppRect:TRect;
  18.     title:pchar;
  19. begin
  20.     result:=0;
  21.     if iCode<0 then
  22.     begin
  23.         CallNextHookEx(hnexthookproc,iCode,wParam,lParam);
  24.         result:=0;
  25.         Exit;
  26.     end;
  27.     if ((lParam and $80000000)=0) and (wParam=$6a) then
  28.     begin
  29.         hwnd:=getforegroundwindow;
  30.         try
  31.             GetWindowRect(hwnd,AppRect);
  32.             CForm.Left:=(AppRect.Right-AppRect.Left) div 2;
  33.             CForm.Top:=(AppRect.Bottom-AppRect.Top) div 2;
  34.             CForm.Show;
  35.         finally
  36.         end;
  37.         result:=1;
  38.     end;
  39. end;
  40. function sethook:bool;export;
  41. begin
  42.     result:=false;
  43.     if hnexthookproc<>0 then exit;
  44.     hNextHookProc := SetWindowsHookEx(WH_KEYBOARD,hookproc,HInstance,0);
  45.     Result := hNextHookProc <> 0;
  46. end;
  47. procedure hotkeyhookexit;
  48. begin
  49.     if hNextHookProc <> 0 then endHook;
  50.         ExitProc := procSaveExit;
  51. end;
  52. function endhook:bool;export;
  53. begin
  54.     if hNextHookProc <> 0 then
  55.     begin
  56.         UnhookWindowshookEx(hNextHookProc); // 解除 Keyboard Hook
  57.         hNextHookProc := 0;
  58.     end;
  59.     Result := hNextHookProc = 0;
  60. end;
  61. end.