Delphi设计一个软件启动画面,当双击Exe后,先显示的不是程序主界面而是一个具有缓冲加载效果的启动画面,这种启动窗体会增加软件的体验度,开发者可在显示启动画面的时候,让主程序载入相关组件,有效避免用户的等待时间,增加美观效果。
program SplashDemo;
uses
Forms,
Windows,
Controls,
Messages,
Main in 'Main.pas',
Splash in 'Splash.pas' {frmSplash};//这个窗体是最先弹出的窗体
{$R *.res}
var
hMutex: THandle;
FoundWnd: THandle;
ModuleName: string;
function EnumWndProc(hwnd: THandle; Param: Cardinal): Bool; stdcall;
//列表并且得到最初的第一个窗口实例的句柄,赋值给FoundWnd
var
ClassName, WinModuleName: string;
WinInstance: THandle;
begin
Result := True;
SetLength(ClassName, 100);
GetClassName (hwnd, PChar (ClassName), Length (ClassName));
ClassName := PChar(ClassName);
if ClassName = TfrmMain.ClassName then
begin
{10 获取目标窗口的模块名 }
SetLength(WinModuleName, 200);
WinInstance := GetWindowLong(hwnd, GWL_HINSTANCE);
GetModuleFileName (WinInstance, PChar (WinModuleName),
Length(WinModuleName));
WinModuleName := PChar(WinModuleName); // 调整长度
{20 比较模块名 }
if WinModuleName = ModuleName then
begin
FoundWnd := Hwnd;
Result := False; // 如果已找到,停止继续列举
end;
end;
end;
begin
{10 检测是否互斥已存在 }
HMutex := CreateMutex(nil, False, 'OneCopyMutex');
if WaitForSingleObject(hMutex, 0) <> WAIT_TIMEOUT then
begin
Application.Initialize;
{10.1 启动Splash屏幕 }
frmSplash := TfrmSplash.Create(nil);
try
with frmSplash do
begin
BeginLoad;
{10.2 装载主窗体}
Application.CreateForm(TfrmMain, frmMain); //切记:需要将主窗体的VISBLE:=FALSE;
Application.CreateForm(TfrmSplash, frmSplash);
UpdateLoadStatus('正在载入启动参数', 10);
{10.3 载入数据1的过程 }
//LoadSomeData1;
UpdateLoadStatus('正在载入数据1', 20);
{10.4 装载其他数据 }
//LoadSomeData2;
UpdateLoadStatus('载入其他数据2', 40);
//LoadSomeData3;
UpdateLoadStatus('开始装载数据3', 60);
//LoadSomeData4;
UpdateLoadStatus('开始装载数据4', 70);
//LoadSomeData5;
UpdateLoadStatus('开始装载数据5', 80);
//LoadSomeData6;
UpdateLoadStatus('开始装载数据6', 90);
{// 结束 }
{10.5 装载完毕 }
frmSplash.EndLoad;
end;
finally
frmSplash.Free;
end;
Application.Run;
end
else
begin
{20 获取当前模块名 }
SetLength(ModuleName, 200);
GetModuleFileName (HInstance, PChar(ModuleName), Length (ModuleName));
ModuleName := PChar(ModuleName); // 调整长度
{30 列表最初实例的窗口 }
EnumWindows(@EnumWndProc, 0);
if FoundWnd <> 0 then
begin
{40 最后显示最初实例的窗口 }
if not IsWindowVisible(FoundWnd) then
PostMessage(FoundWnd, wm_App, 0, 0);
SetForegroundWindow(FoundWnd);
end;
end;
end.
而在窗体中要声明一下变量并赋予相应代码
private
{ Private declarations }
procedure Repaintform;
public
{ Public declarations }
procedure beginload;
procedure endload;
procedure updateloadstatus(const astatustext: string;aprogress:Integer);
在单元代码中输入代码
procedure tfrmlogo.beginload;
begin
lblStatus.Caption:='正在加载中... ...';
g1.Progress:=0;
Repaintform;
end;
procedure tfrmlogo.endload;
begin
lblStatus.Caption:='载入窗体完成';
g1.Progress:=100;
Repaintform;
end;
procedure tfrmlogo.Repaintform;
begin
show;
Update;
Sleep(300);//方便动画演示,延长时间;
end;
procedure tfrmlogo.updateloadstatus(const astatustext: string;aprogress:Integer);
begin
lblStatus.Caption:=AStatustext;
g1.Progress:=aprogress;
Repaintform;
end;
procedure TfrmLogo.FormCreate(Sender: TObject);
begin
lblStatus.Caption:='';
g1.MinValue:=0;
g1.MaxValue:=100;
end;