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

首选需要将要改变外观的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;
 }
 
 }
    

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值