闲话(1)SWT 3.2后的一些更新:关于3.4 不规则窗体与不规则控件的创建

eclipse3.4版本在SWT方面提供了以下有趣的新增功能:
     SWT不规则控件的创建


1:提供真正的SWT全屏状态,不再强制需要使用ON_TOP设置窗体的style,
      在这之前也可达到全屏效果,不过你必须设置shell的region等于屏幕的SIZE.
2:   提供窗体透明与半透明之设置,如windows,部分linux OS。
3:提供真正的不规则控件创建功能,比之前的创建不规则的SHELL更有意思
4   增强SWT对GC或GC对控件内部的绘制。
上述特点将为我们创建更COOL的SWT 界面元素提供基础。

首先我们先有eclipse官方的例子简单说明创建不规则控件的过程

[Java代码]
package org.eclipse.swt.snippets;

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/*
* 使用Region创建不规则控件之SWTButton
*   on a control: create a non-rectangular button

* http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet294.java?view=co
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*
* @since 3.4
*/

public class Snippet294 {

  static int[] circle(int r, int offsetX, int offsetY) {
    int[] polygon = new int[8 * r + 4];
    // x^2 + y^2 = r^2
    for (int i = 0; i < 2 * r + 1; i++) {
      int x = i - r;
      int y = (int)Math.sqrt(r*r - x*x);
      polygon[2*i] = offsetX + x;
      polygon[2*i+1] = offsetY + y;
      polygon[8*r - 2*i - 2] = offsetX + x;
      polygon[8*r - 2*i - 1] = offsetY - y;
    }
    return polygon;
  }

  public static void main(String[] args) {
    final Display display = new Display();
    
    final Shell shell = new Shell(display);
    shell.setText("Regions on a Control");
    shell.setLayout(new FillLayout());
    shell.setBackground(display.getSystemColor(SWT.COLOR_DARK_RED));
    
    Button b2 = new Button(shell, SWT.PUSH);
    b2.setText("Button with Regions");
    
    // define a region that looks like a circle with two holes in ot
    Region region = new Region();
    region.add(circle(67, 87, 77));
    region.subtract(circle(20, 87, 47));
    region.subtract(circle(20, 87, 113));
    
    // define the shape of the button using setRegion
    b2.setRegion(region);
    b2.setLocation(100,50);
    
    b2.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event e) {
        shell.close();
      }
    });
    
    shell.setSize(200,200);
    shell.open();
    
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    region.dispose();
    display.dispose();
  }
  
}


[/java代码]

其次再来看看如何创建不规则的控件,
以下以继承的SWT Button表现不规则按件


package testupdate;

import org.eclipse.swt.graphics.Region;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;

public class cirButton extends Button {
  static int[] circle(int r, int offsetX, int offsetY) {
    int[] polygon = new int[8 * r + 4];
    for (int i = 0; i < 2 * r + 1; i++) {
      int x = i - r;
      int y = (int) Math.sqrt(r * r - x * x);
      polygon[2 * i] = offsetX + x;
      polygon[2 * i + 1] = offsetY + y;
      polygon[8 * r - 2 * i - 2] = offsetX + x;
      polygon[8 * r - 2 * i - 1] = offsetY - y;
    }
    return polygon;
  }

  Region region = null;

  public cirButton(Composite parent, int style) {
    super(parent, style);
    setwingRes();
  }

  /**
   * 指定不规则的区域及形状
   */
  public void setwingRes() {
    region = genRegion();
    setRegion(region);
  }

  // 需要重写checkSubclass方法才可正确使用
  @Override
  protected void checkSubclass() {

  }

  public static Region genRegion() {
    Region region;
    region = new Region();
    region.add(circle(67, 87, 77));
    region.subtract(circle(20, 87, 47));
    region.subtract(circle(20, 87, 113));
    return region;
  }

}

=============================================
测试代码
=============================================
package testupdate;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
* @since Eclispe 3.4
* @author wing5jface
*
*/
public class coolSWTButton {

  public static void main(String[] args) {
    final Display display = new Display();

    final Shell shell = new Shell(display);
    shell.setText("创建不规则控件之SWT");
    final GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns = 2;
    shell.setLayout(gridLayout);
    shell.setBackground(display.getSystemColor(SWT.COLOR_DARK_RED));
    GridData griddata = new GridData(300, 160);
    cirButton button = new cirButton(shell, SWT.PUSH);
    button.setwingRes();
    button.setText("http://www.ben777.cn");
    button.setLayoutData(griddata);

    button = new cirButton(shell, SWT.PUSH);
    button.setText("ben777.cn");
    button.setLayoutData(griddata);
    button = new cirButton(shell, SWT.PUSH);
    button.setwingRes();
    button.setText("cool 控件");
    button.setLayoutData(griddata);
    shell.setSize(1000, 900);
    shell.open();

    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }

    display.dispose();
  }

}

来源wing5jface<http://www.ben777.cn>

你可能感兴趣的:(闲话(1)SWT 3.2后的一些更新:关于3.4 不规则窗体与不规则控件的创建)