2

我编写了一个淡入淡出的启动形式,显示一段时间,然后淡出。淡入淡出是用一个定时器来实现的,它也关闭表单。它工作正常。

我以模态方式显示表单,但我怀疑主表单直到启动表单关闭后才开始构建和显示。

然后我想如果我以非模态方式显示表单并使用 fsStayOnTop(即 SplashForm.Show 而不是 SplashForm.ShowModal),那么一旦显示启动表单,主表单就可以在启动表单后面初始化,这意味着应用程序已准备好飞溅形式关闭时去。

但是,我发现计时器事件不再触发。TApplication.OnIdle 事件也没有。是什么赋予了?

4

3 回答 3

5

你说这是闪屏。它是否在程序启动期间显示,在您Application.Run;到达 DPR 之前?如果是这样,那么 TApplication 事件循环还没有开始,所以你不会得到任何 OnIdle 事件。

于 2010-11-10T23:01:31.880 回答
2

Fade will not work with standard Timers because application loop will not function until you call Application.Run (as said by Mason), and timers are wrappers for a message based timer API mechanism.

You can't use thread based timers, because it will require Synchronize to work with the UI and Synchronize is message based mechanism.

But you can waste the time required to fade-in/fade out, so you could get a fancy application start and if you're looking for this I freely consider you're not worried about wasting a bit of time. I can explain a lot better with (working and tested) code example, so this will work for you:

USplashForm.pas:

//...
interface
//...
type
  TSplashForm = class(TForm)
    //...
  public
    procedure FadeIn;
    procedure FadeOut;
    //...
  end;

//...
implementation
//...
procedure TSplashForm.FadeIn;
begin
  AlphaBlend := True;
  AlphaBlendValue := 0;
  Show;
  Update;
  repeat
    AlphaBlendValue := AlphaBlendValue + 5;
    Update;
    Sleep(20);
  until AlphaBlendValue >= 255;
end;

procedure TSplashForm.FadeOut;
begin
  repeat
    AlphaBlendValue := AlphaBlendValue - 5;
    Update;
    Sleep(20);
  until AlphaBlendValue <= 5;
  Hide;
end;
//...

YourProject.dpr

var
  Splash: TSplashForm;

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Splash := TSplashForm.Create(nil);
  try
    Splash.FadeIn;
    //any initialization code here!!!
    Application.CreateForm(TMainForm, MainForm);
    MainForm.Show;
    MainForm.Update;
    //more code
    Sleep(500);  //I used it to delay a bit, because I only create one form and I have not initialization code at all!
    Splash.FadeOut;
  finally
    Splash.Free;
  end;
  Application.Run;
end.

My 5 cents, enjoy.

于 2010-11-11T00:18:55.847 回答
1

我通过以下方式完成了它:

  • 我从“自动创建表单”中删除了启动表单。
  • FormCreate我的主要形式中:

    with TfSplash.Create(Self) do Show;
    
  • 在启动表单中,我有以下内容:

    procedure TfSplash.FormShow(Sender: TObject);
    begin
      Timer.Enabled:=True;
    end;
    
    
    procedure TfSplash.TimerTimer(Sender: TObject);
    begin
      Release; // like free, but waits for events termination
    end;
    
于 2010-11-11T00:58:33.010 回答