Transparent SWT Canvas

一世执手 提交于 2019-12-24 02:44:07

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/5083837/transparent-swt-canvas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!