鼠标钩子不工作? ( 积分: 100 )

  • 主题发起人 主题发起人 man8888
  • 开始时间 开始时间
M

man8888

Unregistered / Unconfirmed
GUEST, unregistred user!
这是个捕获鼠标键盘是否有输入的类,但是鼠标钩子函数MouseHookCallBack总也不起作用,不知道喂虾米???。。

unit IdleTimer;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls;
type
TIdleTimer = class(TTimer)
private
function GetSnooze: DWORD;
procedure SetSnooze(const Value: DWORD);
public
constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
property Snooze: DWORD read GetSnooze write SetSnooze;
end;

implementation

var
Instances: integer;
CurrentTime: DWORD;
whKeyBoard, whMouse:HHook;

{ TIdleTimer }

function MouseHookCallBack(Code:integer; Msg:lParam; MouseHook:wParam): DWORD;stdcall;
begin
if Code >= 0 then
CurrentTime := GetTickCount;
Result := CallNextHookEx(whMouse, Code, Msg, MouseHook);
end;

function KeyBoardCallBack(Code:integer; Msg:word; KeyBoardHook:Longint): LongInt;stdcall;
begin
if Code >= 0 then
CurrentTime := GetTickCount;
Result := CallNextHookEx(whKeyBoard, Code, Msg, KeyBoardHook);
end;

constructor TIdleTimer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Inc(Instances);
if Instances =1 then
begin
CurrentTime :=GetTickCount;
whMouse := SetWindowsHookEx(WH_MOUSE,@MouseHookCallback,HInstance,0);
whKeyBoard :=SetWindowsHookEx(WH_KEYBOARD,@KeyBoardCallBack,HInstance,0);
end;
end;

destructor TIdleTimer.Destroy;
begin
Dec(Instances);
if Instances =0 then
begin
UnhookWindowsHookEx(whKeyBoard);
UnhookWindowsHookEx(whMouse);
end;
inherited;
end;

function TIdleTimer.GetSnooze: DWORD;
begin
Result:= GetTickCount - CurrentTime;
end;

procedure TIdleTimer.SetSnooze(const Value: DWORD);
begin
CurrentTime := GetTickCount + Value;
end;

end.

//==================================================

测试代码:

var
Form1: TForm1;
IdleTimer: TIdleTimer;

implementation

uses Unit2;

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
IdleTimer:= TIdleTimer.Create(Self);
IdleTimer.Interval:= 100;
IdleTimer.OnTimer:= MyOnTimer;
IdleTimer.Enabled:= true;
end;

procedure TForm1.MyOnTimer(Sender: TObject);
begin
Edit1.Text:= IntToStr( IdleTimer.Snooze );
end;
 
嘿嘿!
在应用程序里面就要下捕鼠夹,不行啊!
仔细阅读SetWindowsHookEx啊,里面的回调事件必须在DLL中啊!
 
不是吧?必须在DLL中么?[:(][:(]
 
wql说的没错,

设置全局钩子,必须放在Dll中
 
后退
顶部