hook鼠标
阅读原文时间:2023年07月11日阅读:3

library dllMouse;
uses
SysUtils,
Classes,
UnitHookDLL in 'UnitHookDLL.pas',
UnitHookConst in 'UnitHookConst.pas';

{$R *.res}
{ function StartHook(sender : HWND;MessageID : WORD) : BOOL; stdcall;
function StopHook: BOOL; stdcall;
procedure GetRbutton; stdcall;}

exports
StartHook, StopHook,GetRbutton;

begin
end.

{
截获鼠标消息例子.
截获所有进程的鼠标消息,需要用到系统钩子,即要用到dll才能够截取,因此挂
钩函数封装在dll动态链接库中.由于需要跨进程共享数据,所以在dll使用内存映像
文件来共享数据.
}
unit UnitHookDLL;
// download by http://www.codefans.net
interface

uses Windows, Messages, Dialogs, SysUtils,UnitHookConst;

var
hMappingFile : THandle;
pShMem : PShareMem;
FirstProcess : boolean;
NextHook: HHook;

function StartHook(sender : HWND;MessageID : WORD) : BOOL; stdcall;
function StopHook: BOOL; stdcall;
procedure GetRbutton; stdcall;

implementation

procedure GetRbutton; stdcall;
begin
pShMem^.IfRbutton:=true;
end;

function HookHandler(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall; export;
begin
Result := ;
If iCode < Then Result := CallNextHookEx(NextHook, iCode, wParam, lParam);

case wparam of
WM_LBUTTONDOWN:
begin
end;
WM_LBUTTONUP:
begin
end;
WM_LBUTTONDBLCLK:
begin
end;
WM_RBUTTONDOWN:
begin
if pShMem^.IfRbutton then
begin
pShMem^.IfRbutton := false;
pShMem^.data2:=pMOUSEHOOKSTRUCT(lparam)^;
getwindowtext(pShMem^.data2.hwnd,pShMem^.buffer,);
SendMessage(pShMem^.data1[],pShMem^.data1[]+,wParam,integer(@(pShMem^.data2)) );
// 窗口 消息 坐标
end;
end;
WM_RBUTTONUP:
begin
end;
WM_RBUTTONDBLCLK:
begin
end;
WM_MBUTTONDOWN:
begin
end;
WM_MBUTTONUP:
begin
end;
WM_MBUTTONDBLCLK:
begin
end;
WM_NCMouseMove, WM_MOUSEMOVE:
begin
pShMem^.data2:=pMOUSEHOOKSTRUCT(lparam)^;
getwindowtext(pShMem^.data2.hwnd,pShMem^.buffer,);
SendMessage(pShMem^.data1[],pShMem^.data1[],wParam,integer(@(pShMem^.data2)) );
// 窗口 消息 坐标
end;
end;
end;

function StartHook(sender : HWND;MessageID : WORD) : BOOL;
function GetModuleHandleFromInstance: THandle;
var
s: array[..] of char;
begin
GetModuleFileName(hInstance, s, sizeof(s)-);
Result := GetModuleHandle(s);
end;
begin
Result := False;
if NextHook <> then Exit;
pShMem^.data1[]:=sender;
pShMem^.data1[]:=messageid;
NextHook :=
SetWindowsHookEx(WH_mouse, HookHandler, HInstance, ); //全局
//SetWindowsHookEx(WH_mouse, HookHandler, GetModuleHandleFromInstance, GetCurrentThreadID); //实例
Result := NextHook <> ;
end;

function StopHook: BOOL;
begin
if NextHook <> then
begin
UnhookWindowshookEx(NextHook);
NextHook := ;
//SendMessage(HWND_BROADCAST,WM_SETTINGCHANGE,0,0);
end;
Result := NextHook = ;
end;

initialization
hMappingFile := OpenFileMapping(FILE_MAP_WRITE,False,MappingFileName);
if hMappingFile= then
begin
hMappingFile := CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,,SizeOf(TShareMem),MappingFileName);
FirstProcess:=true;
end
else FirstProcess:=false;
if hMappingFile= then Exception.Create('不能建立共享内存!');

    pShMem :=  MapViewOfFile(hMappingFile,FILE\_MAP\_WRITE or FILE\_MAP\_READ,,,);  
    if pShMem = nil then  
    begin  
       CloseHandle(hMappingFile);  
       Exception.Create('不能映射共享内存!');  
    end;  
    if FirstProcess then  
    begin  
       pShmem^.IfRbutton := false;  
    end;  
    NextHook:=;  

finalization
UnMapViewOfFile(pShMem);
CloseHandle(hMappingFile);

end.

unit Unitmain;

interface

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

type
TForm1 = class(TForm)
edt1: TEdit;
edt2: TEdit;
capture: TButton;
edt3: TEdit;
btn2: TButton;
lbl1: TLabel;
edt4: TEdit;
edt5: TEdit;
edt6: TEdit;
pnl1: TPanel;
procedure captureClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
procedure WndProc(var Messages: TMessage); override;
end;

var
Form1: TForm1;
hMappingFile : THandle;
pShMem : PShareMem;
const MessageID = WM_User + ;

implementation

{$R *.DFM}
//dllMouse.dll DllMouse.DLL
function StartHook(sender : HWND;MessageID : WORD) : BOOL;stdcall; external 'dllMouse.dll';
function StopHook: BOOL;stdcall; external 'dllMouse.dll';
procedure GetRbutton; stdcall; external 'dllMouse.dll';

procedure TForm1.captureClick(Sender: TObject);
begin
if capture.caption='开始' then
begin
if StartHook(Form1.Handle,MessageID) then
capture.caption:='停止';
end
else begin
if StopHook then
capture.caption:='开始';
end;
end;

procedure TForm1.btn2Click(Sender: TObject);
begin
if capture.caption<>'开始' then
begin
edt4.text:='';
edt5.text:='';
edt6.text:='';
GetRbutton;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
pShMem := nil;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if capture.caption='开始' then
begin
end
else begin
if StopHook then
capture.caption:='开始';
end;
end;

procedure TForm1.WndProc(var Messages: TMessage);
var
x,y:integer;
s:array[..]of char;
begin
if pShMem = nil then
begin
hMappingFile := OpenFileMapping(FILE_MAP_WRITE,False,MappingFileName);
if hMappingFile= then Exception.Create('不能建立共享内存!');
pShMem := MapViewOfFile(hMappingFile,FILE_MAP_WRITE or FILE_MAP_READ,,,);
if pShMem = nil then
begin
CloseHandle(hMappingFile);
Exception.Create('不能映射共享内存!');
end;
end;
if pShMem = nil then exit;
if Messages.Msg = MessageID then
begin
x:=pShMem^.data2.pt.x;
y:=pShMem^.data2.pt.y;
edt3.text:='HWND:'+inttostr(pShMem^.data2.hwnd);
pnl1.caption:='x='+inttostr(x)+' y='+inttostr(y);
edt2.text:='WindowsText:'+string(pShMem^.buffer);
getClassName(pShMem^.data2.hwnd,s,);
edt1.text:='ClassName:"'+string(s)+'"';
end
else if Messages.Msg = MessageID+ then
begin
edt4.text:=inttostr(pShMem^.data2.hwnd);
edt5.text:='WindowsText:'+string(pShMem^.buffer);
getClassName(pShMem^.data2.hwnd,s,);
edt6.text:='ClassName:"'+string(s)+'"';
end
else Inherited;
end;

end.

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章