SWT中的自定义小部件形状

cur*_*rsa 2 java user-interface swt

我正在尝试研究如何使SWT小部件(例如Label,Frame)成为矩形以外的形状.

我使用setRegion()方法制作了一个自定义形状的主窗口.现在我希望窗口中的小部件遵循相同的形状.我已尝试在小部件本身上使用setRegion()方法(它们继承它)但没有任何反应.

如何使SWT窗口小部件具有自定义形状?

cur*_*rsa 6

我成功地解决了这个问题.似乎我的困难来自于对如何定义自定义区域的误解.setBounds()方法定义自定义区域的坐标系.我假设区域和setBounds使用相同的坐标系,结果根本没有显示小部件.

图片显示效果.左侧的绿色标签与主窗口的曲线相同,而右下角的灰色框架则不是.

图像显示在预览中但不是发布的答案,请尝试:http://img87.imageshack.us/my.php? image = curvecroppedwf8.png

代码片段执行此操作:

Display display = Display.getDefault();
Shell shell new Shell(display, SWT.NO_TRIM | SWT.ON_TOP);

// make the region for the main window
// circle is a method that returns a list of points defining a circle
Region region = new Region();
region.add(350, 0, 981, 51);
region.add(circle(380,51,30));
region.add(circle(951,51,30));
region.add(380, 51, 571, 30);
shell.setRegion(region);
Rectangle rsize = region.getBounds();
shell.setSize(rsize.width, rsize.height);

Composite main = new Composite(shell, SWT.NULL);

// make the label
cpyLbl = new Label(main, SWT.NONE);
cpyLbl.setText("Copy");

cpyLbl.setBackground(SWTResourceManager.getColor(38,255,23));

Region cpyRegion = new Region();
cpyRegion.add(0, 0, 161, 51);
cpyRegion.add(circle(28,51,28));
cpyRegion.add(28, 51, 133, 28);
cpyLbl.setRegion(cpyRegion);

// the top left of the bounds is the 0,0 of the region coordinates
// bounds are in screen coordinates (maybe shell coordinates?)
cpyLbl.setBounds(352, 0, 161, 79);
cpyLbl.setVisible(true);
Run Code Online (Sandbox Code Playgroud)