关于鼠标钩子的问题(100分)

  • 主题发起人 主题发起人 njhuadong
  • 开始时间 开始时间
N

njhuadong

Unregistered / Unconfirmed
GUEST, unregistred user!
在网上下载了一个钩子程序,可以用,但是一旦运行了以后,就无法相应正常的鼠标动作了,比如,真正的鼠标点击事件就无法执行了,而只能知道有个点击,不知如何解决?在线等。付上源程序:
钩子动态库:
library MouseHook;

uses
Windows, Messages;

const
MappingFileName = '57D6A971_MouseHookDLL_442C0DB1';
MSGMOUSEMOVE: PChar = 'MSGMOUSEMOVE57D6A971-049B-45AF-A8CD-37E0B706E036';
MSGMOUSECLICK: PChar = 'MSGMOUSECLICK442C0DB1-3198-4C2B-A718-143F6E2D1760';

type
TMappingMem = record
Handle: DWORD;
MsgID: DWORD;
MouseStruct: TMOUSEHOOKSTRUCT;
end;
PMappingMem = ^TMappingMem;

var
MSG_MOUSEMOVE: UINT;
MSG_MOUSECLICK: UINT;
hMappingFile: THandle;
pMapMem: PMappingMem;
mhook: HHook;
fblocked: BOOL = True;
//CriticalSection: TRTLCriticalSection;

function MouseHookProc(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall
var hWindow : HWND;
begin
hWindow := GetActiveWindow;
SendMessage(hWindow,wParam,0,lParam);
if fblocked then
Result := 1
else
Result := 0;

if iCode < 0 then
CallNextHookEx(mhook, iCode, wParam, lParam);

case wParam of
WM_MOUSEMOVE, WM_NCMouseMove:
begin
pMapMem^.MsgID := MSG_MOUSEMOVE;
pMapMem^.MouseStruct := pMOUSEHOOKSTRUCT(lparam)^;
SendMessage(pMapMem^.Handle, pMapMem^.MsgID, 0, 0);
end;
WM_LBUTTONDOWN, WM_NCLBUTTONDOWN:
begin
pMapMem^.MsgID := MSG_MOUSECLICK;
pMapMem^.MouseStruct := pMOUSEHOOKSTRUCT(lparam)^;
SendMessage(pMapMem^.Handle, pMapMem^.MsgID, 0, 0);
end;
WM_LBUTTONUP:
begin
end;
WM_LBUTTONDBLCLK:
begin
end;
WM_RBUTTONDOWN:
begin
end;
WM_RBUTTONUP:
begin
end;
WM_RBUTTONDBLCLK:
begin
end;
WM_MBUTTONDOWN:
begin
end;
WM_MBUTTONUP:
begin
end;
WM_MBUTTONDBLCLK:
begin
end;
end;
end;

function EnableMouseHook(hWindow: HWND; Blocked: BOOL): BOOL; stdcall;
begin
Result := False;
if mhook <> 0 then
Exit;
pMapMem^.Handle := hWindow;
fblocked := Blocked;
mhook := SetWindowsHookEx(WH_MOUSE, MouseHookProc, HInstance, 0);
Result := mhook <> 0;
end;

function DisableMouseHook: BOOL; stdcall;
begin
if mhook <> 0 then
begin
UnhookWindowshookEx(mhook);
mhook := 0;
end;
Result := mhook = 0;
end;

procedure DllMain(dwReason: DWORD);
begin
case dwReason of
DLL_PROCESS_ATTACH:
begin
//InitializeCriticalSection(CriticalSection);
hMappingFile := OpenFileMapping(FILE_MAP_WRITE, False, MappingFileName);
if hMappingFile = 0 then
begin
hMappingFile := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE,
0, SizeOf(TMappingMem), MappingFileName);
end;
if hMappingFile = 0 then
MessageBox(0, 'cannot create share memory!', 'Error', MB_OK or MB_ICONERROR);

pMapMem := MapViewOfFile(hMappingFile, FILE_MAP_WRITE or FILE_MAP_READ,
0, 0, 0);
if pMapMem = nil then
begin
CloseHandle(hMappingFile);
MessageBox(0, 'cannot map share memory!', 'Error', MB_OK or MB_ICONERROR);
end;
mhook := 0;
MSG_MOUSEMOVE := RegisterWindowMessage(MSGMOUSEMOVE);
MSG_MOUSECLICK := RegisterWindowMessage(MSGMOUSECLICK);
end;
DLL_PROCESS_DETACH:
begin
UnMapViewOfFile(pMapMem);
CloseHandle(hMappingFile);
if mhook <> 0 then
DisableMouseHook;
//DeleteCriticalSection(CriticalSection);
end;
DLL_THREAD_ATTACH:
begin
end;
DLL_THREAD_DETACH:
begin
end;
end;
end;

exports
EnableMouseHook,
DisableMouseHook;

begin
DisableThreadLibraryCalls(HInstance);
DLLProc := @DLLMain;
DLLMain(DLL_PROCESS_ATTACH);
end.


调用程序:
unit Main;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, StdCtrls,
MouseHook, KeyboardHook;

type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
Label1: TLabel;
GroupBox2: TGroupBox;
btStartMouse: TButton;
btStartKeyboard: TButton;
Label2: TLabel;
Label3: TLabel;
procedure btStartMouseClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure btStartKeyboardClick(Sender: TObject);
private
mhook: TMouseHook;
khook: TKeyboardHook;
procedure HookMouseMove(const h: HWND; const X, Y: Integer);
procedure HookMouseClick(const h: HWND; const X, Y: Integer);
procedure HookKeyboardDown(const KeyCode: Integer);
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btStartMouseClick(Sender: TObject);
begin
if btStartMouse.Caption = 'Start' then
begin
if mhook.Start then
btStartMouse.Caption := 'Stop';
end
else
begin
mhook.Stop;
btStartMouse.Caption := 'Start';
end;
end;

procedure TForm1.btStartKeyboardClick(Sender: TObject);
begin
if btStartKeyboard.Caption = 'Start' then
begin
if khook.Start then
btStartKeyboard.Caption := 'Stop';
end
else
begin
khook.Stop;
btStartKeyboard.Caption := 'Start';
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
// 创建mouse hook实例
mhook := TMouseHook.Create;
mhook.DLLName := 'mousehook.dll';
mhook.Blocked := False;
mhook.OnMouseMove := HookMouseMove;
mhook.OnMouseClick := HookMouseClick;

// 创建keyboard hook实例
khook := TKeyboardHook.Create;
khook.DLLName := 'keyboardhook.dll';
khook.OnKeyDown := HookKeyboardDown;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
// 停止hook, 释放对象
if khook.Active then
khook.Stop;
if Assigned(khook) then
khook.Free;

if mhook.Active then
mhook.Stop;
if Assigned(mhook) then
mhook.Free;
end;

procedure TForm1.HookKeyboardDown(const KeyCode: Integer);
begin
// 键盘消息测试
if KeyCode = VK_ESCAPE then
begin
MessageBox(Handle, 'We get a keyboard message: VK_ESCAPE', 'Info', MB_OK);
if khook.Active then
khook.Stop;
end;
end;

procedure TForm1.HookMouseMove(const h: HWND; const X, Y: Integer);
begin
// 鼠标消息测试 OnMove
Label1.Caption := Format('Position: WND: %d - X: %d - Y: %d', [h, X, Y])
end;

procedure TForm1.HookMouseClick(const h: HWND; const X, Y: Integer);
var hWindow : HWnd;
begin
// 鼠标消息测试 OnClick

Label2.Caption := Format('Click at: WND: %d - X: %d - Y: %d', [h, x, y]);
end;

end.
 
退出钩子
 
我就是要监考鼠标的动作,退出后怎么弄
 
我也下了这个例子的,确实有点问题,换一个吧
 
关键是应该怎么解决呢
 
退出钩子
 
难道?......[?]
 
我想应该是少了CallNextHookEx(LogHook, iCode, wParam, lParam);这样一条语句
 
后退
顶部