Mega Code Archive

 
Categories / Delphi / Forms
 

How to change the MainForm at runtime

Title: How to change the MainForm at runtime procedure SetAsMainForm(aForm:TForm); var P:Pointer; begin P := @Application.Mainform; Pointer(P^) := aForm; end; {************************************} // Example of Usage: { Question: If my application is main.exe and the main form is form1, form1 displays when it runs. I would like to display other forms based on the parameter passed. main.exe param1 will display form2 as the first form main.exe param2 with display form3 as the first form Answer: } program Project1; uses Forms, Unit1 in 'Unit1.pas' {Form1}, Unit2 in 'Unit2.pas' {Form2}, Unit3 in 'Unit3.pas' {Form3}; {$R *.res} procedure SetAsMainForm(aForm:TForm); var P:Pointer; begin P := @Application.Mainform; Pointer(P^) := aForm; end; begin Application.Initialize; Application.CreateForm(TForm1, Form1); Application.CreateForm(TForm2, Form2); Application.CreateForm(TForm3, Form3); if Paramstr(1) = 'Param1' then SetAsMainForm(Form2); if Paramstr(1) = 'Param2' then SetAsMainForm(Form3); Application.Run; end.