procedure TTest.SetParent(AParent: TWinControl);
begin
inherited;
if csDesigning in ComponentState then Exit; //判断一下,在设计期间就不启用
if Assigned(AParent) then
begin
FParentWndMethod := AParent.WindowProc;
AParent.WindowProc := ParentWndProc;
end else FParentWndMethod := nil;
end;
//我测试了可以改变大小
procedure TTest.ParentWndProc(var Msg: TMessage);
begin
case Msg.Msg of
WM_WINDOWPOSCHANGED:
begin
Form1.Memo1.Lines.Add(
IntToStr(PWINDOWPOS(Msg.LParam).x) + ',' + IntToStr(PWINDOWPOS(Msg.LParam).y));
end;
end;
if Assigned(FParentWndMethod) then FParentWndMethod(Msg);
end;
//注意最后一条语句 //需要调用Parent原来的句柄处理方法
destructor TTest.Destroy;
begin
if Assigned(Parent) then
Parent.WindowProc := FParentWndMethod;
inherited;
end;
procedure TTest.ParentWndProc(var Msg: TMessage);
begin
case Msg.Msg of
WM_MOUSEMOVE:
begin
TForm1(Parent).Memo1.Lines.Add(
IntToStr(LOWORD(Msg.lParam)) + ',' + IntToStr(HIWORD(Msg.lParam))
);
end;
end;
if Assigned(FParentWndMethod) then FParentWndMethod(Msg);
end;
procedure TTest.SetParent(AParent: TWinControl);
begin
inherited;
if Assigned(AParent) then
begin
FParentWndMethod := AParent.WindowProc;
AParent.WindowProc := ParentWndProc;
end else FParentWndMethod := nil;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
with TTest.Create(Self) do
Parent := Self;
end;