DLL 代码:<br>library Mhook;<br><br>uses<br> Main in 'Main.pas',<br><br> windows,<br> messages,<br> Classes,<br> sysutils;<br><br>procedure DLLEntryPoint(dwReason: DWORD);<br>begin<br> case dwReason of<br> DLL_PROCESS_ATTACH: OpenShareData;<br> DLL_PROCESS_DETACH: CloseShareData;<br> end;<br>end;<br>exports<br> EnableMouseHook,<br> DisableMouseHook;<br>begin<br><br> procExit := ExitProc;<br> ExitProc := @HookExit;<br> DLLProc := @DLLEntryPoint;<br> DLLEntryPoint(DLL_PROCESS_ATTACH);<br>end.<br>=======================================================================<br><br>unit Main;<br><br>interface<br>uses<br> windows,<br> messages,<br> Classes,<br> sysutils;<br><br>type<br><br> PServerData = ^ServerData;<br> ServerData = record<br> Wnd: HWND;<br> UniqueMsgID: Cardinal;<br> hHk: HHOOK;<br> end;<br><br>var<br><br> procExit: pointer;<br> SData: PServerData;<br> MapServerHandle: THandle;<br>const<br><br> cMMServerData: PChar = 'ServerMapData';<br><br><br>function EnableMouseHook(h: HWND): Boolean; stdcall;<br>function DisableMouseHook(): Boolean; stdcall;<br><br>procedure HookExit; far;<br>procedure OpenShareData();<br>procedure CloseShareData();<br><br>function CBTHookProc(nCode: integer; wParam, lParam: longint): LRESULT; stdcall;<br><br>implementation<br><br>procedure OpenShareData();<br>var<br> Size: integer;<br>begin<br> Size := Sizeof(SData);<br> MapServerHandle := CreateFileMapping(DWORD(-1), nil, PAGE_READWRITE, 0, Size,<br> cMMServerData);<br> SData := MapViewOfFile(MapServerHandle, FILE_MAP_ALL_ACCESS, 0, 0, Size);<br>end;<br><br><br>procedure CloseShareData();<br>begin<br> UnmapViewOfFile(SData);<br> CloseHandle(MapServerHandle);<br>end;<br><br>function EnableMouseHook(h: HWND): Boolean; stdcall;<br>begin<br> if SData.hHk <> 0 then<br> begin<br> Result := false;<br> exit;<br> end;<br> SData.Wnd := h;<br> SData.UniqueMsgID := RegisterWindowMessage('RWM1');<br><br> SData.hHk := SetWindowsHookEx(WH_CBT, @CBTHookProc, hInstance, 0);<br><br> Result := SData.hHk <> 0;<br>end;<br><br>function DisableMouseHook(): Boolean; stdcall;<br>begin<br> //注销挂钩<br> Result := false;<br> if UnhookWindowsHookEx(SData.hHk) then<br> begin<br> SData.hHk := 0;<br> Result := true;<br> end;<br>end;<br><br>procedure HookExit;<br>begin<br> if SData.hHk <> 0 then<br> DisableMouseHook;<br> ExitProc := procExit;<br>end;<br><br><br>function CBTHookProc(nCode: integer; wParam, lParam: longint): LRESULT; stdcall;<br>begin<br> if nCode <0 then<br> begin<br> Result := CallNextHookEx(SData.hHk, nCode, wParam, lParam)<br> exit;<br> end;<br> if (nCode = HCBT_CREATEWND) then<br> begin<br> //wParam就是窗口的handle<br> //在这里判断是不是你指定的窗口,是的话关闭 <br> <br> end;<br> Result := CallNextHookEx(SData.hHk, nCode, wParam, lParam)<br>end;<br>end.<br>=======================================================================<br>主程序:<br><br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls, ExtCtrls;<br><br>type<br> TForm1 = class(TForm)<br> Label1: TLabel;<br> Button1: TButton;<br> Edit1: TEdit;<br> procedure FormCreate(Sender: TObject);<br> procedure FormDestroy(Sender: TObject);<br> private<br> { Private declarations }<br> procedure WndProc(var Msg: TMessage); override;<br> public<br> { Public declarations }<br> end;<br><br>function EnableMouseHook(h: HWND): Boolean; stdcall;<br>function DisableMouseHook(): Boolean; stdcall;<br><br>var<br> Form1: TForm1;<br> UniqueMsgID: Cardinal;<br><br>implementation<br>function EnableMouseHook; external 'MHook.dll' name 'EnableMouseHook';<br>function DisableMouseHook; external 'MHook.dll' name 'DisableMouseHook';<br><br><br>{$R *.dfm}<br><br>procedure TForm1.WndProc(var Msg: TMessage);<br>var<br> ptMousePos: Tpoint;<br>begin<br> if Msg.msg = UniqueMsgID then<br> begin<br> GetCursorPos(ptMousePos);<br> label1.Caption := 'X:' + inttostr(ptMousePos.X) + ' Y:' + inttostr(ptMousePos.Y);<br> end;<br> inherited WndProc(Msg); ;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>var<br> h: HWND;<br>begin<br> UniqueMsgID := RegisterWindowMessage('RWM1');<br> EnableMouseHook(Handle);<br>end;<br><br>procedure TForm1.FormDestroy(Sender: TObject);<br>begin<br> DisableMouseHook;<br>end;<br><br>end.<br>