您的位置:首页 > 其它

GC绘图:使用setRegion改变swt原生Text的外观为圆角

2011-03-23 21:32 176 查看
首选需要将要改变外观的Text的样式设置为SWT.NONE,若设置为SWT.BORDER,则显示比较怪。
但设置为SWT.NONE时Text没有Border了。显示也比较怪,边界显示的是灰白色。放在背景色比较淡的容器中,几乎看不到Text的边界和范围。
有一种折衷的解决办法,就是在放置Text的容器上为Text模拟绘制一个边界。

eg:
text.addPaintListener(new PaintListener(){
public void paintControl(PaintEvent e)
{
Rectangle recttmp = text.getBounds();
Point size = new Point(recttmp.width,recttmp.height);
final int[] pointArray = new int[]{0,2,2,0,size.x-2,0,size.x,2,size.x,size.y-2,size.x-2,size.y,2,size.y,0,size.y-2,0,2};
Region region = new Region();
region.add(pointArray);
text.setRegion(region);
Color borderColor = new Color(Display.getDefault(),new RGB(202,202,204));
GC gc = new GC(text.getParent());
gc.setForeground(borderColor);
//为Text绘制边界
final int[] pointArray2 = new int[]{recttmp.x-1,recttmp.y+2,2+recttmp.x,0+recttmp.y-1,recttmp.x+size.x-2,0+recttmp.y-1,size.x+recttmp.x,2+recttmp.y,size.x+recttmp.x,size.y-2+recttmp.y,size.x-2+recttmp.x-1,size.y+recttmp.y,2+recttmp.x-1,size.y+recttmp.y,0+recttmp.x-1,size.y-2+recttmp.y,0+recttmp.x-1,2+recttmp.y};
gc.drawPolyline(pointArray2);
//释放静态资源
borderColor.dispose();
gc.dispose();
region.dispose();
}s
});

说明:
final int[] pointArray2 = new int[]{recttmp.x-1,recttmp.y+2,2+recttmp.x,0+recttmp.y-1,recttmp.x+size.x-2,0+recttmp.y-1,size.x+recttmp.x,2+recttmp.y,size.x+recttmp.x,size.y-2+recttmp.y,size.x-2+recttmp.x-1,size.y+recttmp.y,2+recttmp.x-1,size.y+recttmp.y,0+recttmp.x-1,size.y-2+recttmp.y,0+recttmp.x-1,2+recttmp.y};
gc.drawPolyline(pointArray2);
pointArray2数组的一些坐标可能需要微调。

另外还有一种方法是,使用Canvas包装一个Text。将Text尽量充满Canvas。同时在canvas的边界绘制一个圆角边界。

eg:
//文本输入框控件
public class CText extends Canvas implements Listener{
private Text text;

private Color outerColor = getParent().getBackground();
private Color borderColor = new Color(Display.getDefault(),new RGB(255,0,0));
// private Color borderColor = new Color(Display.getDefault(),new RGB(202,202,204));

public CText(Composite parent, int style) {
super(parent,/*SWT.BORDER|*/SWT.DOUBLE_BUFFERED);
GridLayout gl = new GridLayout();
gl.marginWidth= gl.marginHeight = 0;
gl.marginTop = gl.marginBottom = 1;
gl.marginLeft = gl.marginRight = 1;
this.setLayout(gl); // 网格布局,充满
text = new Text(this, SWT.NONE/*|SWT.BORDER*/);
text.setLayoutData(new GridData(GridData.FILL_BOTH));
this.addListener(SWT.Paint, this);
this.addListener(SWT.Dispose,this);
}

//handle event
public void handleEvent(Event event) {
switch(event.type){
case SWT.Paint:onPaint(event);break;
case SWT.Dispose:onDisposed();
}
}

//paint
private void onPaint(Event e) {
Rectangle recttmp = text.getBounds();
Point size = new Point(recttmp.width,recttmp.height);
final int[] pointArray = new int[]{0,2,2,0,size.x-2,0,size.x,2,size.x,size.y-2,size.x-2,size.y,2,size.y,0,size.y-2,0,2};
Region region = new Region();
region.add(pointArray);
text.setRegion(region);
e.gc.setBackground(borderColor);
e.gc.setForeground(borderColor);

final int[] pointArray2 = new int[]{0,2,2,0,size.x-2+1,0,size.x+1,2,size.x+1,size.y-2+1,size.x-2+1,size.y+1,2,size.y+1,0,size.y-2+1,0,2};

// e.gc.fillPolygon(pointArray2);
e.gc.drawPolyline(pointArray2);

// GraphicUtils.drawRoundRectangle(gc, rect.x,rect.y , rect.width-1, rect.height,outerColor,borderColor, true, true);
}

//disposed
private void onDisposed() {
if(outerColor!=null && !outerColor.isDisposed()){
outerColor.dispose();
outerColor = null;
}

if(borderColor!=null && !borderColor.isDisposed()){
borderColor.dispose();
borderColor = null;
}
}

public void setText(String textValue){
text.setText(textValue==null?"":textValue);
}

public String getText(){
if(this.isDisposed()){
return "";
}
return text.getText();
}
}

这种办法的一个缺陷是,当你需要使用Text的一些原生方法时,必须为继承自Canvas的类提供大多数的Text的方法接口。
一个折衷的办法是Canvas的子类中返回所包装的Text实例。
eg:
public class CText extends Canvas implements Listener{
private Text text;

//....其它代码

public Text getTextControl(){
return this.text;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: