鼠标钩子处理过程


{动态链接库}

library GMouseHook;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }

uses
  SysUtils,
  Classes,
  wintypes,
  winprocs,
  Messages;


var
  {保存全局变量值}
  IsHooked :Boolean;
  HookHandle : HHook;
  DesktopWin : HWND;

{该过程在每产生一个鼠标消息调用}
function HookProc(code:Integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;
begin
  if (wParam = WM_LBUTTONDOWN) then
    MessageBeep(MB_ICONASTERISK);

  Result := CallNextHookEx(HookHandle,Code,wParam,lParam);//调用钩子链中的下一钩子
end;

function SetHook:boolean;stdcall;
begin
  Result := False;

  if IsHooked then Exit;//确认钩子已经设置

  DesktopWin := GetDesktopWindow;//获得桌面的窗口的句柄

  HookHandle := SetWindowsHookEx(WH_MOUSE,HookProc,Hinstance,0);//将钩子设置为系统全局钩子

  Result := HookHandle <>0 ;
end;

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

 
//移去钩子
if (not IsHooked) and (HookHandle <>0 ) then
    Result := UnHookWindowsHookEx(HookHandle);

  IsHooked := False; 
end;

{$R *.res}

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


begin
end.

posted on 2004-06-17 23:30  Null  阅读(513)  评论(0)    收藏  举报