定义全局变量:
quietlyCreate : Boolean;
theForm : TForm;
Form1是主MDIForm , Form2 是 MDIChild
在Form1对应的Unit1中如下:
procedure TForm1.Button1Click(Sender: TObject);
begin
quietlyCreate := True;
theForm := TForm2.Create(self);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
theForm.Show;
end;
Form2如下:
uses Unit2;
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
quietlyCreate := True;
theForm := TForm2.Create(self);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
theForm.Show;
end;
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TForm2 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
protected
procedure CreateParams(VAR Params: TCreateParams);
override;
end;
var
Form2: TForm2;
implementation
uses unit1;
{$R *.DFM}
procedure TForm2.CreateParams(VAR Params: TCreateParams);
begin
Inherited CreateParams(Params);
if QuietlyCreate then
Visible := false;
end;
end.
应该没有问题了。