2012-04-17 74 views
5

我使用本文的代码http://melander.dk/articles/alphasplash/在窗体中显示32位位图,但是当我尝试使用纯色位图而不是图像时WM_NCHITTEST消息未收到并且我无法移动表格。如果我使用32位图像,代码工作得很好。我在这里错过了什么?WM_NCHITTEST不工作在WS_EX_LAYERED形式

这是代码

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. 

回答

5

尝试用:

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

因为你的位图数据不具有源阿尔法。 TBitmap的AlphaFormat默认为afIgnored。 'AC_SRC_ALPHA'仅用于颜色值预乘alpha的图像。您从磁盘加载的图像可能具有适当的Alpha通道。

我不能真正猜到'WM_NC_HITTEST'与什么关系,但错误的输入会产生错误的结果:)。

+0

+1,不明白它的意思,但我相信你知道:-)我只是凭直觉解决了这个问题。 – TLama 2012-04-18 01:43:41

+0

非常感谢。 – Salvador 2012-04-18 01:44:26

+1

@TLama - 别那么肯定! 我投你的答案,因为我认为你也一样。真的,我不是专家,但它清楚地记录在'混合功能'中。我的观点是,你的回答也是正确的。唯一的问题是,它生成的图像具有任意的alpha值,但在一个单色位图中没有问题。 – 2012-04-18 01:46:58