Create animated splashscreen delphi 7

2019-06-24 07:30发布

问题:

I'm trying to make an animated splashscreen while my app loads his database. I already created a splashscreen but I want to make the image "move" from left to right while the db is getting converted. Been searching for a while now but all I could find is about progress bars...

Here's my code:

SplashScreen := TSplashScreen.Create(Application) ;
SplashScreen.Show;
Application.Initialize;
SplashScreen.Update;
SplashScreen.lblStatus.Caption:='Loading...';
SplashScreen.Update;
SplashScreen.lblStatus.Caption:='Updating database...';
SplashScreen.Update;
Application.Initialize;
Application.CreateForm(TfmMain, fmMain);
Sleep(1000);

Application.CreateForm(TfmPrefs, fmPrefs);
Application.CreateForm(TfmCode, fmCode);
Application.CreateForm(TfmEmps, fmEmps);
Application.CreateForm(TfmRest, fmRest);
Application.ShowMainForm:=FALSE;

SplashScreen.Hide;
SplashScreen.Free;
Application.Run;

On my splashscrren form, I created 5 duplicates of the same image and while the Main Form is created, I want the image to be Visible and not visible in alternance... ex:

while my db loads... begin
Splashscreen.Image1.Visible:=FALSE;
SplashScreen.Update;
Sleep(25);
SplashScreen.Image1.Visible:=FALSE;
SplashScreen.Update;
SplashScreen.Image2.Visible:=TRUE;....

and so on!

Any thoughts?

回答1:

Doing heavy work in the main thread during startup (like initializing database and many forms) does not work well with splash screens. The main thread is too occupied to do anything with the GUI. Putting Sleep into the code will not work, since this will stop the main thread to do any work at all.

This leaves you with two options :

  1. Do your database initializations in another thread. Sometimes also only initializing the main form is a good option. The database thread can send progress messages to the splash form via PostMessage calls.

  2. Start the splash screen in a separate thread. This is kind of tricky, because you may not use the VCL from another thread. And also you must avoid blocking the message queue. Luckily, Peter Below has made a good example how to do a threaded splash screen using only windows api calls.

There are some more information in this SO thread : displaying-splash-screen-in-delphi-when-main-thread-is-busy.