主窗体试图在关闭登录窗体时显示。

7
这是 *.dpr 文件:
program Project1;

uses
  Vcl.Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2};

{$R *.res}
var
  MainForm: TForm1;

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm1, Form1);
  Login;
  Application.Run;
end.

登录表单:

unit Unit2;

interface

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

type
  TForm2 = class(TForm)
    Button1: TButton;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

procedure Login;

implementation

{$R *.dfm}

Uses Unit1;


procedure Login;
begin
  with TForm2.Create(nil) do
  try
    Application.MainForm.Hide;
    if ShowModal = mrOK then
      Application.MainForm.Show
    else
      Application.Terminate;
  finally
    Free;
  end;
end;

end.

主表单:

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

Uses Unit2;

procedure TForm1.Button1Click(Sender: TObject);
begin
Login;
end;

end.

两个按钮都设置为模态 :mrOK。 登录表单没有自动创建,但在可用表单列表中。

问题是这样的: 如果你在不点击按钮的情况下关闭登录表单,主表单会短暂地显示然后关闭(当然,应用程序也会关闭)。这发生得非常快。看起来像闪烁。

我该如何取消主表单尝试在关闭登录表单时显示自己的尝试?

此外,设置:

Application.MainFormOnTaskbar := False;

也没有帮助...

2个回答

5

在显示登录前,不要隐藏主窗体,而是应该使用初始化应用程序来启动并隐藏主窗体。方法是在创建主窗体前添加Application.ShowMainForm := false

Application.Run 在关闭登录窗体后执行,根据Application.ShowMainForm变量的值来显示或者隐藏主窗体,默认值为true。这样会引起闪烁,因为主窗体会在应用程序终止之前显示出来。

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.ShowMainForm := false;
  Application.CreateForm(TForm1, Form1);
  Login;
  Application.Run;
end.

登录过程由主窗体使用,以便用户可以登录和注销。他们不能直接访问主窗体。这就是一开始隐藏它的原因。提示的方法并不会在尝试显示时消除主窗体。 - user3351050
然后在登录过程中留下Application.MainForm.Hide,只需跳过启动时显示主窗体的步骤。 - Dalija Prasnikar
4
通常情况下,这种方法是不好的设计。如果您不希望用户看到“MainForm”,那么就根本不要创建它。如果“Login”失败,你可以直接退出应用程序,甚至不需要调用“Application.Run”。如果“Login”需要使用“MainForm”的代码,那么这段代码应该在“Login”自身内部,或者至少移到“MainForm”之外的另一个类/单元中。“Login”和“MainForm”不应该互相知道。 - Remy Lebeau
@RemyLebeau 我认为如果您不创建主窗体,只是创建登录窗体而不使用 Application.CreateForm(...)Application.MainFormOnTaskbar := true,那么登录窗体就没有任务栏图标。 - ventiseis
1
@ventiseis 如果登录表单需要在任务栏上显示,可以重写 CreateParams() 方法以启用该行为。 - Remy Lebeau
显示剩余4条评论

3
将您的登录过程转换为函数并更改主代码:
将登录相关的代码封装成一个函数,并在主代码中调用该函数进行登录。
    program Project1;

uses
  Vcl.Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2};

{$R *.res}
var
  MainForm: TForm1;

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  if Login then
    begin
      Application.CreateForm(TForm1, Form1);
      Application.Run;
    end
  else Application.Terminate;
end.

我看不出你在登录表单中检查了什么。我认为你应该只返回TRUE(登录成功)或FALSE作为结果,而不是试图操纵其他表单。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接