4

几年前,当 Vista 首次发布时,我问了一个关于这个问题的问题,但从未解决过这个问题,并将其搁置为以后考虑的问题。

我有一个启动画面,我努力让它看起来很棒。这是一个 32bpp 的 alpha 混合 PNG。我有一些代码(如果需要,我可以挖掘!)在关闭桌面组合时在 Windows XP 或 Vista+ 下运行良好。然而,在 Vista+ 下,所有透明部分都是黑色的,破坏了它看起来很棒的一切!

所以,我的问题是这样的:因为任何人都能够将 32bpp alpha 混合的 PNG 显示为启动屏幕,并且在激活和不激活桌面组合的情况下都可以使用?如果需要,我不反对使用第三方组件,无论是免费的还是其他的。

理想情况下,这将在 Delphi 7 中工作。

更新:除了下面的答案很好,我发现 TMS TAdvSmoothSplashScreen 组件也很好地处理了这个任务,如果有点复杂的话。

4

2 回答 2

6

蒂姆,我刚刚在 Vista/D2007 上尝试了这个,选择了“Windows 经典”主题:

Delphi 中的 Alpha 混合启动画面 - 第 2 部分 http://melander.dk/articles/alphasplash2/2/

我看不到黑色背景......它看起来仍然很棒。

于 2009-08-04T18:38:07.953 回答
5

Bob S 链接的文章给出了正确答案。由于该文章包含您实际需要的相当多的额外信息,因此这是我通过它创建的表单/单元(请注意,您需要来自此处的 GraphicEx 库:

unit Splash2Form;

interface

    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, ExtCtrls, GraphicEx;

type
    TSplash2 = class(TForm)
    private
        { Private declarations }
    procedure PreMultiplyBitmap(Bitmap: TBitmap);
    public
        constructor Create(Owner: TComponent);override;
        { Public declarations }
        procedure CreateParams(var Params: TCreateParams);override;
    procedure Execute;
  end;

var
  Splash2: TSplash2;

implementation

{$R *.dfm}

{ TSplash2 }

constructor TSplash2.Create(Owner: TComponent);
begin
  inherited;
  Brush.Style := bsClear;
end;

procedure TSplash2.CreateParams(var Params: TCreateParams);
begin
    inherited;
end;

procedure TSplash2.Execute;
var exStyle: DWORD;
    BitmapPos: TPoint;
  BitmapSize: TSize;
  BlendFunction: TBlendFunction;
  PNG: TPNGGraphic;
  Stream: TResourceStream;
begin
  // Enable window layering
  exStyle := GetWindowLongA(Handle, GWL_EXSTYLE);
  if (exStyle and WS_EX_LAYERED = 0) then
    SetWindowLong(Handle, GWL_EXSTYLE, exStyle or WS_EX_LAYERED);

  PNG := TPNGGraphic.Create;
  try

      Stream := TResourceStream.Create(HInstance, 'SPLASH', RT_RCDATA);
      try
          PNG.LoadFromStream(Stream);
    finally
        Stream.Free;
        end;

    PreMultiplyBitmap(PNG);

      ClientWidth := PNG.Width;
    ClientHeight := PNG.Height;

      BitmapPos := Point(0, 0);
    BitmapSize.cx := ClientWidth;
      BitmapSize.cy := ClientHeight;

      // Setup alpha blending parameters
    BlendFunction.BlendOp := AC_SRC_OVER;
      BlendFunction.BlendFlags := 0;
    BlendFunction.SourceConstantAlpha := 255;
      BlendFunction.AlphaFormat := AC_SRC_ALPHA;

    // ... and action!
      UpdateLayeredWindow(Handle, 0, nil, @BitmapSize, PNG.Canvas.Handle,
      @BitmapPos, 0, @BlendFunction, ULW_ALPHA);

      Show;

  finally
    PNG.Free;
  end;
end;

procedure TSplash2.PreMultiplyBitmap(Bitmap: TBitmap);
var
  Row, Col: integer;
  p: PRGBQuad;
  PreMult: array[byte, byte] of byte;
begin
  // precalculate all possible values of a*b
  for Row := 0 to 255 do
    for Col := Row to 255 do
    begin
      PreMult[Row, Col] := Row*Col div 255;
      if (Row <> Col) then
        PreMult[Col, Row] := PreMult[Row, Col]; // a*b = b*a
    end;

  for Row := 0 to Bitmap.Height-1 do
  begin
    Col := Bitmap.Width;
    p := Bitmap.ScanLine[Row];
    while (Col > 0) do
    begin
      p.rgbBlue := PreMult[p.rgbReserved, p.rgbBlue];
      p.rgbGreen := PreMult[p.rgbReserved, p.rgbGreen];
      p.rgbRed := PreMult[p.rgbReserved, p.rgbRed];
      inc(p);
      dec(Col);
    end;
  end;
end;

end.
于 2009-08-04T19:25:16.847 回答