关于SetWindowsHookEx的问题(100分)

  • 主题发起人 主题发起人 zhangkan
  • 开始时间 开始时间
Z

zhangkan

Unregistered / Unconfirmed
GUEST, unregistred user!
我想写一个监控鼠标的程序,但写SetWindowsHookEx时遇到问题。以下是源代码:
unit mouseunit;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,lib,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
function MouseProc(Code:Integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;
function HookUpMouse:THandle;
function UnHookMouse:Uint;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
hMNextHook:hHook;
MouseHasHookUp:Boolean;

implementation

{$R *.DFM}

function TForm1.MouseProc(Code:Integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;
begin
Result:=CallNextHookEx(hMNextHook,Code,wParam,lParam);
if Code<0 then
exit;
end;

function TForm1.HookUpMouse:THandle;
begin
Result:=0;
if MouseHasHookUp then
exit;
hMNextHook:=SetWindowsHookEx(WH_MOUSE,@MouseProc,Finstan,0); //此处出错;Variable Required
if hMNextHook<>0 then
begin
MouseHasHookUp:=True;
end
else
begin
exit;
end;
end;

function TForm1.UnHookMouse:Uint;
begin
Result:=0;
if not MouseHasHookUp then
exit;
if hMNextHook<>0 then
begin
if UnHookWindowsHookEx(hMNextHook) then
begin
hMnextHook:=0;
Result:=0;
MouseHasHookUp:=False;
end;
end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
HookUpMouse;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
UnHookMouse;
end;

end.

因为我是写到程序中,而不是写的DLL中。hInstance是DLL中的写法,但我不知道写在程序
中要怎样写。哪位能将我的程序改一下吗?小弟很菜,想写一个计算鼠标滑动长度的程序,
若哪位在源代码给小弟看看,感激不尽,不过不要DLL,要写在程序中的。
 
unit main;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
function HookUpMouse:THandle;
function UnHookMouse:Uint;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
hMNextHook:hHook;
MouseHasHookUp:Boolean;

implementation

{$R *.DFM}

function MouseProc(Code:Integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;
begin
if Code<0 then
begin
Result:=CallNextHookEx(hMNextHook,Code,wParam,lParam);
Exit;
end;
if wParam=WM_LBUTTONDOWN then
Form1.Caption := Form1.Caption + '1';
Result := 0;
end;

function TForm1.HookUpMouse:THandle;
begin
Result:=0;
if MouseHasHookUp then Exit;
hMNextHook:=SetWindowsHookEx(WH_MOUSE,@MouseProc,0,GetCurrentThreadID);
if hMNextHook<>0 then MouseHasHookUp:=True
end;

function TForm1.UnHookMouse:Uint;
begin
Result:=0;
if not MouseHasHookUp then Exit;
if hMNextHook<>0 then
if UnHookWindowsHookEx(hMNextHook) then
begin
hMnextHook:=0;
MouseHasHookUp:=False;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
HookUpMouse;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
UnHookMouse;
end;

end.
 
非常感谢!!
 
后退
顶部