78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Bill Baxter <bill@billbaxter.com>
|
|
12 *******************************************************************************/
|
|
13 module snippets.gc.Snippet215;
|
|
14
|
|
15 /*
|
|
16 * GC example snippet: take a screen shot with a GC
|
|
17 *
|
|
18 * For a list of all SWT example snippets see
|
|
19 * http://www.eclipse.org/swt/snippets/
|
|
20 */
|
|
21 import dwt.DWT;
|
|
22 import dwt.graphics.GC;
|
|
23 import dwt.graphics.Image;
|
|
24 import dwt.widgets.Display;
|
|
25 import dwt.widgets.Shell;
|
|
26 import dwt.widgets.Listener;
|
|
27 import dwt.widgets.Event;
|
|
28 import dwt.widgets.Button;
|
|
29 import dwt.widgets.Canvas;
|
|
30 import dwt.custom.ScrolledComposite;
|
|
31 import dwt.layout.FillLayout;
|
|
32 import dwt.events.PaintListener;
|
|
33
|
|
34 Image image;
|
|
35
|
|
36 void main() {
|
|
37 final Display display = new Display();
|
|
38 final Shell shell = new Shell(display);
|
|
39 shell.setLayout(new FillLayout());
|
|
40 Button button = new Button(shell, DWT.PUSH);
|
|
41 button.setText("Capture");
|
|
42 button.addListener(DWT.Selection, new class() Listener {
|
|
43 public void handleEvent(Event event) {
|
|
44
|
|
45 /* Take the screen shot */
|
|
46 GC gc = new GC(display);
|
|
47 image = new Image(display, display.getBounds());
|
|
48 gc.copyArea(image, 0, 0);
|
|
49 gc.dispose();
|
|
50
|
|
51 Shell popup = new Shell(shell, DWT.SHELL_TRIM);
|
|
52 popup.setLayout(new FillLayout());
|
|
53 popup.setText("Image");
|
|
54 popup.setBounds(50, 50, 200, 200);
|
|
55 popup.addListener(DWT.Close, new class() Listener {
|
|
56 public void handleEvent(Event e) {
|
|
57 image.dispose();
|
|
58 }
|
|
59 });
|
|
60
|
|
61 ScrolledComposite sc = new ScrolledComposite (popup, DWT.V_SCROLL | DWT.H_SCROLL);
|
|
62 Canvas canvas = new Canvas(sc, DWT.NONE);
|
|
63 sc.setContent(canvas);
|
|
64 canvas.setBounds(display.getBounds ());
|
|
65 canvas.addPaintListener(new class() PaintListener {
|
|
66 public void paintControl(PaintEvent e) {
|
|
67 e.gc.drawImage(image, 0, 0);
|
|
68 }
|
|
69 });
|
|
70 popup.open();
|
|
71 }
|
|
72 });
|
|
73 shell.pack();
|
|
74 shell.open();
|
|
75 while (!shell.isDisposed()) {
|
|
76 if (!display.readAndDispatch()) display.sleep();
|
|
77 }
|
|
78 display.dispose();
|
|
79 }
|
|
80
|