5,928
社区成员




//根据名称检查tab是否已经存在
function FindTab(pagecontrol:TPageControl;tabName:string):integer;
var
i:integer;
begin
Result := -1;
for i := 0 to pagecontrol.PageCount - 1 do
begin
if pagecontrol.Pages[i].Name = tabName then
begin
Result := i;
Break;
end;
end;
end;
procedure FormToSheet(pagecontrol:TPageControl;frm:TForm;tabName,tabCaption:string);
var
i:integer;
tab:TTabSheet;
begin
i := FindTab(pagecontrol,tabName);
if i > 0 then
begin
pagecontrol.Pages[i].TabVisible := True;
pagecontrol.ActivePageIndex := i;
end
else
begin
tab := TTabSheet.Create(pagecontrol);
tab.Name := tabName;
tab.Caption := tabCaption;
tab.PageControl := pagecontrol;
pagecontrol.ActivePage := tab;
frm.ParentWindow := tab.Handle;
frm.Tag :=tab.TabIndex;
tab.InsertControl(frm);
frm.Show;
end;
frm.Show;
end;
销毁时,可以查找tab上的组件,找到窗体,然后freeandnil,当然也可以通过指针处理,在tag中保存窗体指针地址,销毁时根据这个地址去处理
unit CHILDWIN;
interface
uses Windows, Classes, Graphics, Forms, Controls, StdCtrls;
type
TMDIChild = class(TForm)
Memo1: TMemo;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
implementation
uses MAIN;
{$R *.dfm}
procedure TMDIChild.FormClose(Sender: TObject; var Action: TCloseAction);
var
temp ,I,J : Integer;
begin
Action := caFree;
if MainForm.MouseClose then Exit;
temp := 0;
for I := 0 to MainForm.pc.PageCount - 1 do
begin
if MainForm.ActiveMDIChild = MainForm.Formarray[I] then
begin
MainForm.len:=MainForm.len-1;
if I < MainForm.pc.PageCount - 1 then
begin
for J := I to MainForm.pc.PageCount - 1 do
MainForm.Formarray[J] := MainForm.Formarray[J + 1];
end;
temp := MainForm.pc.PageCount - 1;
MainForm.Formarray[temp] := nil;
MainForm.pc.Pages[I].Destroy;
Break;
end;
end;
end;
end.