我正在研究一个新的组件,我想处理所有父消息。

Type
 TMyComponent= class(TComponent)
//Bla bla
/..
//.
published
property Parent: TWinControl read FParent write SetParent;

end;


我想访问父级WndProc(处理所有父级消息)。有什么办法可以从我的TMyComponent处理Parent WndProc?

最佳答案

像这样:

type
  TMyComponent = class(TComponent)
  private
    FParent: TWinControl;
    FParentWindowProc: TWndMethod;
    procedure WindowProc(var Message: TMessage);
    procedure SetParent(Value: TWinControl);
  published
    property Parent: TWinControl read FParent write SetParent;
  end;

procedure TMyComponent.SetParent(Value: TWinControl);
begin
  if Value=FParent then
    exit;

  if Assigned(FParent) then
    FParent.WindowProc := FParentWindowProc;
  FParentWindowProc := nil;

  FParent := Value;

  if Assigned(FParent) then
  begin
    FParentWindowProc := FParent.WindowProc;
    FParent.WindowProc := WindowProc;
  end;
end;

procedure TMyComponent.WindowProc(var Message: TMessage);
begin
  // do whatever we want with the message
  FParentWindowProc(Message);// delegate to parent's window procedure
end;

10-07 19:45