首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

Delphi 啟動畫面動態進度提示:Splash Form 即時更新載入狀態資訊

     編輯:Delphi

效果說明

應用程式啟動時顯示無標題列 Splash 視窗,載入過程中(建立主視窗、資料模組、登入視窗等)即時更新提示文字(「正在初始化資料庫...」、「載入使用者權限...」),體驗媲美 Photoshop、Acrobat Reader。

Splash 動態提示效果

Splash 表單設計

  • Name := frm_Splash,BorderStyle := bsNone,BorderIcons := []。
  • TImage:Align := alClient 載入品牌圖片。
  • TLabel (lbl_Text):Transparent := True、字體顏色對比背景、置於適當位置。
  • 可選:TProgressBar 顯示進度百分比。

專案來源檔調度關鍵

begin
  Application.Initialize;
  frm_Splash := Tfrm_Splash.Create(Application);
  frm_Splash.Show;
  frm_Splash.Update;                 // 強制首次繪製

  Application.CreateForm(TfrmMain, frmMain);
  frm_Splash.lbl_Text.Caption := '建立主視窗完成';
  frm_Splash.Update;

  Application.CreateForm(TfrmData, frmData);
  frm_Splash.lbl_Text.Caption := '初始化資料模組';
  frm_Splash.Update;

  Application.CreateForm(TfrmLogin, frmLogin);
  frm_Splash.lbl_Text.Caption := '載入登入介面';
  frm_Splash.Update;

  frm_Splash.Hide;
  frm_Splash.Free;                   // 釋放資源
  Application.Run;
end.

關鍵技術點

  1. Update vs Repaint:Update 立即處理訊息佇列並重繪失效區域,Repaint 強制同步重繪可能阻塞;此處 Update 較佳。
  2. Application.ProcessMessages:若初始化耗時極長,可在關鍵點插入 Application.ProcessMessages 保持 UI 響應。
  3. 執行緒隔離:大型系統建議將耗時初始化移至背景執行緒,Splash 主執行緒僅輪詢進度並更新 UI,避免假死。
  4. 高 DPI:啟用 PerMonitorV2 感知,Splash 圖片需提供多解析度或使用向量圖。

進階:帶進度條的 Splash

// 在建立各模組前預估權重
const TotalWeight = 100;
var DoneWeight: Integer = 0;

procedure Bump(Weight: Integer; const Msg: string);
begin
  Inc(DoneWeight, Weight);
  frm_Splash.ProgressBar1.Position := DoneWeight * 100 div TotalWeight;
  frm_Splash.lbl_Text.Caption := Msg;
  frm_Splash.Update;
end;

begin
  // ...
  Bump(20, '載入組態檔...');   LoadConfig;
  Bump(30, '連線資料庫...');    ConnectDB;
  Bump(40, '快取基礎資料...');  CacheBaseData;
  Bump(10, '完成');
  // ...
end;
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved