Recent

Author Topic: how to put controls in transparent window  (Read 1280 times)

Ericktux

  • Full Member
  • ***
  • Posts: 196
how to put controls in transparent window
« on: March 07, 2019, 08:36:02 am »
Hello friends, first of all thanks for your support.
I just found an example transparent window in the forum.
here the link:
http://forum.lazarus.freepascal.org/index.php?topic=35561.0

the example is "CustomShapedForm.zip" from the friend getmem, thank you very much for sharing, it works perfect.  :)

my question is how to put controls (labels, edit, panels, buttons, etc.) on top of the image?   :-\ :-\

when I try everything disappears and only shows the transparent image.

Thank you for your answers.


this is the original code

Code: Pascal  [Select]
  1. unit uMain;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Windows, JwaWindows, Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   ExtCtrls, Menus, ComCtrls;
  10.  
  11. type
  12.  
  13.   { TfMain }
  14.  
  15.   TfMain = class(TForm)
  16.     im: TImage;
  17.     miClose: TMenuItem;
  18.     pm: TPopupMenu;
  19.     procedure FormShow(Sender: TObject);
  20.     procedure imMouseDown(Sender: TObject; {%H-}Button: TMouseButton;
  21.       {%H-}Shift: TShiftState; X, Y: Integer);
  22.     procedure imMouseMove(Sender: TObject; {%H-}Shift: TShiftState; X, Y: Integer);
  23.     procedure imMouseUp(Sender: TObject; {%H-}Button: TMouseButton;
  24.       {%H-}Shift: TShiftState; {%H-}X, {%H-}Y: Integer);
  25.     procedure miCloseClick(Sender: TObject);
  26.   private
  27.     DX, DY: Integer;
  28.     MoveForm: Boolean;
  29.     procedure MakeTransparentWindow;
  30.     { private declarations }
  31.   public
  32.     { public declarations }
  33.   end;
  34.  
  35. var
  36.   fMain: TfMain;
  37.  
  38. implementation
  39.  
  40. {$R *.lfm}
  41.  
  42. { TfMain }
  43.  
  44. procedure TfMain.FormShow(Sender: TObject);
  45. begin
  46.   MakeTransparentWindow;
  47. end;
  48.  
  49. procedure TfMain.MakeTransparentWindow;
  50. var
  51.   BlendFunction: TBlendFunction;
  52.   Size: TSize;
  53.   P: TPoint;
  54.   ExStyle: DWORD;
  55. begin
  56.   ExStyle := GetWindowLongA(Handle, GWL_EXSTYLE);
  57.   if (ExStyle and WS_EX_LAYERED = 0) then
  58.     SetWindowLong(Handle, GWL_EXSTYLE, ExStyle or WS_EX_LAYERED);
  59.   ClientWidth := im.picture.Bitmap.Width;
  60.   ClientHeight := im.picture.Bitmap.Height;
  61.   P.x := 0;
  62.   P.y := 0;
  63.   Size.cx := im.picture.Bitmap.Width;
  64.   Size.cy := im.picture.Bitmap.Height;
  65.   BlendFunction.BlendOp := AC_SRC_OVER;
  66.   BlendFunction.BlendFlags := 0;
  67.   BlendFunction.SourceConstantAlpha := 255;
  68.   BlendFunction.AlphaFormat := AC_SRC_ALPHA;
  69.   UpdateLayeredWindow(Handle, 0, nil, @Size, im.picture.Bitmap.Canvas.Handle, @P, 0, @BlendFunction, ULW_ALPHA);
  70. end;
  71.  
  72. procedure TfMain.imMouseDown(Sender: TObject; Button: TMouseButton;
  73.   Shift: TShiftState; X, Y: Integer);
  74. begin
  75.   DX := X;
  76.   DY := Y;
  77.   MoveForm := True;
  78. end;
  79.  
  80. procedure TfMain.imMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  81. begin
  82.  if not MoveForm then
  83.    Exit;
  84.  if (ssLeft in Shift) then
  85.  begin
  86.    fMain.Left := fMain.Left + (X - DX);
  87.    fMain.Top := fMain.Top + (Y - DY);
  88.   end;
  89. end;
  90.  
  91. procedure TfMain.imMouseUp(Sender: TObject; Button: TMouseButton;
  92.   Shift: TShiftState; X, Y: Integer);
  93. begin
  94.   MoveForm := False;
  95. end;
  96.  
  97. procedure TfMain.miCloseClick(Sender: TObject);
  98. begin
  99.   Close;
  100. end;
  101.  
  102. end.

Ericktux

  • Full Member
  • ***
  • Posts: 196
Re: how to put controls in transparent window
« Reply #1 on: March 09, 2019, 05:59:19 am »
please if someone can help me  :(

GetMem

  • Hero Member
  • *****
  • Posts: 3459
Re: how to put controls in transparent window
« Reply #2 on: March 09, 2019, 06:13:22 am »
Hard to achieve what you're requested in a cross-platform way. It's doable on windows though.

Ericktux

  • Full Member
  • ***
  • Posts: 196
Re: how to put controls in transparent window
« Reply #3 on: March 09, 2019, 06:23:06 am »
hello my friend getmem, thanks for your comment, I just need for windows.

GetMem

  • Hero Member
  • *****
  • Posts: 3459
Re: how to put controls in transparent window
« Reply #4 on: March 09, 2019, 06:25:35 am »
Here you go...

PS: I must warn you the attachment contains graphic violence.  :D

Ericktux

  • Full Member
  • ***
  • Posts: 196
Re: how to put controls in transparent window
« Reply #5 on: March 09, 2019, 06:41:33 am »
jajaajaja  :D
thank you very much friend, it works perfect.
a query, you should always have the background gray ?

GetMem

  • Hero Member
  • *****
  • Posts: 3459
Re: how to put controls in transparent window
« Reply #6 on: March 09, 2019, 06:48:47 am »
Code: Pascal  [Select]
  1. CreateRegion(const AIm: TImage; const AMaskColor: TColor; out ARegion: HRGN);
AMaskColor must match the backgroundcolor. It can be gray or any other color.

Ericktux

  • Full Member
  • ***
  • Posts: 196
Re: how to put controls in transparent window
« Reply #7 on: March 09, 2019, 11:06:39 pm »
ok my friend, thank you very much for your help.  :) :)