鼠标hook问题?(很困惑)
dudo 2003-03-21 05:46:53 我的程序需要在任何时候知道用户点下了那个鼠标键,所以我做了一个
全局的hook,但是这个hook只能截获我自己的程序的鼠标消息,其他程序的截获不了,请高手指点一下。
library MyHook;
uses
Windows,
Messages,
HookUnit in 'HookUnit.pas';
exports
InstallHook,
UninstallHook,
IsHooked;
end.
unit HookUnit;
interface
uses
Windows,Messages;
const
MSG_HOOK_MOUSE_EVENT='My_MOUSE_EVENT';
function InstallHook(Hwnd: Cardinal): Boolean;
function UninstallHook: Boolean;
function IsHooked: Boolean;
implementation
var
MouseHookEvent: integer;
HookHandle: HHook;
WindowHandle: HWND;
function MouseHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM):
LRESULT; stdcall;
begin
if nCode = HC_ACTION then
begin
with PMouseHookStruct(lParam)^ do
PostMessage(WindowHandle,MouseHookEvent, wParam, (pt.x and $FFFF) or (pt.y shl 16));
end;
Result:=CallNextHookEx(HookHandle, nCode, wParam, lParam);
end;
function InstallHook(Hwnd: Cardinal): Boolean;
begin
if HookHandle=0 then
begin
if MouseHookEvent=0 then
MouseHookEvent:=RegisterWindowMessage(PChar(MSG_HOOK_MOUSE_EVENT));
WindowHandle := Hwnd;
HookHandle:=SetWindowsHookEx(WH_MOUSE, @MouseHookProc, HInstance , 0);
end;
Result := HookHandle>0;
end;
function UninstallHook: Boolean;
begin
if HookHandle<>0 then
begin
UnhookWindowsHookEx(HookHandle);
HookHandle := 0;
end;
Result:=HookHandle=0;
end;
function IsHooked: Boolean;
begin
Result:=HookHandle<>0;
end;
end.
unit Demo;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, AppEvnts;
const
MSG_HOOK_MOUSE_EVENT='EBOARD_MOUSE_EVENT';
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
winHandle: HWND;
myMsgId: integer;
procedure mymsg(var msg: TMessage);
public
{ Public declarations }
end;
var
Form1: TForm1;
function InstallHook(WindowHandle: HWND) : Boolean;external 'eBoardHook.dll';
function DisableHotKeyHook :Boolean;external 'eBoardHook.dll';
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.mymsg(var msg: TMessage);
begin
if msg.Msg=MyMsgId then
if Msg.WParam <> WM_MouseMove then
memo1.Lines.Add('asdfd');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if not installHook(winHandle) then
showmessage('adsfds');
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
WinHandle := Classes.AllocateHWnd(MyMsg);
mymsgid:=RegisterWindowMessage(PChar(MSG_HOOK_MOUSE_EVENT));
end;