1

I have a Delphi XE forms application where I want to add a way to start the application from commandline with input parameters. When starting the application this way, I don't want the mainform to show, instead I want the application to perform a task and close, similar to a normal console project

How do I go about this? In the project source, I have tried various iterations of

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm7, Form7);
  if Paramstr(1) <> '' then begin
    Application.MainForm.Hide;
  end;
  Application.Run;
end.

but the form shows up just the same. And the compiler won't let me set visible during onShow

Bjarke Moholt
  • 313
  • 1
  • 4
  • 20
  • A common pattern when making your app invisible, is to have a tray icon so that users who want to get it back can do so. Check out TTrayIcon, built into the VCL for that. If your app was supposed to do some task and close automatically but could perhaps fail or time out (take too long) and need to let users reconfigure, then that's a common way to do that. – Warren P Nov 30 '15 at 14:38
  • Possible duplicate of [How can I start Delphi application with the hidden main form?](https://stackoverflow.com/questions/24441787/how-can-i-start-delphi-application-with-the-hidden-main-form) – user Jun 30 '17 at 22:29

2 Answers2

5

Set Application.ShowMainForm to False.

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm7, Form7);
  Application.ShowMainForm := Paramstr(1) = '';
  ....
end.
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
1

Setting the Application.ShowMainForm property to False before calling Application.Run() will accomplish what you are asking. However, unless your MainForm is actually needed for the task being performed, I would suggest you simply not create the MainForm at all when running in your command-line task mode:

begin
  Application.Initialize;
  if ParamCount() > 0 then
  begin
    // perform task as needed...
  end else
  begin
    Application.MainFormOnTaskbar := True;
    Application.CreateForm(TForm7, Form7);
    Application.Run;
  end;
end.
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770