不規則表單的大體構建思路與以前沒有什麼不同,在這裡,我僅陳述在java中怎樣實現它。本次內容在eclipse平台下開發,需要swt 的外掛程式,可以到swt的官方網站www.swt-designer.com去下載。先說一下思路吧:先載入一幅圖片,這幅圖片具有不規則特性,然後將這幅圖片設定為表單的背景,這樣就算完蛋。 載入圖片靠以下代碼來完成
public Image getImage(Display display){ InputStream is = ImageLoad.
class.getResourceAsStream(
this.IMAGE_NAME); Image result =
null;
try {
if (is !=
null) { ImageData imageData =
new ImageData(is);
if (
imageData !=
null) { result =
new Image(display, imageData); } } }
catch (Exception e) { e.printStackTrace(); }
finally {
try {
if(is !=
null) is.close(); }
catch (IOException e1) { e1.printStackTrace(); } }
return result; }其中,ImageLoad.
class.getResourceAsStream(
this.IMAGE_NAME)的ImageLoad為方法所屬的類,
this.IMAGE_NAME為圖片的名稱。這樣就返回一個圖片。設定表單的背景圖片則靠以下代碼來實現://載入圖片 image = new ImageLoad().getImage(display); //region代表地區,以下代碼要求構造地區 Region region =
new Region(); imageData = image.getImageData(); //區分image是α還是β像素
if (imageData.alphaData !=
null) {
for (
int y = 0; y < imageData.height; y++) {
for (
int x = 0; x < imageData.width; x++) {
if (imageData.getAlpha(x, y) ==255) { region.add(imageData.x + x,imageData.y + y,1,1); } } } }
else { ImageData mask = imageData.getTransparencyMask();
for (
int y = 0; y < mask.height; y++) {
for (
int x = 0; x < mask.width; x++) {
if (mask.getPixel(x, y) !=0 ) { region.add(imageData.x + x,imageData.y + y,1,1); } } } } shell.setRegion(region); shell.setSize(imageData.x + imageData.width, imageData.y + imageData.height);注釋中的詳解已經解釋得比較明白了,我就不多說了。不規則表單就這樣構建完畢。但是還需要給表單設定表單拖拽重繪製的功效,這樣才能實現一般表單的功能,這就需要你對shell表單添加一些監聽器,用於處理表單重繪、滑鼠拖拽、表單關閉的事件方法。原始碼可以到http://download.csdn.net/user/lejuo/下載