2

有谁知道我将如何打印滚动合成的内容?

每当我打印到 GC 上时,它只会复制滚动合成的当前可见区域。我想要的是能够复制滚动合成的全部内容。

例如,下面的代码在一个小窗口内创建了一个巨大的按钮。当我打印下面的 gc 时,它只会输出滚动复合的小可视区域。是否可以打印滚动合成中的所有内容?

Shell shell = new Shell(getDisplay());
shell.setLayout(new FillLayout());
shell.setSize(200, 200);
shell.setLocation(20, 20);


ScrolledComposite sc = new ScrolledComposite(shell, SWT.H_SCROLL| SWT.V_SCROLL);
sc.setMinSize(availWidth, availHeight + 30);

Button b = new Button(sc, SWT.PUSH);
b.setText("Button");
b.setSize(availWidth, availHeight);
sc.setContent(b);

Image image = new Image(getDisplay(), availWidth, availHeight);
GC imageGC = new GC(image);
sc.print(imageGC);
4

2 回答 2

2

Note >>>

请参阅下面的@CryptDemon评论。尽管已知该解决方案适用于 Windows 7 和 eclipse 3.7.x 版本。


对的,这是可能的。您面临的问题是由于为滚动窗口生成 Windows 绘制事件的方式(我假设是 Windows 操作系统,因为我只有那个!!)。

在任何给定时刻(根据您的代码),您的可见尺寸scrolled composite小于实际尺寸。print()现在,当您调用scrolled compositethen 时,只有其中的一部分(当时可用/可见)被打印出来。像这样:

边框是为了清晰

解决方案是在您的Composite内部创建一个固定scrolled composite并调用 print 。

Code:

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
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.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

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

        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        shell.setSize(200, 200);
        shell.setLocation(20, 20);

        final ScrolledComposite sc = new ScrolledComposite(shell, SWT.BORDER  | SWT.V_SCROLL | SWT.H_SCROLL);
        sc.setLayout(new GridLayout(1,true));

        final Composite innerComposite = new Composite(sc, SWT.NONE);
        innerComposite.setSize(400, 400);
        innerComposite.setLayout(new GridLayout(1, true));

        for(int i = 0; i < 10; i++)
        {
                Button b = new Button(innerComposite, SWT.PUSH);
                b.setText("Text");
                b.addSelectionListener(new SelectionAdapter() {
                    public void widgetSelected(SelectionEvent e) 
                    {
                        Image image = new Image(display, innerComposite.getBounds().width, innerComposite.getBounds().height);
                        ImageLoader loader = new ImageLoader();

                        GC gc = new GC(image);
                        sc.print(gc);
                        gc.dispose();

                        loader.data = new ImageData[]{image.getImageData()};
                        loader.save("c:/temp/out.png", SWT.IMAGE_PNG);
                    }
                });
        }

        sc.setMinSize(innerComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
        sc.setContent(innerComposite);
        sc.setExpandHorizontal(true);
        sc.setExpandVertical(true);


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

Output:

在此处输入图像描述

于 2012-06-05T09:52:04.697 回答
1

SWT(GTK3 似乎修复了这个错误),但是在我们的项目中,我们打算继续使用 SWT(GTK2),我们的解决方案是创建一个临时外壳并将控制重新设置为临时外壳

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
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.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

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

        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        shell.setSize(200, 200);
        shell.setLocation(20, 20);

        final ScrolledComposite sc = new ScrolledComposite(shell, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
        sc.setLayout(new GridLayout(1, true));

        final Composite innerComposite = new Composite(sc, SWT.NONE);
        innerComposite.setSize(400, 400);
        innerComposite.setLayout(new GridLayout(1, true));

        for (int i = 0; i < 10; i++) {
            Button b = new Button(innerComposite, SWT.PUSH);
            b.setText("Text");
            b.addSelectionListener(new SelectionAdapter() {
                public void widgetSelected(SelectionEvent e) {
                    Image image = new Image(display, innerComposite.getBounds().width,
                            innerComposite.getBounds().height);
                    ImageLoader loader = new ImageLoader();

                    GC gc = new GC(image);

                    // Create a temp shell and reparent it+
                    Shell newShell = new Shell(shell);
                    newShell.setLayout(new FillLayout());
                    Composite oldParent = sc.getParent();
                    oldParent.setRedraw(false);
                    sc.setParent(newShell);
                    newShell.open();
                    sc.print(gc);
                    sc.setParent(oldParent);
                    oldParent.setRedraw(true);

                    gc.dispose();
                    newShell.dispose();

                    loader.data = new ImageData[] { image.getImageData() };
                    loader.save("./out.png", SWT.IMAGE_PNG);
                }
            });
        }

        sc.setMinSize(innerComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
        sc.setContent(innerComposite);
        sc.setExpandHorizontal(true);
        sc.setExpandVertical(true);

        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}
于 2016-07-29T02:24:18.987 回答