type
THookedForm =
class(TCustomForm)
procedure HookedDoCreate;
end;
THookedFrame =
class(TCustomFrame)
constructor Create(AOwner: TComponent);
override;
end;
PPatchEvent = ^TPatchEvent;
/// asm opcode hack to patch an existing routine
TPatchEvent =
packed record
Jump: byte;
Offset: integer;
end;
var
PatchForm, OriginalForm: TPatchEvent;
PatchPositionForm: PPatchEvent =
nil;
PatchFrame, OriginalFrame: TPatchEvent;
PatchPositionFrame: PPatchEvent =
nil;
procedure PatchCreate;
var ov: cardinal;
begin
// hook TForm:
PatchPositionForm := PPatchEvent(@THookedForm.DoCreate);
OriginalForm := PatchPositionForm^;
PatchForm.Jump := $E9;
// Jmp opcode
PatchForm.Offset := PtrInt(@THookedForm.HookedDoCreate)-PtrInt(PatchPositionForm)-5;
if not VirtualProtect(PatchPositionForm, 5, PAGE_EXECUTE_READWRITE, @ov)
then
RaiseLastOSError;
PatchPositionForm^ := PatchForm;
// enable Hook
// hook TFrame:
PatchPositionFrame := PPatchEvent(@TCustomFrame.Create);
OriginalFrame := PatchPositionFrame^;
PatchFrame.Jump := $E9;
// Jmp opcode
PatchFrame.Offset := PtrInt(@THookedFrame.Create)-PtrInt(PatchPositionFrame)-5;
if not VirtualProtect(PatchPositionFrame, 5, PAGE_EXECUTE_READWRITE, @ov)
then
RaiseLastOSError;
PatchPositionFrame^ := PatchFrame;
// enable Hook
end;
{ THookedForm }
procedure THookedForm.HookedDoCreate;
var i: integer;
begin
// enumerate all labels, then set Transparent := true
for i := 0
to Components.Count-1
do
if Components[i]
is TLabel
then
TLabel(Components[i]).Transparent := true;
DoCreate;
// call initial code
end;
{ THookedFrame }
constructor THookedFrame.Create(AOwner: TComponent);
var i: integer;
begin
// enumerate all labels, then set Transparent := true
for i := 0
to Components.Count-1
do
if Components[i]
is TLabel
then
TLabel(Components[i]).Transparent := true;
inherited Create(AOwner);
// call normal constructor
end;
....
initialization
PatchCreate;