在应用程序中跟踪MOUSE的坐标
时间:2022-11-17 10:40:00
导语:在应用程序中跟踪MOUSE的坐标一文来源于网友上传,不代表本站观点,若需要原创文章可咨询客服老师,欢迎参考。
第一步,建一DLL,DELPHI中NEW-》DLLSAVEASGETKEYlibrarygetKey;uses
SysUtils,
Windows,
HookMaininhookmain.pas;exports
OpenGetKeyHook,
CloseGetKeyHook,
GetPublicP;begin
NextHook:=0;
procSaveExit:=ExitProc;
DLLproc:=@DLLMain;
ExitProc:=@HookExit;
DLLMain(DLL_PROCESS_ATTACH);
end.第二步,建一UNIT,HOOKMAIN。关键在于CreateFileMapping和消息WM_NCmouseMove,WM_MOUSEMOVE:unitHookMain;interface
usesWindows,Messages,Dialogs,SysUtils;//typeDataBuf=Array[1..2]ofDWORD;
typemydata=record
data1:array[1..2]ofDWORD;
data2:TMOUSEHOOKSTRUCT;
end;
varhObject:Thandle;
pMem:Pointer;
NextHook:Hhook;
procSaveExit:Pointer;functionHookHandler(iCode:Integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;export;
functionOpenGetKeyHook(sender:HWND;MessageID:WORD):BOOL;export;
functionCloseGetKeyHook:BOOL;export;
functionGetPublicP:Pointer;stdcall;export;
ProcedureDLLMain(dwReason:Dword);far;
procedureHookExit;far;implementationProcedureUnMapMem;
begin
ifAssigned(pMem)then
begin
UnMapViewOfFile(pMem);
pMem:=Nil
end;
end;ProcedureMapMem;
begin
hObject:=CreateFileMapping($FFFFFFFF,Nil,Page_ReadWrite,0,$FFFF,pChar(_IOBuffer));
ifhObject=0thenRaiseException.Create(创建公用数据的Buffer不成?;
pMem:=MapViewOfFile(hObject,FILE_MAP_WRITE,0,0,SizeOf(mydata));
//1orSizeOf(DataBuf)????
//创建SizeOf(DataBuf)的数据区
ifnotAssigned(pMem)then
begin
UnMapMem;
RaiseException.Create(创建公用数据的映射关系不成功!);
end;
end;
ProcedureDLLMain(dwReason:Dword);far;
begin
CasedwReasonof
DLL_PROCESS_ATTACH:
begin
pMem:=nil;
hObject:=0;
MapMem;//以下的公有数据,如tHWND,tMessageID将直接使用本Buf.
End;
DLL_PROCESS_DETACH:UnMapMem;
DLL_THREAD_ATTACH,
DLL_THREAD_DETACH:;//缺省
end;
end;procedureHookExit;far;
begin
CloseGetKeyHook;
ExitProc:=procSaveExit;
end;functionGetPublicP:Pointer;export;
begin//这里引出了公用数据区的指针,你可以在你的应用程序中自由操作它。但建议去掉此接口。
Result:=pMem;
end;functionHookHandler(iCode:Integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;export;
begin
Result:=0;
IfiCode$#@60;0
ThenResult:=CallNextHookEx(NextHook,iCode,wParam,lParam);
//Thisisprobablyclosertowhatyouwouldwanttodo...
Casewparamof
WM_LBUTTONDOWN:
begin
end;
WM_LBUTTONUP:
begin
end;
WM_LBUTTONDBLCLK:
begin
end;
WM_RBUTTONDOWN:
begin
messagebeep(1);
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
mydata(pmem^).data2:=pMOUSEHOOKSTRUCT(lparam)^;
//messagebeep(1);
//SendMessage(DataBuf(pMem^)[1],DataBuf(pMem^)[2],wParam,lParam);
SendMessage(mydata(pMem^).data1[1],mydata(pMem^).data1[2],wParam,integer(@(mydata(pmem^).data2)));
end;
end;//发送消息
end;functionOpenGetKeyHook(sender:HWND;MessageID:WORD):BOOL;export;
begin
Result:=False;
ifNextHook$#@60;$#@62;0thenExit;//已经安装了本钩子
//DataBuf(pMem^)[1]:=Sender;//填数据区
//DataBuf(pMem^)[2]:=MessageID;//填数据区
mydata(pmem^).data1[1]:=sender;
mydata(pmem^).data1[2]:=messageid;NextHook:=SetWindowsHookEx(WH_mouse,HookHandler,Hinstance,0);
Result:=NextHook$#@60;$#@62;0;
end;functionCloseGetKeyHook:BOOL;export;
begin
ifNextHook$#@60;$#@62;0then
begin
UnhookWindowshookEx(NextHook);//把钩子链链接到下一个钩子处理上.
NextHook:=0;
end;
Result:=NextHook=0;
end;end.第三步,测试DLL,建一PROJECT。关键在于overrideWndProcunitUnit1;interfaceuses
Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,Dialogs,
StdCtrls,ExtCtrls;type
TForm1=class(Tform)
uncapture:Tbutton;
capture:Tbutton;
Exit:Tbutton;
Panel1:Tpanel;
show:Tlabel;Label1:Tlabel;
counter:Tlabel;
procedureExitClick(Sender:Tobject);
procedureuncaptureClick(Sender:Tobject);
procedurecaptureClick(Sender:Tobject);
private
{Privatedeclarations}
public
{Publicdeclarations}
procedureWndProc(varMessage:Tmessage);override;
end;var
Form1:TForm1;
varnum:integer;
constMessageID=WM_User+100;
implementation{$R*.DFM}
functionOpenGetKeyHook(sender:HWND;MessageID:WORD):BOOL;externalGetKey.DLL;
functionCloseGetKeyHook:BOOL;externalGetKey.DLL;procedureTForm1.ExitClick(Sender:Tobject);
begin
close;
end;procedureTForm1.uncaptureClick(Sender:Tobject);
begin
ifCloseGetKeyHookthen//ShowMessage(结束记录...);
show.caption:=结束记录...;
end;procedureTForm1.captureClick(Sender:Tobject);
begin
//ifOpenGetKeyHook(self.Handle,MessageID)thenShowMessage(开始记录...);ifOpenGetKeyHook(Form1.Handle,MessageID)then
//ShowMessage(开始记录...);
show.caption:=开始记录...;
num:=0;
end;procedureTForm1.WndProc(varMessage:Tmessage);
varx,y:integer;
begin
ifMessage.Msg=MessageIDthen
begin
//Panel1.Caption:=IntToStr(Num);
x:=PMouseHookStruct(message.lparam)^.pt.x;
y:=PMouseHookStruct(message.lparam)^.pt.y;panel1.caption:=x=+inttostr(x)+y=+inttostr(y);
inc(Num);
counter.Caption:=IntToStr(Num);
end
elseInherited;
end;end.