Where is the "Main form" name stored in Delphi?

Which physical file stores the main form name for a Delphi application?

eg MyApplication has a form MyForm which is set as the "Main form" via Project options. Where is the information "Main Form = MyForm" actually stored?

In the Delphi IDE the application "Main form" is specified via the menu: Project | Options | Forms Project | Options | Forms Project | Options | Forms .

The obvious file would be the .bdsproj or the .dpr but there doesn't seem to be anything in either of these that indicates which form is the "main" one.


It's in the project (.DPR) file. The first call to Application.CreateForm() with a form as a parameter identifies the application's main form.

Note that a TDataModule doesn't satisfy the above requirement; that's actually useful, as you can autocreate a datamodule before your main form and then access that datamodule in the main form's constructor.


Just to add to Ken White's answer.

If you look at the source for CreateForm:

procedure TApplication.CreateForm(InstanceClass: TComponentClass; var Reference);
var
  Instance: TComponent;
begin
  Instance := TComponent(InstanceClass.NewInstance);
  TComponent(Reference) := Instance;
  try
    Instance.Create(Self);
  except
    TComponent(Reference) := nil;
    raise;
  end;
  if (FMainForm = nil) and (Instance is TForm) then
  begin
    TForm(Instance).HandleNeeded;
    FMainForm := TForm(Instance);
  end;
end;

You see that the function (despite its name) can be used to create other components. But only the first component that is a TForm and that is created succesfully, can be the main form.

And then a rant on global variables.

Yes globals are often wrong, but for an application object and a mainform object you can make an exception. Although you can omit the global for the mainform but you need to edit the dpr file yourself:

Change:

begin
  Application.Initialize;
  Application.CreateForm(TMyMainForm, MyMainFormGlobal);
  Application.Run
end.

To:

procedure CreateMain;
var
  mainform : TMyMainForm;
begin
  Application.CreateForm(TMyMainForm, mainform);
end;

begin
  Application.Initialize;
  CreateMain;
  Application.Run
end.

And you lost all global forms.

链接地址: http://www.djcxy.com/p/59656.html

上一篇: 在Windows上创建一个OpenSSL证书

下一篇: Delphi中存储的“主窗体”名称在哪里?