3

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)

Astronavigator
  • 2,021
  • 2
  • 24
  • 45
  • Delphi forms have an `AlphaBlendValue` property that you can set. This will make a form transparent. Why muck about with the API calls, see: http://docwiki.embarcadero.com/VCL/XE2/en/Forms.TForm.AlphaBlendValue – Johan Oct 13 '11 at 11:35
  • @Johan: property AlphaBlendValue can only make my window constantly transparent. Not enough for me... – Astronavigator Oct 13 '11 at 11:44
  • Could you add a screenshot of what you see when you move Form1, please? I don't fully understand what behaviour you're getting. Is Form2 parented to Form1? When you move Form1, does it render ok but foes Form2 appear to stay where it was, or do you see a "trail", or...? – David Oct 13 '11 at 13:01
  • 1
    Note you [cannot do this with true child windows.](http://msdn.microsoft.com/en-us/library/windows/desktop/ff700543.aspx) Why a second form - could you achieve the same thing with a 32-bit image on the first form? – David Oct 13 '11 at 13:06
  • @DavidM there are working example of code in question. You could try and see it by your self. I dont think screen short could help here. You must move window to see what happens. And there are no "trails" or whatever. – Astronavigator Oct 24 '11 at 10:26

1 Answers1

1

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

Torbins
  • 2,111
  • 14
  • 16
  • What exactly have you tried? TCustomTransparentControl or UpdateLayeredWindow? – Torbins Oct 24 '11 at 18:03
  • Tried UpdateLayeredWindow. Is there any example how to use TCustomTransparentControl and UpdateLayeredWindow for child windows? Did you try these solutions for child windows? – Astronavigator Nov 08 '11 at 13:56