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.
+1,不明白它的意思,但我相信你知道:-)我只是憑直覺解決了這個問題。 – TLama 2012-04-18 01:43:41
非常感謝。 – Salvador 2012-04-18 01:44:26
@TLama - 別那麼肯定!我投你的答案,因爲我認爲你也一樣。真的,我不是專家,但它清楚地記錄在'混合功能'中。我的觀點是,你的回答也是正確的。唯一的問題是,它生成的圖像具有任意的alpha值,但在一個單色位圖中沒有問題。 –
2012-04-18 01:46:58