Every Delphi application must have a main form. The main form is the first form created in the body of the application. When the main form closes, the application terminates.
When a new project is created, the first form added to the project automatically becomes the value of the Application's MainForm property. To assign a different form to the MainForm property, use the Forms page of the Project|Options dialog box at design time.
Once the application is run, you cannot change the main form of the application.
No Main Form?What if you need to create an application where every form is equally important (for example like IE) - where closing the main form will *not* close the application.
If from the first form created (or better to say: displayed to the user) you can create another form - you want the application to continue running even if the user closes the first form!
Hide the Main FormOne way to "remove" the main form from a Delphi application is to hide it when the application is started. Since every Delphi application MUST have a main form - your only option is not to show the main form when the application is executed.
This is rather simple, just go to the project's source code and add (after "Application.Initialize") a line like
Therefore, to hide the main form at startup, set ShowMainForm to false in the main project file before the call to the Run method. Make sure that the Visible property of the form is also false.
Application.ShowMainForm := false;
Now, when you do not have a main form, how do you know when to terminate the application? Since there's no main form - if all other forms are closed you application will still run - the main form is hidden and therefore the user cannot close it - and therefore exit from the application.
A Fake Main FormLet's have a fake main form that host a button to create another instance of this fake main form.
Of course, when the main form is created we have to show this fake main form since the real main form will be hidden.
Here's how the project's source looks like if the real main form is named TMainForm.
Since the real main form is hidden, you need to make sure the fake one gets displayed to the user when the application is run. Use the OnCreate event of the main form to create an instance of the main form.
//dpr source begin Application.Initialize; Application.ShowMainForm := false; Application.CreateForm(TMainForm, MainForm) ; Application.Run; end.
In the code below, the fake form is named "TAppForm".
There's a button on the fake main form that will create another instance of this fake main form:
//OnCreate event of the real main form procedure TMainForm.FormCreate(Sender: TObject) ; begin with TAppForm.Create(Application) do begin Show; end; end;
Only one more little thingy to take into account - close the application when the last fake main form is closed:
//"CreateButton" OnClick even handler procedure TAppForm.CreateButtonClick(Sender: TObject) ; begin with TAppForm.Create(Application) do begin Show; end; end;
The FormCount property of the Screen object can be used with Forms to iterate over all the forms in an application.
//fake main form OnClose procedure TAppForm.FormClose(Sender: TObject; var Action: TCloseAction) ; begin Action := caFree; //TRUE MAIN and this fake instance if Screen.FormCount = 2 then begin Application.Terminate; end; end;
If the number of active forms is 2, and the fake main form is about to close - close the entire application using Application.Terminate. The other running form - is the real main form itself :)
That's it. Download the source code and try for yourself.