⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 capturescreen.java

📁 用SWT 实现的截屏程序。源码中有注释
💻 JAVA
字号:
package swt;

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Transform;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class CaptureScreen {//2007-8-2, weilai2@163.com
	Shell shell;
	Canvas canvas;
	Button bt;
	final Display display;

	public CaptureScreen(final Display display) {
		this.display = display;
		final float scaleX = 0.2f, scaleY = 0.2f;
		final Rectangle bound0 = display.getBounds();
		//imgRect is the image's position shown in canvas.
		final Rectangle imgRect = new Rectangle(0, 0,
				(int) (scaleX * bound0.width), (int) (scaleY * bound0.height));

		shell = new Shell(display);
		shell.setText("CaptureScreen, ctrl+c save to file.");
		shell.setBounds(0, 0, bound0.width / 3, bound0.height / 4);
		Canvas[][] LR = addCanvases(shell, new int[] { 100 }, new int[] { 20,
				80 }, SWT.BORDER);
		Button[][] bts = addButtons(LR[0][0], 4, 1, SWT.PUSH);
		bt = bts[0][0];
		bt.setText("capture");
		canvas = LR[0][1];
		//
		Button bt = bts[0][0];
		MouseListeners_CaptureScreen mouse = new MouseListeners_CaptureScreen(
				canvas);
		new KeyListener_CaptureScreen(canvas, mouse, imgRect);

		//
		canvas.addPaintListener(new PaintListener() {
			Transform trans = new Transform(display);
			{
				trans.scale(scaleX, scaleY);//zoom in, zoom out...
			}

			public void paintControl(PaintEvent e) {
				GC gc = e.gc;
				Image img = copyArea(0, 0, display, display.getBounds());
				gc.setTransform(trans);
				gc.drawImage(img, 0, 0);
			}
		});
		bt.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
			}
		});
		post();
	}

	/**
	 *   Copies a rectangular area of the receiver(PC window?) at the specified
	 * position(x,y..) into a image(with size of $imgSize).
	 */
	public static Image copyArea(int x, int y, Display dis, Rectangle imgSize) {
		Image image = new Image(dis, imgSize);
		final GC gc = new GC(dis);
		gc.copyArea(image, x, y);
		return image;
	}

	private void post() {
		shell.pack();//seemed that it caused  compression.
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

	/**
	 * add new canvas on $parent with GridLayout. the count of new canvas is $rows.len*$cols.len. <br>
	 * if rows[i] is 22, rows[i]'s high  is 22% of $parent's  high....etc.<br>
	 * if cols[j] is 22, cols[j]'s width is 22% of $parent's width....etc.<br>
	 * should: sigmaSum(rows[i])==sigmaSum(cols[i])==1 . <br>
	 * $style is SWT.BORDER...etc.
	 */
	public static Canvas[][] addCanvases(Canvas parent, int[] rows, int[] cols,
			int style) {
		GridLayout layout = new GridLayout(cols.length, false);
		parent.setLayout(layout);
		Canvas[][] ret = new Canvas[rows.length][];
		final int height0 = (int) (parent.getBounds().height * 0.8);//0.8 avoid figure excess the screen.
		final int width0 = (int) (parent.getBounds().width * 0.9);
		int rowY = 0;
		for (int i = 0; i < ret.length; i++) {
			final int rowHigh = (int) (height0 * rows[i] / (float) 100);
			ret[i] = new Canvas[cols.length];
			int colX = 0;
			for (int j = 0; j < ret[i].length; j++) {
				ret[i][j] = new Canvas(parent, SWT.BORDER);
				int colWidth = (int) (width0 * cols[j] / (float) 100);
				//
				GridData d = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
				d.widthHint = colWidth;
				d.heightHint = rowHigh;
				d.grabExcessHorizontalSpace = true;
				d.grabExcessVerticalSpace = true;
				ret[i][j].setLayoutData(d);
				//
				ret[i][j].setBounds(colX, rowY, colWidth, rowHigh);
				colX += colWidth;
			}
			rowY += rowHigh;
		}
		return ret;
	}

	/**
	 * addButton buttons to $parent, style is SWT.PUSH...etc.
	 */
	public static Button[][] addButtons(Canvas parent, int row, int col,
			int style) {
		GridLayout layout = new GridLayout();
		layout.numColumns = col;
		Rectangle[][] r = splitRectangle(parent.getBounds(), row, col);
		Button[][] b = new Button[row][];
		for (int i = 0; i < b.length; i++) {
			b[i] = new Button[col];
			for (int j = 0; j < b[i].length; j++) {
				b[i][j] = new Button(parent, style);
				b[i][j].setText("but" + i + "" + j);
				b[i][j].setBounds(r[i][j]);
			}
		}
		return b;
	}

	/**
	 * splitRectangle(x,y,width,height) to row*col sub rectangles.
	 */
	public static Rectangle[][] splitRectangle(Rectangle r, int row, int col) {
		int w = r.width / col;
		int h = r.height / row;
		Rectangle[][] ret = new Rectangle[row][];
		int curY = 0;
		for (int i = 0; i < ret.length; i++) {
			ret[i] = new Rectangle[col];
			int curX = 0;
			for (int j = 0; j < ret[i].length; j++) {
				ret[i][j] = new Rectangle(curX, curY, w, h);
				curX += w;
			}
			curY += h;
		}
		return ret;
	}

	public static void main(String[] args) {
		Display dis = new Display();
		new CaptureScreen(dis);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -