代码之家  ›  专栏  ›  技术社区  ›  GreenKiwi

Windows7似乎破坏了SWT控件。打印(gc)

  •  1
  • GreenKiwi  · 技术社区  · 16 年前

    已在SWT中归档并修复了一个错误(非常快): https://bugs.eclipse.org/bugs/show_bug.cgi?id=305294

    首先,我的目标是将这两个图像打印到画布上,这样我就可以动画画布在屏幕上滑动(想想iphone),滑动控件本身太占用CPU,所以这是一个很好的选择,直到我在win7上测试它。我愿意接受任何有助于我解决原始问题的方法,而不必解决下面的问题。

    有人知道如何让“control.print(gc)”使用Windows7Aero吗?当禁用Aero时,我的代码在Windows XP和Windows 7中工作正常,但命令:

    打印(GC)使非顶端控件从屏幕上有效删除。

    GC gc = new GC(image);
    try {
        // As soon as this code is called, calling "layout" on the controls
        // causes them to disappear.
        control.print(gc);
    } finally {
        gc.dispose();
    }
    

    我有堆叠的控件,希望打印当前和下一个控件中的图像,以便我可以将它们从屏幕上“滑动”出来。但是,在打印非顶部控件时,它将不再重新绘制。

    下面是一些示例代码。(有趣的代码位位于顶部,需要指向SWT才能工作。)

    谢谢你的帮助。为了解决这个问题,我正在考虑在打印件之间交换控件,看看是否有帮助,但我宁愿不这样做。

    import org.eclipse.swt.SWT;
    import org.eclipse.swt.custom.StackLayout;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.graphics.GC;
    import org.eclipse.swt.graphics.Image;
    import org.eclipse.swt.graphics.Point;
    import org.eclipse.swt.layout.GridData;
    import org.eclipse.swt.layout.GridLayout;
    import org.eclipse.swt.widgets.Button;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Control;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Label;
    import org.eclipse.swt.widgets.Shell;
    
    public class SWTImagePrintTest {
        private Composite stack;
        private StackLayout layout;
        private Label lblFlip;
        private Label lblFlop;
        private boolean flip = true;
        private Button buttonFlop;
        private Button buttonPrint;
    
        /**
         * Prints the control into an image
         * 
         * @param control
         */
        protected void print(Control control) {
            Image image = new Image(control.getDisplay(), control.getBounds());
            GC gc = new GC(image);
            try {
                // As soon as this code is called, calling "layout" on the controls
                // causes them to disappear.
                control.print(gc);
            } finally {
                gc.dispose();
            }
        }
    
        /**
         * Swaps the controls in the stack
         */
        private void flipFlop() {
            if (flip) {
                flip = false;
                layout.topControl = lblFlop;
                buttonFlop.setText("flop");
                stack.layout();
            } else {
                flip = true;
                layout.topControl = lblFlip;
                buttonFlop.setText("flip");
                stack.layout();
            }
        }
    
        private void createContents(Shell shell) {
            shell.setLayout(new GridLayout(2, true));
            stack = new Composite(shell, SWT.NONE);
            GridData gdStack = new GridData(GridData.FILL_BOTH);
            gdStack.horizontalSpan = 2;
            stack.setLayoutData(gdStack);
            layout = new StackLayout();
            stack.setLayout(layout);
            lblFlip = new Label(stack, SWT.BOLD);
            lblFlip.setBackground(Display.getCurrent().getSystemColor(
                    SWT.COLOR_CYAN));
            lblFlip.setText("FlIp");
            lblFlop = new Label(stack, SWT.NONE);
            lblFlop.setBackground(Display.getCurrent().getSystemColor(
                    SWT.COLOR_BLUE));
            lblFlop.setText("fLoP");
            layout.topControl = lblFlip;
            stack.layout();
            buttonFlop = new Button(shell, SWT.FLAT);
            buttonFlop.setText("Flip");
            GridData gdFlip = new GridData();
            gdFlip.horizontalAlignment = SWT.RIGHT;
            buttonFlop.setLayoutData(gdFlip);
            buttonFlop.addSelectionListener(new SelectionAdapter() {
                @Override
                public void widgetSelected(SelectionEvent e) {
                    flipFlop();
                }
    
            });
            buttonPrint = new Button(shell, SWT.FLAT);
            buttonPrint.setText("Print");
            GridData gdPrint = new GridData();
            gdPrint.horizontalAlignment = SWT.LEFT;
            buttonPrint.setLayoutData(gdPrint);
            buttonPrint.addSelectionListener(new SelectionAdapter() {
                @Override
                public void widgetSelected(SelectionEvent e) {
                    print(lblFlip);
                    print(lblFlop);
                }
            });
    
        }
    
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            Shell shell = new Shell();
            shell.setText("Slider Test");
            shell.setSize(new Point(800, 600));
            shell.setLayout(new GridLayout());
            SWTImagePrintTest tt = new SWTImagePrintTest();
            tt.createContents(shell);
            shell.open();
            Display display = Display.getDefault();
            while (shell.isDisposed() == false) {
                if (display.readAndDispatch() == false) {
                    display.sleep();
                }
            }
            display.dispose();
        }
    }
    
    1 回复  |  直到 16 年前
        1
  •  1
  •   GreenKiwi    16 年前

    这是错误的结果: https://bugs.eclipse.org/bugs/show_bug.cgi?id=305294

    已经修好了。希望SWT的一个新的打包版本很快就会出现,这样我们就可以正式使用它了。