让子窗体不自动创建(在project->options->autoCreate form控制),由程序动态创建,例如:
主窗体:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus;
type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
File1: TMenuItem;
new1: TMenuItem;
window1: TMenuItem;
Cascade1: TMenuItem;
procedure Cascade1Click(Sender: TObject);
procedure new1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
Unit2;
procedure TForm1.Cascade1Click(Sender: TObject);
begin
Cascade
end;
procedure TForm1.new1Click(Sender: TObject);
var
Form2:TForm2;
begin
Form2 := TForm2.Create(Self);
Form2.Caption := TimeToStr(Now);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Form1.WindowMenu := window1;
end;
end.
******************************************************************
子窗体:
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm2 = class(TForm)
procedure FormClose(Sender: TObject
var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.FormClose(Sender: TObject
var Action: TCloseAction);
begin
Action := caFree;
end;
end.
其中window1为主菜单中的一个顶端菜单项,即“window”,window1下边还有一个“Cascade”菜单项,即平铺,window1赋值给Form1.WindowMenu后,每打开一个子窗口Delphi都会在window1下增加一个和Cascade平级的菜单项,就像一般的MDI程序一样,另外window1菜单项下一定得有一个其他菜单项(如:Cascade),否则是不能工作的。