在應(yīng)用程序中跟蹤MOUSE的坐標(biāo)
時(shí)間:2022-11-17 10:40:00
導(dǎo)語:在應(yīng)用程序中跟蹤MOUSE的坐標(biāo)一文來源于網(wǎng)友上傳,不代表本站觀點(diǎn),若需要原創(chuàng)文章可咨詢客服老師,歡迎參考。
第一步,建一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。關(guān)鍵在于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(創(chuàng)建公用數(shù)據(jù)的Buffer不成?;
pMem:=MapViewOfFile(hObject,FILE_MAP_WRITE,0,0,SizeOf(mydata));
//1orSizeOf(DataBuf)????
//創(chuàng)建SizeOf(DataBuf)的數(shù)據(jù)區(qū)
ifnotAssigned(pMem)then
begin
UnMapMem;
RaiseException.Create(創(chuàng)建公用數(shù)據(jù)的映射關(guān)系不成功!);
end;
end;
ProcedureDLLMain(dwReason:Dword);far;
begin
CasedwReasonof
DLL_PROCESS_ATTACH:
begin
pMem:=nil;
hObject:=0;
MapMem;//以下的公有數(shù)據(jù),如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//這里引出了公用數(shù)據(jù)區(qū)的指針,你可以在你的應(yīng)用程序中自由操作它。但建議去掉此接口。
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;//發(fā)送消息
end;functionOpenGetKeyHook(sender:HWND;MessageID:WORD):BOOL;export;
begin
Result:=False;
ifNextHook$#@60;$#@62;0thenExit;//已經(jīng)安裝了本鉤子
//DataBuf(pMem^)[1]:=Sender;//填數(shù)據(jù)區(qū)
//DataBuf(pMem^)[2]:=MessageID;//填數(shù)據(jù)區(qū)
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);//把鉤子鏈鏈接到下一個(gè)鉤子處理上.
NextHook:=0;
end;
Result:=NextHook=0;
end;end.第三步,測試DLL,建一PROJECT。關(guān)鍵在于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(結(jié)束記錄...);
show.caption:=結(jié)束記錄...;
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.