简体   繁体   English

SWT中的自定义小部件形状

[英]Custom widget shapes in SWT

I'm trying to work out how to make SWT widgets (eg Label, Frame) be some shape other than rectangular. 我正在尝试找出如何使SWT小部件(例如Label,Frame)成为矩形以外的其他形状。

I've made a custom shaped main window using the setRegion() method. 我使用setRegion()方法制作了一个自定义形状的主窗口。 Now I would like the widgets in the window to follow the same shape. 现在,我希望窗口中的小部件遵循相同的形状。 I've tried using the setRegion() method on the widgets themselves (they inherit it) but nothing happens. 我尝试在小部件本身上使用setRegion()方法(它们继承了它),但是什么也没有发生。

How do I make an SWT widget have a custom shape? 如何使SWT小部件具有自定义形状?

I've managed to work it out. 我已经设法解决了。 It seems my difficulty was arising from a misunderstanding of how to define a custom region. 似乎我的困难源于对如何定义自定义区域的误解。 The setBounds() method defines the coordinate system for the custom region. setBounds()方法定义自定义区域的坐标系。 I was assuming the region and setBounds were using the same coordinate system and the result was not displaying the widget at all. 我以为region和setBounds使用相同的坐标系,结果根本不显示小部件。

The picture displays the effect. 图片显示效果。 The green label on the left curves like the main window, while the grey frame on the bottom right corner doesn't. 左侧的绿色标签像主窗口一样弯曲,而右下角的灰色框则不一样。

The image appears in the preview but not the posted answer, try: http://img87.imageshack.us/my.php?image=curvecroppedwf8.png 该图像显示在预览中,而不显示在发布的答案中,请尝试: http : //img87.imageshack.us/my.php? image= curvecroppedwf8.png

The code fragments to do this: 代码片段可以做到这一点:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM