开发者

Transparent SWT Canvas

I would like to make the background of an SWT Canvas transparent, so that other widgets 开发者_如何转开发can be seen behind it.

I have tried setting alpha to 0 and filling the canvas with a rectangle and also using the option SWT.NO_BACKGROUND to no avail.


AFAIK, it is not possible with SWT to place widgets on top of each other in this way (in a cross-platform manner). Transparent backgrounds won't allow other widgets to be seen. Also any clicks on the transparent pixels won't be delegated to the widgets behind.

But you can place shells on top of each other. And you can create transparent shells or shells with irregular bounds. See the Shell examples in the Snippets. This is not the same, but depending on what you want to achieve, it might suffice.


There is a way to do it on Linux (GTK). (However, it doesn't work on Windows. (Don't know about MacOSX.))

Just setBackground to RGBA with alpha-channel. E.g:

Canvas canvas = new Canvas(parent, SWT.NONE);
canvas.setBackground(new Color(canvas.getDisplay(), 100,100,100,100));
//Note the last argument (I'm setting not only R, G, B, but also A).

You don't even need SWT.NO_BACKGROUND, SWT.TRANSPARENT or other style flags.

P.S.: You shouldn't create colors in this way. Colors are to be disposed using dispose. Assign newly created color to variable to be able to reuse it and free it. I created color "inline" just for simplicity of example.

Additional note: don't set alpha-channel to 0. Although lesser value should mean more transparent, in reality it doesn't work with value 0, only with 1 and above. Probably, it considers 0 as unset or etc.


On Windows I had success with following transparent control:

final Composite composite = new Composite(parent, SWT.TRANSPARENT);
composite.addListener(SWT.Paint, new Listener() {
    @Override
    public void handleEvent(Event event) {
        event.gc.drawString("hello world", 0, 0, true);
    }
});

Unfortunately, this does not work on OS X or Linux.


You can use:

shell.setAlpha(180);

The value can be set ranging from 0 to 255. 0 means transparent and 255 means solid colored background


Try:

 new Canvas(parent, SWT.TRANSPARENT);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜