Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2012-09

Transparent Container/Panel in LCL?

13 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

Hi,

How do I create a transparent container/panel in LCL? I tried the following code, and as you can see, csOpaque is not defined in the ControlStyle property.

-----------------------------------
type
   TTransparentContainer = class(TCustomControl)
   public
     constructor Create(AOwner: TComponent); override;
   end;
constructor TTransparentContainer.Create(AOwner: TComponent);
begin
   inherited Create(AOwner);
   // Do not include [csOpaque] because we want a transparent widget.
   ControlStyle := [csAcceptsControls, csNoFocus];
end;
-----------------------------------

Please see attached the grey box in the middle of the form (which contains a TLabel and TButton) is not "transparent" container. Not very transparent because the form's tiled background is not showing through.

Maybe I misunderstood the [csOpaque] option, though I though it seems self explanatory.

I'm using Lazarus v1.0 GTK2 under Linux with FPC 2.6.0 on 64-bit OpenSUSE 12.1

Any tips?

Regards,
- Graeme -
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.png
Type: image/png
Size: 5259 bytes
Desc: not available
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20120903/8f438bcc/attachment.png>

Reply by email

Graeme Geldenhuys
to Graeme Geldenhuys
Graeme Geldenhuys wrote
How do I create a transparent container/panel in LCL? I tried the following code, and as you can see, csOpaque is not defined in the ControlStyle property.

Don't worry, searching the mailing list archives I found my answer back in December 2009. It's not possible in LCL. csOpaque has nothing to do with "visual behaviour" but simply with "mouse click / clipping behaviour".

Sorry for the noise.

Graeme.

Reply by email

Felipe Monteiro de Carvalho
to Graeme Geldenhuys
Transparency is supported, just use TWinControl.SetShape(Region) or
SetShape(Bitmap);

It will be tricky if you want to have a very irregular region of transparency, such as for having text. In this case SetShape(Bitmap) is the best option.

Felipe Monteiro de Carvalho

Reply by email

Graeme Geldenhuys
to Felipe Monteiro de Carvalho
Felipe Monteiro de Carvalho wrote
Transparency is supported, just use TWinControl.SetShape(Region) or SetShape(Bitmap);

It seems some people have different definitions of "transparency". Will that make the TTransparentContainer clear like glass? Will I see through it and see the main form's tiled canvas?

Anyway, I'm not 100% sure how to your the TRegion, so tried the following. Still no luck. The TTransparentContainer doesn't look "visually" transparent / see-thru.

procedure TForm1.FormCreate(Sender: TObject);
var
   r: TRegion;
begin
   Width := 400;
   r := TRegion.Create;
   FMyContainer := TTransparentContainer.Create(self);
   FMyContainer.Parent := self;
   with FMyContainer do
   begin
     Left := 50;
     Top := 50;
     Width := 300;
     Height := 200;
     r.AddRectangle(0, 0, FMyContainer.Width, FMyContainer.Height);
     SetShape(r);
   end;
   ...
end;

See attached image for the results.

Regards,
- Graeme -

-------------- next part --------------
A non-text attachment was scrubbed...
Name: test2.png
Type: image/png
Size: 5144 bytes
Desc: not available
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20120903/43cac8af/attachment.png>

Reply by email

Felipe Monteiro de Carvalho
to Graeme Geldenhuys

The code works as expected. Translucency is not supported, only full transparent or full opaque. But you can set irregular regions as 100% transparent.

The region that you set will be 100% opaque. Anything outside that region will be 100% transparent.

If you set a region that includes the entire control, by definition nothing will change. You need to include in your region what you want to include. In this case the text+button. The down side is that TRegion doesn't support text shapes, only geometric figures (ellipses, rectangles and any combination of them using various operators).

You can work around this by using SetShape(bitmap) instead, but I don't think it will work in all platforms.

Felipe Monteiro de Carvalho

Reply by email

Graeme Geldenhuys
to Felipe Monteiro de Carvalho
Felipe Monteiro de Carvalho wrote
The code works as expected. Translucency is not supported, only full transparent or full opaque.

I want "full transparent" or "100% see-thru". Like I said... there seems to be a 101 definitions of "transparent" in this mailing list. I got the same confusing answers back in 2009.

If you set a region that includes the entire control, by definition nothing will change. You need to include in your region what you want to include. In this case the text+button.

OK, so as a sample, I included a 5x5 region to be opaque, and by your definition, everything else should be "full transparent/see-thru".

So I did this..

   r := TRegion.Create;
   FMyContainer := TTransparentContainer.Create(self);
   FMyContainer.Parent := self;
   with FMyContainer do
   begin
     Left := 50;
     Top := 50;
     Width := 300;
     Height := 200;
     r.AddRectangle(0, 0, 10, 10);
     SetShape(r);
   end;

Now as you can see the container is much larger than 10x10, so I should see a grey 10x10 block, and the rest should be the background logo of the main form.

Yet, nothing changed. The output at runtime looks exactly like the image I attached in my previous message. The whole container is painted grey, even though I this time defined a smaller region.

It seems my initial assumption is still correct. Transparency in LCL-GTK2 at least is not supported. I say LCL-GTK2, because that is currently the only widgetset I have tested.

If you still disagree with my assumption, could you possibly take my test code (attached) and see if you can get a working fully 100% transparent (looking like clear glass) see-thru container? Something where the main forms tiled background will show through. Ignoring the Label and Button for now. I would very much appreciate your help with this. I simply can't get it to work.

Regards,
- Graeme -

-------------- next part --------------
A non-text attachment was scrubbed...
Name: tile_background.tar.gz
Type: application/x-gzip
Size: 4438 bytes
Desc: not available
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20120903/1624236f/attachment-0001.bin>

Reply by email

Felipe Monteiro de Carvalho
to Graeme Geldenhuys

The code works in Windows and in Qt in Mac OS X.

Gtk2 has a problem that it does not allow setting the region of a control before the expose event. So basically setting the region in the event OnCreate will not do anything. I had this problem in my application and I think that the only thing that worked for me was using a timer with interval 1. It sucks, but well, that's Gtk2 =)

Felipe Monteiro de Carvalho

Reply by email

Zaher Dirkey
to Graeme Geldenhuys: It seems some people have different definitions of "transparency". Will that make the…
Graeme Geldenhuys wrote 41 lines
On 03/09/12 15:19, Felipe Monteiro de Carvalho wrote: > Transparency is supported, just use TWinControl.SetShape(Region) or > SetShape(Bitmap); > It seems some people have different definitions of "transparency". Will that make the TTransparentContainer clear like glass? Will I see through it and see the main form's tiled canvas? Anyway, I'm not 100% sure how to your the TRegion, so tried the following. Still no luck. The TTransparentContainer doesn't look "visually" transparent / see-thru. procedure TForm1.FormCreate(Sender: TObject); var r: TRegion; begin Width := 400; r := TRegion.Create; FMyContainer := TTransparentContainer.Create(**self); FMyContainer.Parent := self; with FMyContainer do begin Left := 50; Top := 50; Width := 300; Height := 200; r.AddRectangle(0, 0, FMyContainer.Width, FMyContainer.Height); SetShape(r); end; ... end; See attached image for the results.
That if you want to use a real "window" in the OS as transparent;

The idea i need a container transparent to see behind it, and ignore any mouse actions, in another way, this container only can container a child controls, but do nothing, so it is not a real window, i mean it have no handle.

In fact, it is TControl bypass any mouse/keys action bypass any drawing to the window handler, it is easy to make it, but not easy, we cant put control inside it, and if we use TCustomControl it become a real window and have handle from the OS.

Uhm any one understand me :)

Best Regards
Zaher Dirkey
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20120903/2bb2b30f/attachment.html>

Reply by email

zeljko
to Graeme Geldenhuys: It seems some people have different definitions of "transparency". Will that make the…
Graeme Geldenhuys wrote 37 lines
On 03/09/12 15:19, Felipe Monteiro de Carvalho wrote: > Transparency is supported, just use TWinControl.SetShape(Region) or > SetShape(Bitmap); It seems some people have different definitions of "transparency". Will that make the TTransparentContainer clear like glass? Will I see through it and see the main form's tiled canvas? Anyway, I'm not 100% sure how to your the TRegion, so tried the following. Still no luck. The TTransparentContainer doesn't look "visually" transparent / see-thru. procedure TForm1.FormCreate(Sender: TObject); var r: TRegion; begin Width := 400; r := TRegion.Create; FMyContainer := TTransparentContainer.Create(self); FMyContainer.Parent := self; with FMyContainer do begin Left := 50; Top := 50; Width := 300; Height := 200; r.AddRectangle(0, 0, FMyContainer.Width, FMyContainer.Height); SetShape(r); end; ... end; See attached image for the results.

I don't know for gtk2 atm, but eg. under Qt QPainter have opacity property which does thing you want. Maybe it's possible under gtk2 (by using cairo) to do same thing.

zeljko

Reply by email

zeljko
to Felipe Monteiro de Carvalho: The code works in Windows and in Qt in Mac OS X. Gtk2 has a problem that it does not…
Felipe Monteiro de Carvalho wrote 5 lines
The code works in Windows and in Qt in Mac OS X. Gtk2 has a problem that it does not allow setting the region of a control before the expose event. So basically setting the region in the event OnCreate will not do anything. I had this problem in my

OnCreate() will do exactly nothing on Qt too. If handle does not exist , setting any shape won't work (just looked into implementation).

zeljko

Reply by email

Felipe Monteiro de Carvalho
to zeljko
zeljko wrote
OnCreate() will do exactly nothing on Qt too. If handle does not exist , setting any shape won't work (just looked into implementation).

Aha, might be, but it doesn't matter. You can use OnShow then.

In Gtk2 OnShow also doesn't work.

Felipe Monteiro de Carvalho

Reply by email

zeljko
to Felipe Monteiro de Carvalho
Felipe Monteiro de Carvalho wrote 7 lines
On Tue, Sep 4, 2012 at 2:53 PM, zeljko wrote: > OnCreate() will do exactly nothing on Qt too. If handle does not exist , > setting any shape won't work (just looked into implementation). Aha, might be, but it doesn't matter. You can use OnShow then. In Gtk2 OnShow also doesn't work.

Depends on your application. Here it works for shaped TButton (shape is setted up in OnShow()).

zeljko

Reply by email