* * *

Author Topic: transparent TPanel?  (Read 762 times)

majid.ebru

  • Full Member
  • ***
  • Posts: 130
transparent TPanel?
« on: May 05, 2017, 12:15:41 pm »
Hi
.

How to make transparent TPanel?
or
how to set alphablend to panel?

RAW

  • Full Member
  • ***
  • Posts: 189
Re: transparent TPanel?
« Reply #1 on: May 05, 2017, 12:35:42 pm »
The easiest way would probably be to not use a TPanel...  :P

I saw someone who did that with Delphi but it was not possible to scroll something inside that panel.
Good Luck !


EDIT:
Another easy way would be to use a TImage inside that TPanel with a part of the background image... (not the best though).
« Last Edit: May 05, 2017, 12:55:19 pm by RAW »
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.6.4 FPC 3.0.2

majid.ebru

  • Full Member
  • ***
  • Posts: 130
Re: transparent TPanel?
« Reply #2 on: May 05, 2017, 06:49:10 pm »

Another easy way would be to use a TImage inside that TPanel with a part of the background image... (not the best though).
hi
i need container element and Timage is not?

kupferstecher

  • Jr. Member
  • **
  • Posts: 82
Re: transparent TPanel?
« Reply #3 on: May 05, 2017, 09:38:04 pm »
Some time before I also searched for it, and didn't find any solution for Lazarus (doesn't work in the way it does for Delphi).
In the meantime I stumbled across the below link, containing code for a transparent panel. No idea if/how it works though.

There is a remark in the description, it roughly says: "The transparent panel is realized through capturing the picture of the parent, thus there may not be any other component under the panel, otherwise it will be hidden."

http://lc51746.blog.163.com/blog/static/123509108200982802026564/

If you try it please let us know if it works.
Regards

majid.ebru

  • Full Member
  • ***
  • Posts: 130
Re: transparent TPanel?
« Reply #4 on: May 05, 2017, 11:32:11 pm »
Quote
Lazarus中没有透明Panel组件,就自己写了一个,经过一番折腾,总算是设计期和运行期都可以正确的透明了。

注意,透明Panel是通过抓取Parent图片的方式实现的,因此Panel下不能放置别的组件,否则会被背景图挡住。

我写了一个具有背景图片功能的Panel组件,可以结合透明Panel一起使用,效果非常不错,详情参考《Lazarus 的具有背景图片的Panel组件》

下面是组件源码

Google translate :
Quote
Lazarus no transparent Panel components, write their own one, after some toss, finally the design period and the operation period can be correct and transparent.

Note that the transparent Panel is achieved by capturing the Parent image, so the Panel can not place other components, otherwise it will be blocked by the background.

I have written a Panel component with background image functionality that can be used in conjunction with a transparent Panel. The results are very good. For details, refer to "Lazarus Panel Components with Background Pictures"

The following is the component source
Code: Pascal  [Select]
  1. unit uTransparentPanel;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.  
  9.   Classes, SysUtils, LMessages, Forms, Controls, Graphics, LCLType, types, Windows;
  10.  
  11. type
  12.  
  13.   { TTransparentPanel }
  14.  
  15.   TTransparentPanel = class(TCustomControl)
  16.  
  17.   private
  18.  
  19.     fBuffer: Graphics.TBitmap;
  20.  
  21.     fBufferChanged : boolean;
  22.  
  23.     procedure SetColor(Value: TColor); override;
  24.  
  25.   protected
  26.  
  27.     function getBuffer : Graphics.TBitmap; virtual;
  28.  
  29.     procedure WMWindowPosChanged(var Message: TLMWindowPosChanged); message LM_WINDOWPOSCHANGED;
  30.  
  31.     procedure WMEraseBkgnd(var Message: TLMEraseBkgnd); message LM_ERASEBKGND;
  32.  
  33.     procedure CreateParams(var Params: TCreateParams);
  34.  
  35.     procedure Paint; override;
  36.  
  37.     procedure Resize; override;
  38.  
  39.     procedure redrawBackgroundBuffer(var buffer : Graphics.TBitmap); virtual;
  40.  
  41.     function getBufferChanged : boolean; virtual;
  42.  
  43.     procedure setBufferChanged(val : boolean); virtual;
  44.  
  45.     procedure Invalidate; override;
  46.  
  47.   public
  48.  
  49.     constructor Create(AOwner : TComponent); override;
  50.  
  51.     destructor Destroy; override;
  52.  
  53.   published
  54.  
  55.     property OnPaint;
  56.  
  57.     property Color;
  58.  
  59.     property Align;
  60.  
  61.     property Height;
  62.  
  63.     property Cursor;
  64.  
  65.     property HelpContext;
  66.  
  67.     property HelpType;
  68.  
  69.     property Hint;
  70.  
  71.     property Left;
  72.  
  73.     property Name;
  74.  
  75.     property Tag;
  76.  
  77.     property Top;
  78.  
  79.     property Width;
  80.  
  81.     property Anchors;
  82.  
  83.     property Constraints;
  84.  
  85.   end;
  86.  
  87. procedure Register;
  88.  
  89. implementation
  90.  
  91. procedure Register;
  92.  
  93. begin
  94.  
  95.   RegisterComponents('aess',[TTransparentPanel]);
  96.  
  97. end;
  98.  
  99. { TTransparentPanel }
  100.  
  101. procedure TTransparentPanel.SetColor(Value: TColor);
  102.  
  103. begin
  104.  
  105.   inherited SetColor(Value);
  106.  
  107.   RecreateWnd(Self);
  108.  
  109. end;
  110.  
  111. function TTransparentPanel.getBuffer: Graphics.TBitmap;
  112.  
  113. begin
  114.  
  115.   Result := fBuffer;
  116.  
  117. end;
  118.  
  119. procedure TTransparentPanel.WMWindowPosChanged(var Message: TLMWindowPosChanged);
  120.  
  121. begin
  122.  
  123.   setBufferChanged(true);
  124.  
  125.   Invalidate;
  126.  
  127.   inherited;
  128.  
  129. end;
  130.  
  131. procedure TTransparentPanel.WMEraseBkgnd(var Message: TLMEraseBkgnd);
  132.  
  133. begin
  134.  
  135.   Message.Result := 1;
  136.  
  137. end;
  138.  
  139. procedure TTransparentPanel.CreateParams(var Params: TCreateParams);
  140.  
  141. begin
  142.  
  143.   inherited CreateParams(Params);
  144.  
  145.   params.exstyle := params.exstyle or WS_EX_TRANSPARENT;
  146.  
  147. end;
  148.  
  149. procedure TTransparentPanel.Paint;
  150.  
  151. begin
  152.  
  153.   if getBufferChanged then
  154.  
  155.   begin
  156.  
  157.     redrawBackgroundBuffer(fBuffer);
  158.  
  159.     setBufferChanged(false);
  160.  
  161.   end;
  162.  
  163.   Canvas.Draw(0, 0, fBuffer);
  164.  
  165.   if assigned(OnPaint) then
  166.  
  167.     OnPaint(Self);
  168.  
  169. end;
  170.  
  171. procedure TTransparentPanel.Resize;
  172.  
  173. begin
  174.  
  175.   setBufferChanged(true);
  176.  
  177.   Invalidate;
  178.  
  179.   inherited Resize;
  180.  
  181. end;
  182.  
  183. procedure TTransparentPanel.redrawBackgroundBuffer(var buffer : Graphics.TBitmap);
  184.  
  185. var
  186.  
  187.   rDest : TRect;
  188.  
  189.   bmp : Graphics.TBitmap;
  190.  
  191. begin
  192.  
  193.   bmp := Graphics.TBitmap.Create;
  194.  
  195.   try
  196.  
  197.     bmp.PixelFormat := pf24bit;
  198.  
  199.     bmp.Width := Parent.Width;
  200.  
  201.     bmp.Height := Parent.Height;
  202.  
  203.     bmp.TransparentColor:= Self.Color;
  204.  
  205.     bmp.Canvas.brush.Color:=TCustomForm(parent).Color;
  206.  
  207.     bmp.Canvas.FillRect(types.rect(0,0,bmp.width,bmp.height));
  208.  
  209.     SendMessage(parent.Handle, WM_PAINT, bmp.Canvas.handle, 0);
  210.  
  211.     Application.ProcessMessages;
  212.  
  213.     buffer.Width:= Self.Width;
  214.  
  215.     buffer.Height := Self.Height;
  216.  
  217.     rDest := types.Rect(0,0,Width, Height);
  218.  
  219.     buffer.Canvas.CopyRect(rDest, bmp.Canvas, BoundsRect);
  220.  
  221.   finally
  222.  
  223.     freeandnil(bmp);
  224.  
  225.   end;//fianlly
  226.  
  227. end;
  228.  
  229. function TTransparentPanel.getBufferChanged: boolean;
  230.  
  231. begin
  232.  
  233.   Result := fBufferChanged;
  234.  
  235. end;
  236.  
  237. procedure TTransparentPanel.setBufferChanged(val: boolean);
  238.  
  239. begin
  240.  
  241.   fBufferChanged := val;
  242.  
  243. end;
  244.  
  245. procedure TTransparentPanel.Invalidate;
  246.  
  247. begin
  248.  
  249.   if assigned(parent) and parent.HandleAllocated then
  250.  
  251.   begin
  252.  
  253.     InvalidateRect(parent.Handle, BoundsRect, true);
  254.  
  255.     inherited Invalidate;
  256.  
  257.   end
  258.  
  259.   else
  260.  
  261.     inherited Invalidate;
  262.  
  263. end;
  264.  
  265. constructor TTransparentPanel.Create(AOwner: TComponent);
  266.  
  267. begin
  268.  
  269.   inherited Create(AOwner);
  270.  
  271.   fBuffer := Graphics.TBitmap.Create;
  272.  
  273.   ControlStyle := [csAcceptsControls, csCaptureMouse, csClickEvents,
  274.  
  275.   csDoubleClicks, csReplicatable];
  276.  
  277.   Width := 200;
  278.  
  279.   Height := 150;
  280.  
  281.   ParentCtl3d := False;
  282.  
  283.   Ctl3D := False;
  284.  
  285.   ParentColor := False;
  286.  
  287.   fBufferChanged:= false;
  288.  
  289.   inherited Color := clWindow;
  290.  
  291. end;
  292.  
  293. destructor TTransparentPanel.Destroy;
  294.  
  295. begin
  296.  
  297.   fBuffer.Free;
  298.  
  299.   inherited Destroy;
  300.  
  301. end;
  302.  
  303. end.
  304.  

.
.
Thank you

but how can i use this ?!?!?

wp

  • Hero Member
  • *****
  • Posts: 3433
Re: transparent TPanel?
« Reply #5 on: May 06, 2017, 12:12:22 am »
In the attachment, there's a quick test in which the panel extends over the entire form which is painted with a gradient background; the panel also contains a button and a radiogroup.

I had to fix a couple of issues (compilation error due to properties Ctrl3D and ParentCtrl3D not existing in Lazarus, remove Windows unit to make the component cross-platform, add missing override directive to CreateParams).

The panel really is transparent but causes a lot of flicker when the form is resized.

If you can live with this you must add the unit to a package and install the package into the ide (http://wiki.lazarus.freepascal.org/How_To_Write_Lazarus_Component). After that the panel will appear in the component palette for usage like any other component.
Lazarus trunk / fpc 3.0.0 / Win32

jc99

  • Sr. Member
  • ****
  • Posts: 375
    • My private Site
Re: transparent TPanel?
« Reply #6 on: May 06, 2017, 12:22:21 am »
@majid.ebru
What do you try to do ?
Do your users have to work with Controls e.G: Buttons under the transparent panel ? -> try shaped controls
Do you only need it for a cool blending effect ?-> Try TImage or BGRA

The transparent panel is just a Transparent Panel, but alpha-blending might be possible (but only for the background).
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.2 - 1.6.4, 1.8rc1
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are

majid.ebru

  • Full Member
  • ***
  • Posts: 130
Re: transparent TPanel?
« Reply #7 on: May 06, 2017, 10:21:38 am »
thanks for answer.
.
in this topic i show what i need and Mr.jmpessoa solved it
.
i just have a beauty form and both of my program (OS : android and windows ) Like each other.
.
the TShape element is not container and i don't find any samlpe of BGRA aboute alpha transparent.
.
i just find this post.
.
this program sets Blur panel
« Last Edit: May 06, 2017, 10:26:04 am by majid.ebru »

kupferstecher

  • Jr. Member
  • **
  • Posts: 82
Re: transparent TPanel?
« Reply #8 on: May 06, 2017, 07:17:50 pm »
The panel really is transparent but causes a lot of flicker when the form is resized.
Now I also made a try. Same here, a lot of flickering, even when entering the form with the mouse. Anyone any idea how to improve that?

Regards

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus