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

📄 demoshell.java

📁 JAVA透明窗体的例子
💻 JAVA
字号:
package demo.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Region;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class DemoShell extends Shell implements ControlListener, PaintListener,
		MouseListener, MouseMoveListener, MouseTrackListener {
	private Composite northwestPanel;

	private Composite northeastPanel;

	private Composite northPanel;

	private Composite southwestPanel;

	private Composite southeastPanel;

	private Composite southPanel;

	private Composite westPanel;

	private Composite eastPanel;

	private Composite contentPanel;

	// window button
	private Composite closeButton;

	// icon

	private Image northwestImage;

	private Image northeastImage;

	private Image northImage;

	private Image southwestImage;

	private Image southeastImage;

	private Image southImage;

	private Image westImage;

	private Image eastImage;

	private Image closeImage;

	private Image closeOverImage;

	private Image closeDownImage;

	// color

	private Color color1 = new Color(getDisplay(), 255, 255, 254);

	private Color color2 = new Color(getDisplay(), 204, 220, 247);

	private Color titleColor = new Color(getDisplay(), 128, 128, 128);

	//
	private Point location;

	private Point size;

	// cursor
	private Cursor seCursor = new Cursor(getDisplay(), SWT.CURSOR_SIZESE);

	private Cursor titleCursor = new Cursor(getDisplay(), SWT.CURSOR_SIZEALL);

	/**
	 * Create the shell
	 * 
	 * @param display
	 * @param style
	 */
	public DemoShell(Display display) {
		super(display, SWT.NO_TRIM);
		createContents();
		addControlListener(this);
	}
	
	/**
	 * Create contents of the window
	 */
	private void createContents() {
		northwestPanel = new Composite(this, SWT.NONE);
		northwestImage = new Image(getDisplay(), "icon/northwest.png");
		northwestPanel.setBackgroundImage(northwestImage);
		northeastPanel = new Composite(this, SWT.NONE);
		northeastImage = new Image(getDisplay(), "icon/northeast.png");
		northeastPanel.setBackgroundImage(northeastImage);
		northPanel = new Composite(this, SWT.NONE);
		northImage = new Image(getDisplay(), "icon/north.png");
		northPanel.setBackgroundImage(northImage);
		southwestPanel = new Composite(this, SWT.NONE);
		southwestImage = new Image(getDisplay(), "icon/southwest.png");
		southwestPanel.setBackgroundImage(southwestImage);
		southeastPanel = new Composite(this, SWT.NONE);
		southeastImage = new Image(getDisplay(), "icon/southeast.png");
		southeastPanel.setBackgroundImage(southeastImage);
		southPanel = new Composite(this, SWT.NONE);
		southImage = new Image(getDisplay(), "icon/south.png");
		southPanel.setBackgroundImage(southImage);
		westPanel = new Composite(this, SWT.NONE);
		westImage = new Image(getDisplay(), "icon/west.png");
		westPanel.setBackgroundImage(westImage);
		eastPanel = new Composite(this, SWT.NONE);
		eastImage = new Image(getDisplay(), "icon/east.png");
		eastPanel.setBackgroundImage(eastImage);
		contentPanel = new Composite(this, SWT.NONE);
		contentPanel.setBackgroundMode(SWT.INHERIT_FORCE);
		contentPanel.addPaintListener(this);
		//
		closeButton = new Composite(this, SWT.NONE);
		closeImage = new Image(getDisplay(), "icon/close.png");
		closeOverImage = new Image(getDisplay(), "icon/closeover.png");
		closeDownImage = new Image(getDisplay(), "icon/closedown.png");
		closeButton.setBackgroundImage(closeImage);
		closeButton.addMouseListener(this);
		closeButton.addMouseTrackListener(this);
		//
		northPanel.addMouseListener(this);
		northPanel.addMouseMoveListener(this);
		northPanel.setCursor(titleCursor);
		northPanel.addPaintListener(this);
		southeastPanel.addMouseListener(this);
		southeastPanel.addMouseMoveListener(this);
		southeastPanel.setCursor(seCursor);
	}

	public void controlMoved(ControlEvent e) {
		// TODO 自动生成方法存根

	}

	public void controlResized(ControlEvent e) {
		int w = getSize().x;
		int h = getSize().y;
		northwestPanel.setBounds(0, 0, northwestImage.getBounds().width,
				northwestImage.getBounds().height);
		northeastPanel.setBounds(w - northeastImage.getBounds().width, 0,
				northeastImage.getBounds().width,
				northeastImage.getBounds().height);
		closeButton.setBounds(w - northeastImage.getBounds().width
				- closeImage.getBounds().width, 0,
				closeImage.getBounds().width, closeImage.getBounds().height);
		northPanel.setBounds(northwestImage.getBounds().width, 0, w
				- northwestImage.getBounds().width
				- northeastImage.getBounds().width
				- closeImage.getBounds().width, northImage.getBounds().height);
		southwestPanel.setBounds(0, h - southwestImage.getBounds().height,
				southwestImage.getBounds().width,
				southwestImage.getBounds().height);
		southeastPanel.setBounds(w - southeastImage.getBounds().width, h
				- southeastImage.getBounds().height,
				southeastImage.getBounds().width,
				southeastImage.getBounds().height);
		southPanel.setBounds(southwestImage.getBounds().width, h
				- southImage.getBounds().height, w
				- southwestImage.getBounds().width
				- southeastImage.getBounds().width,
				southImage.getBounds().height);
		westPanel.setBounds(0, northwestImage.getBounds().height, westImage
				.getBounds().width, h - northwestImage.getBounds().height
				- southwestImage.getBounds().height);
		eastPanel.setBounds(w - eastImage.getBounds().width, northeastImage
				.getBounds().height, eastImage.getBounds().width, h
				- northeastImage.getBounds().height
				- southeastImage.getBounds().height);
		contentPanel
				.setBounds(westImage.getBounds().width,
						northImage.getBounds().height, w
								- westImage.getBounds().width
								- eastImage.getBounds().width, h
								- northImage.getBounds().height
								- southImage.getBounds().height);
		// region
		Region oldRegion = getRegion();
		if (oldRegion != null && !oldRegion.isDisposed()) {
			oldRegion.dispose();
		}
		Region newRegion = new Region();
		newRegion.add(0, 0, getSize().x, getSize().y);
		newRegion.subtract(getImageTransparenceRegion(northwestImage, 0, 0));
		newRegion.subtract(getImageTransparenceRegion(northeastImage,
				getSize().x - northeastImage.getBounds().width, 0));
		newRegion.subtract(getImageTransparenceRegion(southwestImage, 0,
				getSize().y - southwestImage.getBounds().y));
		newRegion.subtract(getImageTransparenceRegion(southeastImage,
				getSize().x - southeastImage.getBounds().width, getSize().y
						- southeastImage.getBounds().height));
		setRegion(newRegion);
	}

	public void paintControl(PaintEvent e) {
		GC gc = e.gc;
		if (e.getSource() == contentPanel) {
			gc.setBackground(color1);
			gc.setForeground(color2);
			gc.fillGradientRectangle(0, 0, e.width, e.height, true);
		} else if (e.getSource() == northPanel) {
			String text = getText();
			if (text != null) {
				gc.setForeground(titleColor);
				gc.drawText(text, e.width / 2 - gc.stringExtent(text).x / 2,
						e.height / 2 - gc.stringExtent(text).y / 2, true);
			}
			Image image = getImage();
			if (text == null) {
				text = "";
			}
			if (image != null) {
				gc.drawImage(image, e.width / 2 - gc.stringExtent(text).x / 2
						- image.getBounds().width - 10, e.height / 2
						- image.getBounds().height / 2);
			}
		}
		gc.dispose();
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public void mouseDoubleClick(MouseEvent e) {
		if (e.getSource() == northPanel) {
			setMaximized(!getMaximized());
		}
	}

	public void mouseDown(MouseEvent e) {
		if (e.getSource() == northPanel) {
			if (!getMaximized()) {
				location = new Point(e.x, e.y);
			}
		} else if (e.getSource() == southeastPanel) {
			size = new Point(e.x, e.y);
		} else if (e.getSource() == closeButton) {
			closeButton.setBackgroundImage(closeDownImage);
		}
	}

	public void mouseUp(MouseEvent e) {
		if (e.getSource() == northPanel) {
			location = null;
		} else if (e.getSource() == southeastPanel) {
			if (size == null) {
				return;
			}
			setSize(new Point(getBounds().width + e.x - size.x,
					getBounds().height + e.y - size.y));
			size = null;
		} else if (e.getSource() == closeButton) {
			if (e.x > 0 && e.x < closeButton.getSize().x && e.y > 0
					&& e.y < closeButton.getSize().y) {
				closeButton.setBackgroundImage(closeOverImage);
				dispose();
			} else {
				closeButton.setBackgroundImage(closeImage);
			}
		}
	}

	public void mouseMove(MouseEvent e) {
		if (e.getSource() == northPanel) {
			if (location != null) {
				Point p = getDisplay().map(this, null, e.x, e.y);
				setLocation(p.x - location.x, p.y - location.y);
			}
		}
	}

	public void mouseEnter(MouseEvent e) {
		if (e.getSource() == closeButton) {
			closeButton.setBackgroundImage(closeOverImage);
		}
	}

	public void mouseExit(MouseEvent e) {
		if (e.getSource() == closeButton) {
			closeButton.setBackgroundImage(closeImage);
		}
	}

	public void mouseHover(MouseEvent e) {

	}

	@Override
	public void dispose() {
		try {
			northwestImage.dispose();
			northeastImage.dispose();
			northImage.dispose();
			southwestImage.dispose();
			southeastImage.dispose();
			southImage.dispose();
			westImage.dispose();
			eastImage.dispose();
			closeImage.dispose();
			closeOverImage.dispose();
			color1.dispose();
			color2.dispose();
			titleColor.dispose();
		} finally {
			super.dispose();
		}
	}

	private Region getImageTransparenceRegion(Image image, int offsetX,
			int offsetY) {
		Region region = new Region();
		final ImageData imageData = image.getImageData();
		if (imageData.alphaData != null) {
			Rectangle pixel = new Rectangle(0, 0, 1, 1);
			for (int y = 0; y < imageData.height; y++) {
				for (int x = 0; x < imageData.width; x++) {
					if (imageData.getAlpha(x, y) != 255) {
						pixel.x = imageData.x + x + offsetX;
						pixel.y = imageData.y + y + offsetY;
						region.add(pixel);
					}
				}
			}
		}
		return region;
	}
}

⌨️ 快捷键说明

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