1
votes

My program is written in Delphi 10. It shows a login dialog before it actually starts, and everything works fine.

But if I minimize the program by clicking on the Windows Taskbar (while login dialog active), it will remain minimized forever.

Does anyone knows the solution ?

Unit1.pas

class function TForm1.Execute: Boolean;
var
 Dlg:TForm1;
begin
 Dlg:=TForm1.Create(nil);
 try
   Result:=Dlg.ShowModal=mrOk;
 finally
  Dlg.Free;
 end;
end;

Project1.dpr

begin
  if not TForm1.Execute then //if i press on task bar, window will remain minimized forever.
   Exit;
  Application.Initialize;
  Application.MainFormOnTaskbar := True; 
  //Moving TForm1.Execute here has no effect
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end
1
Why are you using the same class for both a modal dialog and the MainForm? This code makes no sense. - Remy Lebeau
It's just an example to reproduce error. Renaming class will not solve this problem. - user3715238
You should let the application initialise and run Application.CreateForm before you have if not TForm1.Execute... it would also be better to use the application created instance, so using Execute as an object method, not a class method. - Rob Lambden
Hello, can you post a brief example? I dont clearly understand how to create form with Application.CreateForm and exit program in case of modal result of this form is mrCancel - user3715238
What happens if you create the main form before the login form? - David Heffernan

1 Answers

0
votes

I made a sample code showing how to have a login form shown BEFORE main form. I checked (Win10) that minimizing the window with task bar click, it can be reactivated with task bar click.

In the login form, I just made it with two buttons, one for login OK and one for login failed, without actuallu checking any usercode/password. I assumed you was able to do that part.

Project file:

program LoginDemo;
uses
  Vcl.Forms, Vcl.Dialogs, Vcl.Controls,
  LoginDemoMain in 'LoginDemoMain.pas' {DemoMainForm},
  LoginDemoDialog in 'LoginDemoDialog.pas' {DemoLoginForm};
{$R *.res}
begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TDemoLoginForm, DemoLoginForm);
  try
      if DemoLoginForm.ShowModal <> mrOK then begin
          ShowMessage('Invalid login, sorry...');
          Exit;
      end;
  finally
      DemoLoginForm.Free;
  end;
  Application.CreateForm(TDemoMainForm, DemoMainForm);
  Application.Run;
end.

Login dialog:

unit LoginDemoDialog;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TDemoLoginForm = class(TForm)
    LoginOKButton: TButton;
    LoginFailedButton: TButton;
    Label1: TLabel;
    procedure LoginFailedButtonClick(Sender: TObject);
    procedure LoginOKButtonClick(Sender: TObject);
  end;

var
  DemoLoginForm: TDemoLoginForm;

implementation

{$R *.dfm}

procedure TDemoLoginForm.LoginFailedButtonClick(Sender: TObject);
begin
    Close;
    ModalResult := mrCancel;
end;

procedure TDemoLoginForm.LoginOKButtonClick(Sender: TObject);
begin
    Close;
    ModalResult := mrOK;
end;

end.

Main form:

unit LoginDemoMain;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TDemoMainForm = class(TForm)
    Label1: TLabel;
    ExitButton: TButton;
    procedure ExitButtonClick(Sender: TObject);
  end;

var
  DemoMainForm: TDemoMainForm;

implementation

{$R *.dfm}

procedure TDemoMainForm.ExitButtonClick(Sender: TObject);
begin
    Close;
end;

end.