Не сложно Только не WH_GETMESSAGE, а WH_CBT тут нужно:
Создаешь новую ДЛЛ:
| Код | library Hook;
uses Windows, UHook in 'UHook.pas';
{$R *.res}
exports SetHook, FreeHook;
begin end. |
К ней добавляешь юнит и дополняешь его этим кодом:
| Код | unit UHook;
interface
uses Windows, Messages, SysUtils;
const MAP_NAME = 'MonitoringHandle'; HOOK_NAME = 'HookName';
var hMonMap, hHookMap : THandle; pMonHandle : PHandle = nil; pHook : PHandle = nil;
function SetHook(MonitoringHandle: THandle): Boolean; stdcall; function FreeHook:Boolean; stdcall;
implementation
procedure CheckHandle; begin if not Assigned(pMonHandle) then raise Exception.Create('Monitoring handle not exists'); end;
procedure SetHandle(MonitoringHandle: THandle); begin CheckHandle; pMonHandle^ := MonitoringHandle; end;
function GetHandle: THandle; begin CheckHandle; Result := pMonHandle^; end;
function CbtProc(Code: integer; wParam, lParam: LongInt): LongInt; stdcall; var RectPtr: PRect; begin if Code < 0 then begin Result := CallNextHookEx(pHook^, Code, wParam, lParam); Exit; end;
if (Code = HCBT_MOVESIZE) and (wParam = GetHandle) then begin RectPtr := PRect(lParam); MessageBox(GetHandle, PAnsiChar(Format('new width - %d, new height - %d', [RectPtr.Right - RectPtr.Left, RectPtr.Bottom - RectPtr.Top])), 'Window resized', MB_OK); end;
Result := CallNextHookEx(pHook^, Code, wParam, lParam); end;
function SetHook(MonitoringHandle: THandle): Boolean; stdcall; begin Result := False; SetHandle(MonitoringHandle); pHook^ := SetWindowsHookEx(WH_CBT, @CbtProc, hInstance, 0); Result := pHook^ <> 0; end;
function FreeHook:Boolean; stdcall; begin Result := UnhookWindowsHookEx(pHook^); end;
function GetMappedPtr(MapName: PAnsiChar; out hFileMap: THandle; DataSize: Cardinal = sizeof(THandle)): PHandle; var hMap: THandle; begin
hMap := OpenFileMapping(FILE_MAP_ALL_ACCESS, False, MapName);
if hMap = 0 then hMap := CreateFileMapping(INVALID_HANDLE_VALUE, nil, PAGE_READWRITE, 0, DataSize, MapName);
if hMap = 0 then raise Exception.Create('Error on create file mapping!');
Result := MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, DataSize); end;
procedure UnMap(MapPtr: Pointer; hMapFile: THandle); begin if Assigned(MapPtr) then UnMapViewOfFile(MapPtr);
if hMapFile <> 0 then CloseHandle(hMapFile); end;
initialization
pMonHandle := GetMappedPtr(MAP_NAME, hMonMap); pHook := GetMappedPtr(HOOK_NAME, hHookMap);
finalization
UnMap(pMonHandle, hMonMap); UnMap(pHook, hHookMap);
end. |
Компилишь ДЛЛ.
Затем создаешь новый проект(Application) и импортируешь туда эти функции:
| Код | unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); private { Private declarations } public { Public declarations } end;
var Form1: TForm1;
function SetHook(MonitoringHandle: THandle): Boolean; stdcall; external 'Hook.dll' name 'SetHook'; function FreeHook: Boolean; stdcall; external 'Hook.dll' name 'FreeHook';
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject); begin // За место этого Handle поставь нужный тебе хендл SetHook(Handle); end;
procedure TForm1.FormDestroy(Sender: TObject); begin FreeHook; end;
end. | |