ÄãµÄλÖãºÈí¼þÓë¹âÅÌ>
ÔÚÓ¦ÓóÌÐòÖиú×ÙMOUSEµÄ×ø±ê
2000-03-19 00:00:00¡¤ -¡¤¿ªÌì±ÙµØ

ªÊ²Ã´Ñ¡Õâ¸ö»°Ì⣿ÒòΪ¸ú×ÙMOUSE×ø±êºÜ³£¼û£¬ÈÝÒ×ÓÖÌØ±ð²»ÈÝÒ×£¬·Ç³£ËµÃ÷WINDOWS95ϱà³ÌµÄÌØµã¡£

¡¡¡¡{ Èç¹ûÄú¿´²»¶®£¬ÇëÂòDELPHI 2 UNLEASHED RMB133£¬µ±È»ËûûÕâ¸ö³ÌÐò£¬µ«ÓÐһЩдWIN HOOK±ØÐë¾ß±¸µÄ֪ʶ¡£±¾³ÌÐòµÃµ½AIMING´óϺµÄ´óÁ¦Ð­Öú£¬ÊÂʵÉÏÎҵijÌÐòÊÇÔÚËûµÄ»ù´¡ÉϸÄдµÄ£¬ËûµÄÊÇ´ÓDELPHI HELPÖиÄд³öÀ´µÄ¡£µ÷ÊÔ³ÌÐò»¨ÁËÎÒÁ½¸öÀñ°Ý£¬×îºÃÄãÄÜ»¨Í¬ÑùµÄʱ¼ä£¬ÄÇôÄã¾Í»áÊÕ»ñºÜ¶à£¡ }

¡¡¡¡µÚÒ»²½£¬½¨Ò»DLL£¬DELPHIÖÐNEW-¡·DLL SAVE AS GETKEY

library getKey;

uses
SysUtils,
Windows,
HookMain in hookmain.pas;

exports
OpenGetKeyHook,
CloseGetKeyHook,
GetPublicP;

begin
NextHook := 0;
procSaveExit := ExitProc;
DLLproc := @DLLMain;
ExitProc := @HookExit;
DLLMain(DLL_PROCESS_ATTACH);
end.

µÚ¶þ²½£¬½¨Ò»UNIT £¬HOOK MAIN¡£¹Ø¼üÔÚÓÚCreateFileMapping ºÍ ÏûÏ¢ WM_NCMouseMove, WM_MOUSEMOVE:

unit HookMain;

interface
uses Windows, Messages, Dialogs, SysUtils;

//type DataBuf = Array [1..2] of DWORD;
type mydata=record
data1:array [1..2] of DWORD;
data2:TMOUSEHOOKSTRUCT;
end;
var hObject : Thandle;
pMem : Pointer;
NextHook: Hhook;
procSaveExit: Pointer;

function HookHandler(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall; export;
function OpenGetKeyHook(sender : HWND;MessageID : WORD) : BOOL; export;
function CloseGetKeyHook: BOOL; export;
function GetPublicP : Pointer;stdcall; export;
Procedure DLLMain(dwReason:Dword); far;
procedure HookExit; far;

implementation

Procedure UnMapMem;
begin
if Assigned(pMem) then
begin
UnMapViewOfFile(pMem);
pMem := Nil
end;
end;

Procedure MapMem;
begin
hObject := CreateFileMapping($FFFFFFFF,Nil,Page_ReadWrite,0,$FFFF,pChar(_IOBuffer));
if hObject = 0 then Raise Exception.Create(´´½¨¹«ÓÃÊý¾ÝµÄBuffer²»³É¹ ;
pMem := MapViewOfFile(hObject,FILE_MAP_WRITE,0,0,SizeOf(mydata));
// 1 or SizeOf(DataBuf) ????
// ´´½¨SizeOf(DataBuf)µÄÊý¾ÝÇø
if not Assigned(pMem) then
begin
UnMapMem;
Raise Exception.Create(´´½¨¹«ÓÃÊý¾ÝµÄÓ³Éä¹ØÏµ²»³É¹¦!);
end;
end;
Procedure DLLMain(dwReason:Dword); far;
begin
Case dwReason of
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;

procedure HookExit; far;
begin
CloseGetKeyHook;
ExitProc := procSaveExit;
end;

function GetPublicP : Pointer;export;
begin //ÕâÀïÒý³öÁ˹«ÓÃÊý¾ÝÇøµÄÖ¸Õ룬Äã¿ÉÒÔÔÚÄãµÄÓ¦ÓóÌÐòÖÐ×ÔÓɲÙ×÷Ëü¡£µ«½¨ÒéÈ¥µô´Ë½Ó¿Ú¡£
Result := pMem;
end;

function HookHandler(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall; export;
begin
Result := 0;
If iCode $#@60; 0
Then Result := CallNextHookEx(NextHook, iCode, wParam, lParam);


// This is probably closer to what you would want to do...
Case wparam of
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;

function OpenGetKeyHook(sender : HWND;MessageID : WORD) : BOOL; export;
begin
Result := False;
if NextHook $#@60;$#@62; 0 then Exit; //ÒѾ­°²×°Á˱¾¹³×Ó
// 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;

function CloseGetKeyHook: BOOL; export;
begin
if NextHook $#@60;$#@62; 0 then
begin
UnhookWindowshookEx(NextHook); //°Ñ¹³×ÓÁ´Á´½Óµ½ÏÂÒ»¸ö¹³×Ó´¦ÀíÉÏ.
NextHook := 0;
end;
Result := NextHook = 0;
end;

end.



µÚÈý²½£¬²âÊÔDLL£¬½¨Ò»PROJECT¡£¹Ø¼üÔÚÓÚoverride WndProc

unit Unit1;

interface

uses
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;
procedure ExitClick(Sender: Tobject);
procedure uncaptureClick(Sender: Tobject);
procedure captureClick(Sender: Tobject);
private
{ Private declarations }
public
{ Public declarations }
procedure WndProc(var Message: Tmessage); override;
end;

var
Form1: TForm1;
var num : integer;
const MessageID = WM_User + 100;
implementation

{$R *.DFM}
function OpenGetKeyHook(sender : HWND;MessageID : WORD) : BOOL; external GetKey.DLL;
function CloseGetKeyHook: BOOL; external GetKey.DLL;

procedure TForm1.ExitClick(Sender: Tobject);
begin
close;
end;

procedure TForm1.uncaptureClick(Sender: Tobject);
begin
if CloseGetKeyHook then //ShowMessage(½áÊø¼Ç¼...);
show.caption:=½áÊø¼Ç¼...;
end;

procedure TForm1.captureClick(Sender: Tobject);
begin
// if OpenGetKeyHook(self.Handle,MessageID) then ShowMessage(¿ªÊ¼¼Ç¼...);

if OpenGetKeyHook(Form1.Handle,MessageID) then
//ShowMessage(¿ªÊ¼¼Ç¼...);
show.caption:=¿ªÊ¼¼Ç¼...;
num := 0;


end;

procedure TForm1.WndProc(var Message: Tmessage);
var x,y:integer;
begin
if Message.Msg = MessageID then
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
else Inherited;
end;

end.


¡¾·¢±íÆÀÂÛ¡¿¡¾¹Ø±Õ´°¿Ú¡¿