1,183
社区成员




library Mouse_HookDLL;
uses
SysUtils,
Windows,
Messages,
Classes;
const WH_MOUSE_LL=14;
{$R *.res}
var
NextHook : HHook;
CallHandle : HWND;
MessageID : Word;
function HookProc(code:Integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;
begin
Result := 0;
if code < 0 then
Result := CallNextHookEx(NextHook,code,wParam,lParam);
case wParam of
WM_LBUTTONDOWN:
begin
end;
WM_LBUTTONUP:
begin
end;
WM_LBUTTONDBLCLK:
begin
SendMessage(CallHandle,MessageID,wParam,Integer(@pMouseHookStruct(lParam)^));
end;
WM_RBUTTONDOWN:
begin
end;
WM_RBUTTONUP:
begin
SendMessage(CallHandle,MessageID,wParam,Integer(@pMouseHookStruct(lParam)^));
end;
WM_NCMOUSEMOVE,WM_MOUSEMOVE:
begin
end;
end;
end;
//启动钩子
function StartHook(MsgID:Word):Bool;stdcall;
begin
Result := False;
if NextHook <> 0 then
Exit;
MessageID := MsgID;
NextHook := SetWindowsHookEx(WH_MOUSE_LL,@HookProc,HInstance,0);
Result := NextHook <> 0;
end;
//脱钩
function StopHook:Bool;stdcall;
begin
if NextHook <> 0 then
begin
UnHookWindowsHookEx(NextHook);
NextHook := 0;
end;
Result := NextHook = 0;
end;
//传递调用者句柄
procedure SetCallHandle(sender:HWND);stdcall;
begin
CallHandle := sender;
NextHook := 0;
end;
exports
StartHook name 'StartHook',
StopHook name 'StopHook',
SetCallHandle name 'SetCallHandle';
begin
end.
private
{ Private declarations }
//重载消息处理
procedure WndProc(var Message: TMessage);override;
const
WM_TestMsg = WM_User + 100;
implementation
{$R *.dfm}
function StartHook(MsgID:Word):Bool;stdcall;external 'Mouse_HookDLL.dll';
function StopHook:Bool;stdcall;external 'Mouse_HookDLL.dll';
procedure SetCallHandle(sender:HWND);stdcall;external 'Mouse_HookDLL.dll';
procedure TForm1.FormShow(Sender: TObject);
begin
SetCallHandle(Self.Handle);
if not StartHook(WM_TestMsg) then //WM_TestMsg为自定义消息
begin
ShowMessage('挂钩失败!');
end;
end;
procedure TForm1.WndProc(var Message: TMessage);
begin
//得到符合条件的钩子
if Message.Msg = WM_TestMsg then
begin
memo1.Lines.Add('Get it!');
end;
inherited;
end;