不好意思,找了一下没找到Mouse Hook方面的,只有Keyboard hook方面的多......
所以干脆自己写了一个 ^_^
--------------------MouseHook.DPR------------
Lirbrary MouseHook;
uses main in 'main.pas';
exports
EnableMouseHook, DisableMouseHook;
begin
HookDC:=0;
end;
----------------main.pas----------------------
unit main;
interface
uses windows, message;
type
TmyData = record
H: Hwnd;
end;
var
HookDC: HHook;
procSaveExit;
Pointer;
MapFile: THandle;
ShareData: ^TMyData;
const
WM_MyMousMsg = WM_User + 100;
function MouseProc(nCode:integer;
wParam:wParam;
lParam:lParam):lResult;
stdcall;
export;
fcuntion EnableMouseHook(hd: Hwnd);
export;
function DisableMouseHook:bool;
export;
procedure HotMouseHookExit;
far;
proceudre CreateMapFile;
procedure CloseMapFIle;
implementation
procedure CloseMapFile;
begin
UnMapViewOfFile(ShareData);
CloseHandle(MapFile);
end;
procedure CreateMapFile;
begin
MapFile := CreateFileMapping($FFFFFF, nil, Page_ReadWrite,0,sizeof(sharedata),'sharedatafile');
sharedata := MapViewOfFile(mapfile,file_map_write,0,0,sizeof(sharedata));
end;
procedure EnableMouseHook(hd: Hwnd): bool;
export;
begin
result := false;
if HookDC <> 0 then
exit;
ShareData.H := hd;
HookDC := SetWindwosHookEx(WH_Mouse, mouseproc, HInstance, 0);
Result := HookDC <> 0;
end;
function MouseProc(nCode:integer;
wParam:wParam;
lParam:lParam):lResult;
stdcall;
export;
var
mhs: pMouseHookStruct;
begin
result := CallNextHookEx(HookDC, nCode, wParam, lParam);
if wParam = WM_MouseMove then
begin
mhs:=pMouseHookStruct(lParam);
if mhs.pt.x < 3 then
begin
result := 1;
postMessage(ShareData.H, wm_myMouseMsg, mhs.hwnd,0);
end;
end;
end;
function DisableMouseHook:bool;
export;
begin
UnHookWindowsHookEx(HookDC);
HookDC := 0;
Result := true;
end;
procedure HotMouseHookExit;
begin
if HookDC <> 0 then
DisableMouseHook;
exitproc := procsaveexit;
end;
initalization
CreateMapFile;
finalization
CloseMapFile;
end;
-----------新建一个project1.dpr---------------
......
----------unit1.pas----------------------
unit unit1;
interface ......
type
TMainForm = class(TForm)
Button1: TButton;
button2: tButton;
procedure Button1Click(Sender: TOBject);
procedure Button2click(sender: TObject);
private
proceudre WndProc(var message: TMessage);
override;
end;
......
const
MW_myMouseMsg = WM_User + 100;
function EnableMouseHook(hd: Hwnd): bool;
external 'MouseHook.DLL';
function DisableMouseHook:bool;
external 'MouseHook.dll';
......
procedure TMainFOrm.WndProc(var message: TMesage);
begin
inherited WndProc(Message);
if message.msg = wm_mymousemsg then
begin
application.bringtofront;
...... //你需要做的事情
end;
end;
procedure TMainFOrm.button1.click(Sender: TObject);
begin
if enablemousehook(handle) <> true then
showmessage('鼠标钩子已经装载或装载错误!');
end;
proceudre TMainForm.button2click(......);
beign
if disablemousehook <> true then
showmessage('鼠标钩子解除发生错误!');
end;
呼~~~~~~~~~~~~~~总算打完了。在网吧上网真不方便,输入法都不顺手的说
。
我只测试了一下,在D3 + win2000pro下成功。
打得太快,可能其中有错误或遗漏 ^_^ ,你先试一试吧!