2013-03-01 2 views

답변

1

을 당신이가는 :

:

public static void main(String[] args) 
{ 
    final Display display = new Display(); 
    final Shell shell = new Shell(display); 
    shell.setText("Widget"); 
    shell.setLayout(new GridLayout(1, false)); 

    final StyledText text = new StyledText(shell, SWT.BORDER); 
    text.setText("Text to capture"); 
    text.setStyleRange(new StyleRange(0, text.getText().length(), display.getSystemColor(SWT.COLOR_RED), display.getSystemColor(SWT.COLOR_BLACK))); 
    text.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false)); 

    Button button = new Button(shell, SWT.PUSH); 
    button.setText("Capture"); 
    button.addListener(SWT.Selection, new Listener() 
    { 
     public void handleEvent(Event event) 
     { 
      Point tableSize = text.getSize(); 
      GC gc = new GC(text); 
      final Image image = new Image(display, tableSize.x, tableSize.y); 
      gc.copyArea(image, 0, 0); 
      gc.dispose(); 

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

      image.dispose(); 
     } 
    }); 

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

이것은 Shell의 모습입니다

enter image description here

그리고이 촬영 된 화상이 모습입니다 :

enter image description here

+0

친애하는 Baz, 코드에 대해 감사드립니다. StyledText를 이미지로 변환 할 수 있지만 내용이 많고 스크롤 판을 사용한 경우 노출 된 내용 만 인쇄되었습니다 .. – CarlJohn

+0

@CarlJohn There 그것을 달성하는 쉬운 방법이 아닙니다. 'GC '로'이미지'에 직접 그림을 그려 볼 수는 있지만 많은 일처럼 들립니다. – Baz