Inno Installer启动画面实现渐显渐隐效果的技术问询
Inno Installer 实现启动画面渐显渐隐效果
你的基础启动画面功能已正常,要实现先渐显、再停留、最后渐隐的效果,无需依赖Inno内置特殊参数,通过调用Windows API控制窗口透明度即可实现,以下是具体方案:
核心原理
Inno Installer的自定义窗口本身不支持透明度渐变,需通过Windows API启用窗口的分层属性,然后逐步调整透明度值(范围0-255,0为完全透明,255为完全不透明),实现平滑的渐显/渐隐动画。
修改后的完整代码
[Code] const WS_EX_LAYERED = $80000; // 启用窗口分层的扩展样式 LWA_ALPHA = $2; // 通过透明度值控制窗口可见性 // 声明所需Windows API函数 function GetWindowLongPtr(hWnd: HWND; nIndex: Integer): LongPtr; external 'GetWindowLongPtrW@user32.dll stdcall'; function SetWindowLongPtr(hWnd: HWND; nIndex: Integer; dwNewLong: LongPtr): LongPtr; external 'SetWindowLongPtrW@user32.dll stdcall'; function SetLayeredWindowAttributes(hWnd: HWND; crKey: COLORREF; bAlpha: Byte; dwFlags: DWORD): BOOL; external 'SetLayeredWindowAttributes@user32.dll stdcall'; procedure InitializeWizard; var SplashForm: TForm; SplashBitmap: TBitmapImage; Alpha: Byte; begin // 创建启动画面窗口 SplashForm := CreateCustomForm; SplashForm.BorderStyle := bsNone; SplashBitmap := TBitmapImage.Create(SplashForm); SplashBitmap.AutoSize := True; SplashBitmap.Align := alClient; SplashBitmap.Left := 0; SplashBitmap.Top := 0; SplashBitmap.Stretch := True; SplashBitmap.Parent := SplashForm; // 加载启动图片 ExtractTemporaryFile('Dahlia.bmp'); SplashBitmap.Bitmap.LoadFromFile(ExpandConstant('{tmp}') + '\Dahlia.bmp'); SplashForm.Width := SplashBitmap.Width; SplashForm.Height := SplashBitmap.Height; SplashForm.SizeAndCenterOnShow := True; // 启用窗口分层效果,开启透明度控制权限 SetWindowLongPtr(SplashForm.Handle, GWL_EXSTYLE, GetWindowLongPtr(SplashForm.Handle, GWL_EXSTYLE) or WS_EX_LAYERED); // 初始透明度设为0,显示窗口 SetLayeredWindowAttributes(SplashForm.Handle, 0, 0, LWA_ALPHA); SplashForm.Show; SplashForm.Refresh; // 渐显动画:从完全透明到完全显示 for Alpha := 0 to 255 do begin SetLayeredWindowAttributes(SplashForm.Handle, 0, Alpha, LWA_ALPHA); SplashForm.Refresh; Sleep(10); // 控制渐变速度,数值越小动画越快 end; // 启动画面停留时长(可自定义,单位:毫秒) Sleep(5000); // 渐隐动画:从完全显示到完全透明 for Alpha := 255 downto 0 do begin SetLayeredWindowAttributes(SplashForm.Handle, 0, Alpha, LWA_ALPHA); SplashForm.Refresh; Sleep(10); end; // 关闭启动画面,进入安装向导 SplashForm.Close; end;
关键参数与调整说明
- WS_EX_LAYERED:必须启用的窗口扩展样式,是实现透明度控制的核心前提
- LWA_ALPHA:指定通过透明度值(0-255)控制窗口可见性,替代颜色键控方式
- 渐变速度:
Sleep(10)的数值决定动画快慢,改为5可加快渐变,改为20则减慢 - 停留时长:
Sleep(5000)是渐显后画面停留的时间,可根据需求调整为任意长度
内容的提问来源于stack exchange,提问作者Lukáš Lenghard
相关产品推荐
相关产品推荐

