生成加载画面

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.

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页