5

我正在使用这篇文章http://melander.dk/articles/alphasplash/的代码在表单中显示 32 位位图,但是当我尝试使用纯色位图而不是图像时,WM_NCHITTEST 消息不是收到,我无法移动表格。如果我使用 32 位图图像,代码就可以正常工作。我在这里缺少什么?

这是代码

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  protected
    { Private declarations }
    procedure WMNCHitTest(var Message: TWMNCHitTest); message WM_NCHITTEST;
  public
    { Public declarations }
  end;

var
  Form1 : TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  BlendFunction: TBlendFunction;
  BitmapPos: TPoint;
  BitmapSize: TSize;
  exStyle: DWORD;
  Bitmap: TBitmap;
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);

  Bitmap := TBitmap.Create;
  try
   //Bitmap.LoadFromFile('splash.bmp'); //if I use a image the code works fine

    Bitmap.PixelFormat := pf32bit;
    Bitmap.SetSize(Width, Height);    
    Bitmap.Canvas.Brush.Color:=clRed;
    Bitmap.Canvas.FillRect(Rect(0,0, Bitmap.Width, Bitmap.Height));

    // Position bitmap on form
    BitmapPos := Point(0, 0);
    BitmapSize.cx := Bitmap.Width;
    BitmapSize.cy := Bitmap.Height;


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

    UpdateLayeredWindow(Handle, 0, nil, @BitmapSize, Bitmap.Canvas.Handle,
      @BitmapPos, 0, @BlendFunction, ULW_ALPHA);
    Show;
  finally
    Bitmap.Free;
  end;
end;

procedure TForm1.WMNCHitTest(var Message: TWMNCHitTest);
begin
  Message.Result := HTCAPTION;
end;

end.
4

1 回答 1

5

尝试:

BlendFunction.SourceConstantAlpha := 150; // 255 is fully opaque.
BlendFunction.AlphaFormat := 0;

因为您的位图数据没有源 alpha。AlphaFormat对于 TBitmap 是默认的afIgnored。'AC_SRC_ALPHA' 仅用于颜色值预乘 alpha 的图像。您从磁盘加载的图像可能具有正确的 Alpha 通道。

我真的无法猜测与“WM_NC_HITTEST”的关系是什么,但错误的输入会产生错误的结果:)。

于 2012-04-18T01:36:31.217 回答