2009-07-27 60 views
3

在Java SWT中,是否有方法在透明外殼上繪製文本,以便只顯示文本可見?我想要做的是將文本顯示在我的桌面上,沒有任何背景窗口。使用shell.setAlpha()將使整個外殼透明,包括任何顯示在其上的元素(我試圖避免)。Java SWT - 透明Shell上的文本?

回答

3

下面是一個示例displaying an image without the shell。你可以很容易地適應它來顯示文本。

更新:

好吧,我很無聊。下面是一個在透明背景上繪製「Hello」的基本示例:

package swttest; 

import org.eclipse.swt.SWT; 
import org.eclipse.swt.events.PaintEvent; 
import org.eclipse.swt.events.PaintListener; 
import org.eclipse.swt.graphics.Font; 
import org.eclipse.swt.graphics.FontData; 
import org.eclipse.swt.graphics.Rectangle; 
import org.eclipse.swt.graphics.Region; 
import org.eclipse.swt.widgets.Canvas; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Shell; 

public class Test { 

    public static void main(String[] args) { 
     Test test = new Test(); 

     test.show(); 
    } 

    public void show() { 
     Display display = new Display(); 
     // Create a shell with no trim 
     final Shell shell = new Shell(display, SWT.NO_TRIM); 
     shell.setForeground(display.getSystemColor(SWT.COLOR_WHITE)); 

     //set the transparent canvas on the shell 
     Canvas canvas = new Canvas(shell, SWT.NO_BACKGROUND); 

     //create an area to paint the text 
     Rectangle size = new Rectangle(0, 0, 200, 200); 
     canvas.setBounds(size); 

     Region region = canvas.getRegion(); 

     //mucking about with fonts 
     Font font = display.getSystemFont(); 

     FontData[] fd = font.getFontData(); 

     fd[0].setHeight(24); 
     fd[0].setStyle(SWT.BOLD); 

     Font bigFont = new Font(display, fd[0]); 
     canvas.setFont(bigFont); 

     // define the shape of the shell using setRegion 
     shell.setRegion(region); 
     shell.setSize(size.width, size.height); 

     canvas.addPaintListener(new PaintListener() { 
      public void paintControl(PaintEvent e) { 
       e.gc.drawString("Hello", 10, 10, true); 
      } 
     }); 

     shell.open(); 
     while (!shell.isDisposed()) { 
      if (!display.readAndDispatch()) 
       display.sleep(); 
     } 
     region.dispose(); 
     display.dispose(); 
    } 
} 
+2

在Windows 7和SWT 3.550上無法正常工作。顯示一個沒有文字的黑色矩形。 – 2009-09-24 23:55:55