[Delphi] Notification Free關聯事件

需求是這樣:相關聯得一個嵌套窗體,想要去關閉子窗體的同時去關閉父窗體,瞭解到Notification這個概念,經過 X.FreeNotification(Y) 將控件Y的 Free 事件與X的 Notification 事件進行關聯,這樣 Y.Free 的時候就會通知X的 Notification ,只須要重寫Y的 Notification,就能達到控制其餘關聯控件的目的。ide

Syntax
pascal type TOperation = (opInsert, opRemove); procedure Notification(AComponent: TComponent; Operation: TOperation);code

某些控件在銷燬以前會自動調用此過程。 通知過程強制控件對做爲AComponent參數傳遞的關聯組件上做爲AOperation參數傳遞的操做做出響應。orm

unit frmMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  protected
    procedure Notification(AComponent: TComponent;
      Operation: TOperation); override;
  private

    { Private declarations }
  public
    { Public declarations }
  end;

  TNotificationHelper = class(TComponent)
  private
    FDialogForm: TForm;
    FTabControl: TControl;
  protected
    procedure Notification(AComponent: TComponent;
      Operation: TOperation); override;
  public
    procedure Init(vPreviewForm, vDialogForm:TForm; vTabControl: TControl);
  end;

var
  Form1: TForm1;

implementation

uses
  frm1, frm2;

{$R *.dfm}


procedure TForm1.Button1Click(Sender: TObject);
begin
  if not assigned(frmMDI) then begin
    frmMDI := TfrmMDI.create(self);
  end;

  frmChild := TfrmChild.Create(Application);
  with frmChild do begin
    caption     := 'HAHAHAHA';
    Parent      := frmMDI;
    Align       := alClient;
    FormStyle := fsNormal;
    BorderStyle := bsNone;
    WindowState := wsMaximized;
    show;
  end;
  self.FreeNotification(frmChild);
  frmTDI.show;
end;

procedure TForm1.Notification(AComponent: TComponent; Operation: TOperation);
begin
  inherited Notification(AComponent, Operation);
  if (Operation = opRemove) and (AComponent=frmChild) then
  begin
    frmMDI.close;
    self.close;
  end;
end;
相關文章
相關標籤/搜索