Delphi TFrame[3] 自定义显示、销毁、隐藏事件(onShow 、OnHide、OnDestroy)


Delphi TFrame[3] 自定义显示、销毁、隐藏事件(onShow 、OnHide、OnDestroy)

Frames中本身不存在显示onShow 、OnHide、OnDestroy事件

方法1:通过设置框架的属性形式来模拟调用 frame的onshow过程

type
  TFrame2 = class(TFrame)
    Edit1: TEdit;
  private
    { Private declarations }
    fmOnShow : TNotifyEvent;
    fmOnHide : TNotifyEvent;
    
    procedure fmSetOnShow(ps: TNotifyEvent);
    procedure fmSetOnHide(ph: TNotifyEvent);
    
    procedure FrameOnShowHide(var M: TMessage); message CM_SHOWINGCHANGED;
  public
    { Public declarations }
    property OnShow : TNotifyEvent write fmSetOnShow;            //通过设置框架的属性形式来模拟调用 frame的onshow过程
    property OnHide : TNotifyEvent write fmSetOnHide;
  end;

implementation

{$R *.dfm}

{ TFrame2 }

procedure TFrame2.fmSetOnHide(ph: TNotifyEvent);
begin
   fmOnHide := ph;
end;

procedure TFrame2.fmSetOnShow(ps: TNotifyEvent);
begin
  fmOnShow := ps;
end;

procedure TFrame2.FrameOnShowHide(var M: TMessage);
begin
  inherited;
  
  if Showing then
  if Assigned( fmOnShow ) then
      fmOnShow( Self )
  else
    if Assigned( fmOnHide ) then
      fmOnHide( Self );
end;

调用:

    procedure FrameShiJian(Sender: TObject);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FrameShiJian(Sender: TObject);
begin
  Frame2.Edit1.Text:='Hi,Delphi';
  ShowMessage('hi,滔Roy');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Frame2.OnShow:=FrameShiJian;  //放在onshow之前
end;

方法2:继承 AfterConstruction 和 BeforeDestruction,类似OnCreate 和 OnDestroy

type
  TFrame3 = class(TFrame)
    Label1: TLabel;
    Image1: TImage;
  private
    { Private declarations }
  public
    { Public declarations }
    procedure AfterConstruction; override;    //类似OnCreate事件
    procedure BeforeDestruction; override;    //类似OnDestroy事件
  end;

implementation

{$R *.dfm}

{ TFrame3 }

procedure TFrame3.AfterConstruction;
begin
  inherited;
  label1.Caption:='Hi,滔Roy!';
end;

procedure TFrame3.BeforeDestruction;
begin
  inherited;

end;

创建时间:2021.12.16  更新时间:

相关