gmousehook.dpr

来自「Delphi Win32核心API参考光盘源码 本书包含了常用的Windows」· DPR 代码 · 共 67 行

DPR
67
字号
library GMousehook;

uses
  SysUtils,
  Classes,
  WinTypes,
  WinProcs,
  Messages;

var
  {holds various global values}
  IsHooked: Boolean;
  HookHandle: HHook;
  DesktopWin: HWND;

{this is the procedure called every time a mouse message is processed}
function HookProc(Code: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
begin
  {if the user clicked the left mouse button, output a sound}
  if (wParam = WM_LBUTTONDOWN) then
    MessageBeep(MB_ICONASTERISK);

  {call the next hook in the chain. note that leaving this out would
   effectively remove this message}
  Result := CallNextHookEx(HookHandle, Code, wParam, lParam);
end;

function SetHook: Boolean; stdcall;
begin
  Result := FALSE;

  {make sure the hook is not already set}
  if IsHooked then
    Exit;

  {get a handle to the desktop window}
  DesktopWin := GetDesktopWindow;

  {set this hook as a system level hook}
  HookHandle := SetWindowsHookEx(WH_MOUSE, HookProc, HInstance, 0);

  {indicate if the hook was set right}
  Result := HookHandle <> 0;
end;

function RemoveHook: Boolean; stdcall;
begin
  Result := FALSE;

  {remove the hook}
  if (not IsHooked) and (HookHandle <> 0) then
    Result := UnhookWindowsHookEx(HookHandle);

  {reset the global variable}
  IsHooked := FALSE;
end;

exports
  SetHook name 'SetHook',
  RemoveHook name 'RemoveHook',
  HookProc name 'HookProc';

begin
   IsHooked := FALSE;
end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?