delphi之完美Splash方案(在TfrmMain.FormCreate里不断调用TfrmSplash显示加载进度文字,并且及时Update显示)
2016-03-02 22:14
567 查看
前言:网上有很多介绍delphi创建闪屏的代码,大多只是在程序开启前显示一个闪屏,但是却没有说如何在闪屏上显示程序加载的进度,于是笔者有意思介绍一下这种闪屏方式。
1.创建一个窗体(TfrmSplash),放入一个TImageBox,加载一幅图片,调整好TImageBox与图片的大小,然后在其上放入一个TLabel,name=LblStatus,用于显示加载进度文字。然后将TfrmSplash设置为不自动创建。
2.加入如下代码(代码很简单,就不用解释太多)
Delphi代码
unit UntFormSplash;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TfrmSplash = class(TForm)
Image1: TImage;
LblStatus: TLabel;
private
{ Private declarations }
FParam:Pointer;
public
{ Public declarations }
class function Execute(AParam:Pointer):Boolean;
procedure SetStatusText(Value: string);
published
property StatusText : string write SetStatusText;
end;
var
SplashForm: TfrmSplash;
implementation
{$R *.dfm}
{ TfrmSplash }
class function TfrmSplash.Execute(AParam:Pointer): Boolean;
begin
with TfrmSplash.Create(nil) do
try
FParam := AParam;
Result := ShowModal = mrOk;
finally
Free;
end;
end;
procedure TfrmSplash.SetStatusText(Value: string);
begin
LblStatus.Caption := Value;
Update; //这句非常重要,不加的话,界面会阻塞,文字也就不会更新显示
Sleep(1000); //这句根据自己实际情况来调整,主要是怕闪屏太快关闭,达不到效果
end;
end.
3. 在项目的.dpr文件中加入如下代码:
Delphi代码
begin
Application.Initialize;
SplashForm := TfrmSplash.Create(Application);
SplashForm.Show;
SplashForm.Update;
SplashForm.StatusText := '准备启动...';
SplashForm.Update;
Application.CreateForm(TDM, DM);
Application.CreateForm(TfrmMain, frmMain);
SplashForm.Hide;
SplashForm.Free;
Application.Run;
end.
4.这一步就是主窗体加载数据的时候,边加载边更新闪屏的进度文字了:
Delphi代码
procedure TfrmMain.FormCreate(Sender: TObject);
begin
with SplashForm do
try
StatusText := ('开始初始化内存...');
FCacheHash := TStringHashMap.Create(CaseInsensitiveTraits, 255);
FCurrentClients := TList.Create;
//VST.NodeDataSize := SizeOf(TTagCustomListItem);
//VST.RootNodeCount := 2;
VST.NodeDataSize := SizeOf(TMyTreeNodeDate);
StatusText :=('初始化内存完成');
StatusText :=('开始加载客户端列表...');
BuildGroupTree;
StatusText :=('加载客户端列表完成');
StatusText :=('开始加载分组信息...');
AddELVDefaultGroup;
StatusText :=('开始初始化内存');
StatusText :=('开始初始化数据...');
G_DefNetImpl := TDefNetImpl.Create();
G_DefNetImpl.RegisterObserver(Self);
StatusText :=('全部数据加载完毕,程序即将启动...');
finally
end;
end;
收功,试着运行一下吧,一个漂亮的splash诞生了.
http://peirenlei.iteye.com/blog/521581
1.创建一个窗体(TfrmSplash),放入一个TImageBox,加载一幅图片,调整好TImageBox与图片的大小,然后在其上放入一个TLabel,name=LblStatus,用于显示加载进度文字。然后将TfrmSplash设置为不自动创建。
2.加入如下代码(代码很简单,就不用解释太多)
Delphi代码
unit UntFormSplash;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TfrmSplash = class(TForm)
Image1: TImage;
LblStatus: TLabel;
private
{ Private declarations }
FParam:Pointer;
public
{ Public declarations }
class function Execute(AParam:Pointer):Boolean;
procedure SetStatusText(Value: string);
published
property StatusText : string write SetStatusText;
end;
var
SplashForm: TfrmSplash;
implementation
{$R *.dfm}
{ TfrmSplash }
class function TfrmSplash.Execute(AParam:Pointer): Boolean;
begin
with TfrmSplash.Create(nil) do
try
FParam := AParam;
Result := ShowModal = mrOk;
finally
Free;
end;
end;
procedure TfrmSplash.SetStatusText(Value: string);
begin
LblStatus.Caption := Value;
Update; //这句非常重要,不加的话,界面会阻塞,文字也就不会更新显示
Sleep(1000); //这句根据自己实际情况来调整,主要是怕闪屏太快关闭,达不到效果
end;
end.
3. 在项目的.dpr文件中加入如下代码:
Delphi代码
begin
Application.Initialize;
SplashForm := TfrmSplash.Create(Application);
SplashForm.Show;
SplashForm.Update;
SplashForm.StatusText := '准备启动...';
SplashForm.Update;
Application.CreateForm(TDM, DM);
Application.CreateForm(TfrmMain, frmMain);
SplashForm.Hide;
SplashForm.Free;
Application.Run;
end.
4.这一步就是主窗体加载数据的时候,边加载边更新闪屏的进度文字了:
Delphi代码
procedure TfrmMain.FormCreate(Sender: TObject);
begin
with SplashForm do
try
StatusText := ('开始初始化内存...');
FCacheHash := TStringHashMap.Create(CaseInsensitiveTraits, 255);
FCurrentClients := TList.Create;
//VST.NodeDataSize := SizeOf(TTagCustomListItem);
//VST.RootNodeCount := 2;
VST.NodeDataSize := SizeOf(TMyTreeNodeDate);
StatusText :=('初始化内存完成');
StatusText :=('开始加载客户端列表...');
BuildGroupTree;
StatusText :=('加载客户端列表完成');
StatusText :=('开始加载分组信息...');
AddELVDefaultGroup;
StatusText :=('开始初始化内存');
StatusText :=('开始初始化数据...');
G_DefNetImpl := TDefNetImpl.Create();
G_DefNetImpl.RegisterObserver(Self);
StatusText :=('全部数据加载完毕,程序即将启动...');
finally
end;
end;
收功,试着运行一下吧,一个漂亮的splash诞生了.
http://peirenlei.iteye.com/blog/521581
相关文章推荐
- DELPHI字符串、数组操作函数
- delphi 如何比较两个二维数组是否相等
- delphi获得当前鼠标坐标
- Delphi通过查找字符定位TADOQuery数据的位置
- Delphi在DBGridEh改变选中行颜色
- 未安装excel delphi listview导出到excel
- delphi程序在自身EXE后(插入)追加文件、数据
- delphi在64位系统下写注册表注意事项
- Delphi DBGrid图显用法
- 一个简单的MDI示范程序(Delphi)
- DELPHI如何调用存储过程
- delphi 中如何调用sql 存储过程
- Delphi Length函数
- Delphi 7 调用 C# WebService
- Delphi一共封装(超类化)了8种Windows基础控件和17种复杂控件
- Delphi基本图像处理方法汇总
- Delphi制作图像特殊显示效果
- delphi的TThread.CreateAnonymousThread导致界面卡死
- Delphi XE6 String转MemoryStream
- Delphi TXLSReadWriteII 导出EXCEL