【问题标题】:Java SWT - Text on Transparent Shell?Java SWT - 透明外壳上的文本?
【发布时间】:2009-07-27 08:01:54
【问题描述】:

在 Java SWT 中,有没有办法在透明外壳上绘制文本,以便只有文本可见?我想做的是让文本出现在我的桌面上,没有任何背景窗口。使用 shell.setAlpha() 将使整个 shell 透明,包括出现在上面的任何元素(我试图避免)。

【问题讨论】:

    标签: java user-interface swt


    【解决方案1】:

    这是一个示例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();
        }
    }
    

    【讨论】:

    • 在 Windows 7 和 SWT 3.550 上对我不起作用。显示一个没有文本的黑色矩形。
    猜你喜欢
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多