Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WM_NCHITTEST not working in WS_EX_LAYERED form

Tags:

delphi

I'm using the code of this article http://melander.dk/articles/alphasplash/ to display a 32 bit bitmap in a form, but when i try to use a solid color bitmap instead of a image the WM_NCHITTEST message is not received and i cann't move the form. If I use 32 bitmap image the code work just fine. What i'm missing here?

This is the code

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.
like image 817
Salvador Avatar asked Apr 17 '12 23:04

Salvador


1 Answers

Try with:

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

Because your bitmap data has no source alpha. AlphaFormat for a TBitmap is by default afIgnored. 'AC_SRC_ALPHA' is only used with images having color values premultiplied with alpha. The images you are loading from the disk probably has proper alpha channel.

I can't really guess what's the relation with 'WM_NC_HITTEST' but wrong inputs yields wrong results :).

like image 180
Sertac Akyuz Avatar answered Oct 12 '22 07:10

Sertac Akyuz