[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[news.eclipse.platform.swt] Re: Control with transparent background

Guy,

This question gets asked a lot. I'm afraid SWT doesn't support transparent
controls. Many people new to SWT try to set the SWT.NO_BACKGROUND style 
on their Composite, but this just tells SWT not to paint the background 
of the Composite with the background color. Instead what you see (at least
on windows) is garbage from whatever's on the desktop behing your 
application. 

What do you want to see through your control? If you want to see other 
controls then this can't be done. However, when people say they
want a transparent control they often mean that they want to see the 
background of the containing Composite through the control. This can
be faked quite effectively, though it's a little tricky. 

I've implemented a class called PaintedComposite fakes transparency. 
You can set the background of a PaintedComposite to be an arbitrary 
Painter object (I've implemented TexturePainter and GradientPainter). 
If you don't set a painter then the control is "transparent". The trick 
is that a PaintedComposite that does have a Painter paints itself onto any 
children (arbitrarily nested) that are "transparent". The effect is 
that you see through the transparent controls to the painted background.
If you implemented your custom control by extending this class then 
it would be "transparent" when placed within a PaintedComposite. As an
example I've implemented a "transparent" label control.

If I get time I'll put this code up on my site in the next couple of 
days. 

Cheers,

Alun


Guy Moreillon wrote:

> Howdy SWT gurus,

>   can a kind soul tell me how I can write a custom control with a
> transparent background? When my PaintListener gets called, the background
> has already been painted...

> Thanks,

> -- Guy