我希望看到完全初始化的用户界面,标签显示"数据未加载",然后5秒后#34;数据加载"。
我从那里做了一个例子:What is the best way to autostart an action after OnShow event?
但是我没有看到任何内容,然后只有"数据被加载",在延迟之后。
代码如下:
StartupAction.dpr
program StartupAction;
uses
Vcl.Forms,
MainForm in 'MainForm.pas' {Form1};
{$R *.res}
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
MainForm.dfm
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 254
ClientWidth = 635
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object lbl1: TLabel
Left = 104
Top = 40
Width = 87
Height = 13
Caption = 'Data is not loaded'
end
end
MainForm.pas
unit MainForm;
//Load data after full UI initialization
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)
lbl1: TLabel;
private const
WM_STARTUP = WM_USER;
protected
procedure DoShow(); override;
private
procedure WMStartup(var Msg: TMessage); message WM_STARTUP;
procedure _loadData();
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.DoShow();
begin
inherited;
PostMessage(Handle, WM_STARTUP, 0, 0);
end;
procedure TForm1.WMStartup(var Msg: TMessage);
begin
inherited;
_loadData();
end;
procedure TForm1._loadData();
begin
Sleep(5000);
lbl1.Caption := 'Data is loaded';
end;
end.
在某种程度上有效的唯一解决方案是使用TTimer,延迟时间为500毫秒,但这不是一个真正的解决方案。