需求是这样:相关联得一个嵌套窗体,想要去关闭子窗体的同时去关闭父窗体,了解到Notification这个概念,通过 X.FreeNotification(Y) 将控件Y的 Free 事件与X的 Notification 事件进行关联,这样 Y.Free 的时候就会通知X的 Notification ,只需要重写Y的 Notification,就能达到控制其他关联控件的目的。
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;