我有一个需要几秒钟才能加载的应用程序(大量初始化)。GUI 在启动期间冻结。所以我想创建一个在应用程序加载时淡入淡出的启动画面。我使用TBackgroundWorker组件在后台线程中执行动画。
但是,当我使用这个组件时,会发生一些奇怪的事情:当它发出“工作完成”信号时(请参阅 BackgroundWorkerWorkComplete),我同时打开的消息对话框会自动关闭。
procedure TMainForm.ButtonStartSplashClick(Sender: TObject);
VAR
frmSplash: TfrmSplash;
begin
frmSplash:= TfrmSplash.Create(NIL);
frmSplash.StartAnimation;
//MessageBox(Handle, 'Hi', nil, MB_OK); // This remains on screen
Application.MessageBox(PChar('Hi'), PChar('Box'), MB_ICONINFORMATION); // This is automatically closed when the background thread is done
end;
这是启动画面:
procedure TfrmSplash.StartAnimation;
begin
Show;
BackgroundWorker.Execute;
end;
procedure TfrmSplash.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action:= caFree;
end;
procedure TfrmSplash.BackgroundWorkerWork(Worker: TBackgroundWorker);
VAR i: Integer;
begin
for i:= 1 to 255 DO
begin
AlphaBlendValue:= i; // do not access GUI directly from thread
Sleep(30);
end;
end;
procedure TfrmSplash.BackgroundWorkerWorkComplete(Worker: TBackgroundWorker; Cancelled: Boolean);
begin
Close; // At this point, the msg box will be closed also
end;
我觉得奇怪的是 MessageBox 保留在屏幕上,而 Application.MessageBox 没有(自动关闭)。
为什么关闭 TfrmSplash 也会关闭消息框?