How to create child layered alpha-transparent window?

落爺英雄遲暮 提交于 2020-01-02 09:13:44

问题


I am trying to create transparent child window.

procedure TForm1.BtnGoClick(Sender: TObject);
var
  bmp:TBitmap;
  BitmapPos: TPoint;
  BitmapSize: TSIZE;
  BlendFunction: _BLENDFUNCTION;
  exStyle: Cardinal;
begin
  bmp := TBitmap.Create;
  bmp.LoadFromFile('my32bitbitmap.bmp');
  exStyle := GetWindowLongA(Form2.Handle, GWL_EXSTYLE);
  if (exStyle and WS_EX_LAYERED = 0) then
    SetWindowLong(Form2.Handle, GWL_EXSTYLE, exStyle or WS_EX_LAYERED);
  BitmapPos := Point(0, 0);
  BitmapSize.cx := bmp.Width;
  BitmapSize.cy := bmp.Height;
  BlendFunction.BlendOp := AC_SRC_OVER;
  BlendFunction.BlendFlags := 0;
  BlendFunction.SourceConstantAlpha := 200;
  BlendFunction.AlphaFormat := AC_SRC_ALPHA;
  UpdateLayeredWindow(Form2.Handle, 0, nil, @BitmapSize, bmp.Canvas.Handle, @BitmapPos, 0, @BlendFunction, ULW_ALPHA);

  Windows.SetParent(Form2.Handle, Form1.Handle);
  bmp.Free;      
end;

It almost works: Form2 become nice transparent window inside Form1. But it looks like Form2 does not move with Form1. When i move Form1, Form2-Window moves, but on screen i see it when it was. When Form1 is moved i cant click on Form2, clicks goes through, so i know window was moved.

So question is how to make child transparent window without these features? (just normal window that moves with it's parrent)


回答1:


You need to call UpdateLayeredWindow after each move or resize of your Form2. Or you can replace it with TCustomTransparentControl descendant.



来源:https://stackoverflow.com/questions/7750224/how-to-create-child-layered-alpha-transparent-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!