2009-02-03 86 views
2

我想解决如何让SWT窗口小部件(例如标签,框架)变成矩形以外的其他形状。SWT中的自定义窗口小部件形状

我使用setRegion()方法制作了一个自定义的主窗口。现在我想让窗口中的小部件遵循相同的形状。我试过在小部件上使用setRegion()方法(它们继承它),但没有任何反应。

如何让SWT小部件具有自定义形状?

回答

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); 
相关问题