whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

31 Visitors Online


 
...change the MainForm at runtime?
Autor: Ahmed Ammar
[ Print tip ]  

Tip Rating (56):  
     


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.

 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners