有谁知道如何打印滚动复合材料的内容?
当我在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);
答案 0 :(得分:1)
Note >>>
请参阅下面的@CryptDemon条评论。虽然已知该解决方案适用于Windows 7和eclipse 3.7.x版本。
<小时/> 对的,这是可能的。您面临的问题是由于生成Windows绘制事件的方式(我假设Windows操作系统,因为我只有那个!)用于滚动窗口。
在任何特定时刻(根据您的代码),scrolled composite
的可见尺寸 比实际尺寸小。现在,当您在print()
上拨打scrolled composite
时,只会打印其中的一部分(当时可用/可见)。像这样:
解决方案是在Composite
内创建一个固定的scrolled composite
,并在其上打印。
<强> 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:
强>
答案 1 :(得分:0)
SWT(GTK3似乎解决了这个问题),但是在我们的项目中我们打算继续使用SWT(GTK2),我们的解决方案是创建一个临时shell并重新设置到temp shell的控制
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();
}
}