2

我正在开发一个新组件,我想处理所有父消息。

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

end;

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

4

1 回答 1

2

像这样:

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;
于 2012-12-15T14:43:36.857 回答