2012-06-04 93 views
1

有誰知道我會如何打印滾動複合材料的內容?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); 

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

回答

1

Note >>>

見下文@CryptDemon意見。雖然該解決方案已知可在Windows 7和Eclipse 3.7.x版本上運行。


是的,這是可能的。你面臨的問題是由於Windows畫圖事件產生的方式(我假定Windows操作系統,因爲我只有那個!!),因爲 滾動窗口

在任何特定時刻(根據您的代碼),您的scrolled composite的可見尺寸爲,比實際尺寸小。現在,當您在scrolled composite上撥打print()時,只會打印其中一部分(此時可用/可見)的一部分。就像這樣:

Border is for clarity

解決方案是創建scrolled composite內固定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:

enter image description here

+0

那麼,似乎在Windows上工作就好,但它不適用於Linux系統。 GTK小部件仍然只是打印出可視區域。這太遺憾了。我的程序是跨平臺的,所以我真的需要解決這個問題的方法。 – CryptDemon

+0

糟糕!無論如何,在您的問題中添加了「跨平臺」標籤。 – Favonius

0

SWT(GTK3似乎修復這個bug),然而,在我們的項目中,我們打算使用SWT(GTK2)保持,我們的解決方案是創建一個臨時shell和重新控制臨時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(); 
    } 
}