SWT Screenshots erstellen

Thomas Darimont

Erfahrenes Mitglied
Hallo!

Hier mal ein kleines Beispiel zur Erstellung von Screenshots der Oberflaeche einer
SWT Anwendung anhand einer Eclipse-Action.
Java:
package de.tutorials.eclipse.screencapture.actions;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
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.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;

public class CreateScreenShotAction implements IWorkbenchWindowActionDelegate {
    private IWorkbenchWindow window;
    /**
     * The constructor.
     */
    public CreateScreenShotAction() {
    }

    /**
     * The action has been activated. The argument of the
     * method represents the 'real' action sitting
     * in the workbench UI.
     * @see IWorkbenchWindowActionDelegate#run
     */
    public void run(IAction action) {
        final Display display = Display.getDefault();
        Rectangle rectangle = display.getBounds();
        Image image = new Image(display,rectangle.width,rectangle.height);
        GC gc = new GC(display);
        gc.copyArea(image, 0,0);
        ImageLoader imageLoader = new ImageLoader();
        imageLoader.data = new ImageData[]{image.getImageData()};
        imageLoader.save("c:/screenShot.jpg", SWT.IMAGE_JPEG);
        image.dispose();
        gc.dispose();
        
        MessageDialog.openInformation(
                window.getShell(),
                "Screencapture Plug-in",
                "Screenshot created");
    }

    /**
     * Selection in the workbench has been changed. We 
     * can change the state of the 'real' action here
     * if we want, but this can only happen after 
     * the delegate has been created.
     * @see IWorkbenchWindowActionDelegate#selectionChanged
     */
    public void selectionChanged(IAction action, ISelection selection) {
    }

    /**
     * We can use this method to dispose of any system
     * resources we previously allocated.
     * @see IWorkbenchWindowActionDelegate#dispose
     */
    public void dispose() {
    }

    /**
     * We will cache window object in order to
     * be able to provide parent shell for the message dialog.
     * @see IWorkbenchWindowActionDelegate#init
     */
    public void init(IWorkbenchWindow window) {
        this.window = window;
    }
}

Gruss Tom
 
Auch wenn es nach fünf Jahren ein wenig spät kommt. Muß es in der run-Methode nicht

gc.copyArea(image, rectangle.x, rectangle.y);

heißen um die aktive Anwendung zu bekommen?

Trotzdem ein tolles Beispiel.
 

Neue Beiträge

Zurück