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.             CForm:=TCForm.CreateParented(hwnd);
  32.             GetWindowRect(hwnd,AppRect);
  33.             CForm.Left:=(AppRect.Right-AppRect.Left) div 2;
  34.             CForm.Top:=(AppRect.Bottom-AppRect.Top) div 2;
  35.             CForm.Show;
  36.             CForm.BringToFront;
  37.         finally
  38.             CForm.Free;
  39.         end;
  40.         result:=1;
  41.     end;
  42. end;
  43. function sethook:bool;export;
  44. begin
  45.     result:=false;
  46.     if hnexthookproc<>0 then exit;
  47.     hNextHookProc := SetWindowsHookEx(WH_KEYBOARD,hookproc,HInstance,0);
  48.     Result := hNextHookProc <> 0;
  49. end;
  50. procedure hotkeyhookexit;
  51. begin
  52.     if hNextHookProc <> 0 then endHook;
  53.         ExitProc := procSaveExit;
  54. end;
  55. function endhook:bool;export;
  56. begin
  57.     if hNextHookProc <> 0 then
  58.     begin
  59.         UnhookWindowshookEx(hNextHookProc); // 解除 Keyboard Hook
  60.         hNextHookProc := 0;
  61.     end;
  62.     Result := hNextHookProc = 0;
  63. end;
  64. end.