SWT如何打印scrolledComposite的内容? [英] SWT How to print contents of a scrolledComposite?

查看:258
本文介绍了SWT如何打印scrolledComposite的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道如何打印滚动组合的内容?



当我打印到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);

按钮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);


解决方案

注意> ;>>



请参阅 @CryptDemon 如下。虽然该解决方案已知可在Windows 7上工作,并具有eclipse 3.7.x版本。



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



在任何给定的时刻(根据您的代码),您的滚动复合的可见大小为比实际大小。现在当您在滚动复合材料上调用 print()时,只有其中的一部分可用/可见)被打印。像这样:





解决方案是在滚动内创建一个固定的复合复合并打印。



代码:

  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 = 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)); (int i = 0; i< 10; i ++)
{
按钮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.setExpandHorizo​​ntal(true);
sc.setExpandVertical(true);


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

输出:




Does anyone know how I would print the contents of a scrolled composite?

Whenver I print onto a GC it will only copy the currently viewable area of the scrolled composite. What I want is to be able to copy the entire contents of the scrolled composite.

For instance, the code below makes a huge button, inside of a little window. Whenver I print the gc below, it only will output the small viewable area of the scrolled composite. Is it possible to print everything in the scrolled composite?

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);

解决方案

Note >>>

See @CryptDemon comments below. Though the solution is known to work on Windows 7 and with eclipse 3.7.x release.


Yes it is possible. The problem you are facing is due the way Windows paint events are generated (I am assuming Windows OS because I only have that !!) for a scrolling window.

At any given moment (as per your code) the visible size of your scrolled composite is less than the actual size. Now when you call print() on your scrolled composite then only a part of that (which is at that moment is available/visible) gets printed. Like this:

The solution is to create a fixed Composite inside your scrolled composite and call print on that.

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:

这篇关于SWT如何打印scrolledComposite的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆