带登录/注销的 Delphi 应用程序 - 如何实现? [英] Delphi application with login / logout - how to implement?

查看:15
本文介绍了带登录/注销的 Delphi 应用程序 - 如何实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用程序有一个登录表单和一个主表单.

Application has a Login form and a Main form.

Applications DPR 文件有代码首先加载登录表单,当登录表单返回登录成功时,然后创建并加载主表单.

Applications DPR file has code to load Login form first, and when Login form returns successful login, then Main form is created and loaded.

当用户通过主窗体中的菜单命令注销时,应关闭主窗体并加载登录窗体.

When user logs out via a menu command in Main form, it should close the Main form and load the Login form.

仅当用户在主表单中选择退出时(或当用户从登录表单中取消时),应用程序才会退出.

Application exits only when user selects Exit in the Main form (or when user Cancels out of the Login form).

使用应用程序的 DPR 文件中的代码,是否可以对此进行编码?

Using code in the application's DPR file, is it possible to code this?

这是目前存在的代码:

program H;

uses
  Forms,
  SysUtils,
  Registry,
  MidasLib,
  Dialogs,
  Controls,
  uDatamod in 'uDatamod.pas' {datamod: TDataModule} ,
  uMain in 'uMain.pas' {fMain} ,
  uMtlUpd in 'uMtlUpd.pas' {fMtlUpd} ,
  uReportPrv in 'uReportPrv.pas' {fReportPrv} ,
  uCamera in 'uCamera.pas' {fCamera} ,
  uConfig in 'uConfig.pas' {fConfig} ,
  uFuncs in 'uFuncs.pas',
  uLogin in 'uLogin.pas' {fLogin} ,
  uAdmin in 'uAdmin.pas' {fAdmin};

// MidasLib is required.

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.Title := 'HTech';

  if ((ParamCount = 1) and (UpperCase(ParamStr(1)) = '/CONFIG')) or
    (getHServerHostName = EmptyStr) then
  begin
    Application.CreateForm(TfConfig, fConfig);

    Application.Run;
  end
  else
  begin
    if not testHServerConnection then
    begin
      ShowMessage('Error: Could not connect to HServer');

      Exit;
    end;

    Application.CreateForm(Tdatamod, Datamod);

    while not TerminateApplicationFlag do
    begin

      fLogin := TfLogin.Create(Application);
      try
        if fLogin.ShowModal = mrOk then
        begin
          LoggedInEmployeeID := fLogin.FEmployeeID;
          LoggedInEmployeeNm := fLogin.edtFirstName.Text + ' ' +
            fLogin.edtLastName.Text;
          AdminLogin := fLogin.FAdminUser;
          FinanceLogin := fLogin.FFinanceUser;
        end
        else
        begin

          FreeAndNil(fLogin);
          FreeAndNil(Datamod);
          Exit;
        end;
      finally
        // FreeAndNil(fLogin);
      end;

      if AdminLogin then
        Application.CreateForm(TfAdmin, fAdmin)
      else
      begin
        FreeAndNil(fLogin);
        if not Assigned(fMain) then
          Application.CreateForm(TfMain, fMain);
        fMain.FHServerHost := getHServerHostName;
      end;

      Application.Run;

    end;
  end;

end.

上述代码的问题是,在一次迭代后(用户在Main表单中执行Logout后),应用程序退出(控制权返回给操作系统),因为fLogin.ShowModal"退出时没有显示Login表单.

The problem with the above code is that after one iteration (after user performs Logout in Main form), the application exits (control is returned to the operating system) because " fLogin.ShowModal " exits without showing the Login form.

这是主窗体中的代码:

Procedure LogoutProcedure;
  begin
    TerminateApplicationFlag := False;
    Close;
  end;

Procedure ExitProcedure;
  begin
    TerminateApplicationFlag := True;
    Close;
  end;

我一直坚持这一点,希望得到任何建议或更正以使其正常工作.

I'm stuck with this and would appreciate any advice or corrections in getting it to work.

提前致谢.

问候,
史蒂夫·法莱罗

Regards,
Steve Faleiro

推荐答案

也许这个非常简单的解决方案就足够了:

Maybe this very simple solution is sufficient:

项目文件:

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.

主要形式:

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.

和登录表单:

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.

现在,这个答案在这里有效,在 Delphi 7 中效果很好,但我怀疑更新版本的问题是 Application.MainFormOnTaskbarApplication.ShowMainForm默认为 True.如果是这样,请尝试将它们设置为 False.

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.

这篇关于带登录/注销的 Delphi 应用程序 - 如何实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆