需求是这样:相关联得一个嵌套窗体,想要去关闭子窗体的同时去关闭父窗体,了解到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;