Delphi application with login / logout - how to implement?

后端 未结 1 859
我在风中等你
我在风中等你 2020-11-27 22:06

Application has a Login form and a Main form.

Applications DPR file has code to load Login form first, and when Login form returns successful login, then Main form i

1条回答
  •  旧时难觅i
    2020-11-27 22:46

    Maybe this very simple solution is sufficient:

    The project file:

    program Project1;
    
    uses
      Forms,
      FMain in 'FMain.pas' {MainForm},
      FLogin in 'FLogin.pas' {LoginForm};
    
    {$R *.res}
    
    var
      MainForm: TMainForm;
    
    begin
      Application.Initialize;
      Application.CreateForm(TMainForm, MainForm);
      Login;
      Application.Run;
    end.
    

    The main form:

    unit FMain;
    
    interface
    
    uses
      Classes, Controls, Forms, StdCtrls, FLogin;
    
    type
      TMainForm = class(TForm)
        LogoutButton: TButton;
        procedure LogoutButtonClick(Sender: TObject);
      end;
    
    implementation
    
    {$R *.dfm}
    
    procedure TMainForm.LogoutButtonClick(Sender: TObject);
    begin
      Login;
    end;
    
    end.
    

    And the login form:

    unit FLogin;
    
    interface
    
    uses
      Classes, Controls, Forms, StdCtrls;
    
    type
      TLoginForm = class(TForm)
        LoginButton: TButton;
        CancelButton: TButton;
        procedure FormCreate(Sender: TObject);
      end;
    
    procedure Login;
    
    implementation
    
    {$R *.dfm}
    
    procedure Login;
    begin
      with TLoginForm.Create(nil) do
      try
        Application.MainForm.Hide;
        if ShowModal = mrOK then
          Application.MainForm.Show
        else
          Application.Terminate;
      finally
        Free;
      end;
    end;
    
    procedure TLoginForm.FormCreate(Sender: TObject);
    begin
      LoginButton.ModalResult := mrOK;
      CancelButton.ModalResult := mrCancel;
    end;
    
    end.
    

    Now, this answer works here, quite well with Delphi 7, but I suspect problems with more recent versions were Application.MainFormOnTaskbar and Application.ShowMainForm are True by default. When so, try to set them to False.

    0 讨论(0)
提交回复
热议问题