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

📄 titlebarbutton.java

📁 Eclipse 编程技术与实例,讲解了Eclipse编辑器的使用方法
💻 JAVA
字号:
package org.test.custom.internal;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

import org.test.custom.shell.InternalShell;

public class TitleBarButton extends CustomDrawnButton {
	private final Color highlightShadowColor, lightShadowColor,
			normalShadowColor, darkShadowColor;

	private final Color gradEndColor, inactiveGradEndColor,
			widgetBackgroundColor, widgetForegroundColor;

	private final int style;

	private final Shell shell;

	private final Display display;

	private final InternalShell ishell;

	private int leftOff, rightOff;

	public TitleBarButton(InternalShell parent, int style) {
		super(parent, SWT.NO_FOCUS | SWT.NO_BACKGROUND);
		this.style = style;
		this.shell = getShell();
		this.display = getDisplay();
		this.ishell = parent;

		// 获得控件在不同情况下的颜色
		highlightShadowColor = display
				.getSystemColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW);
		lightShadowColor = display
				.getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW);
		normalShadowColor = display
				.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW);
		darkShadowColor = display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW);
		gradEndColor = display
				.getSystemColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT);
		inactiveGradEndColor = display
				.getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT);
		widgetBackgroundColor = display
				.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
		widgetForegroundColor = display
				.getSystemColor(SWT.COLOR_WIDGET_FOREGROUND);

		if ((style & (SWT.CLOSE | SWT.MAX)) != 0)
			rightOff = 2;
		else
			leftOff = 2;

		// 注册事件侦听器
		final Listener activateListener = new Listener() {
			public void handleEvent(Event event) {
				redraw();
			}
		};
		final Listener deactivateListener = new Listener() {
			public void handleEvent(Event event) {
				redraw();
			}
		};
		shell.addListener(SWT.Activate, activateListener);
		shell.addListener(SWT.Deactivate, deactivateListener);

		addListener(SWT.Dispose, new Listener() {
			public void handleEvent(Event event) {
				shell.removeListener(SWT.Activate, activateListener);
				shell.removeListener(SWT.Deactivate, deactivateListener);
			}
		});
	}

	// 返回控件样式
	public int getStyle() {
		return style;
	}

	// 实现父类的抽象方法
	protected void onPaint(Event event, boolean pressed) {
		Point size = getSize();
		boolean active = (shell == display.getActiveShell() && ishell
				.isActiveShell());
		GC gc = event.gc;

		// 设置背景颜色
		gc.setBackground(active ? gradEndColor : inactiveGradEndColor);
		gc.fillRectangle(0, 0, size.x, size.y);
		gc.setBackground(widgetBackgroundColor);
		gc.fillRectangle(2, 4, size.x - 4, size.y - 6);

		Color tloColor, tliColor, broColor, briColor;
		int pOff;

		// 根据按钮的状态设置它的颜色
		if (pressed) {
			tloColor = darkShadowColor;
			tliColor = normalShadowColor;
			broColor = highlightShadowColor;
			briColor = lightShadowColor;
			pOff = 1;
		} else {
			tloColor = highlightShadowColor;
			tliColor = lightShadowColor;
			broColor = darkShadowColor;
			briColor = normalShadowColor;
			pOff = 0;
		}

		drawBevelRect(gc, leftOff, 2, size.x - 1 - leftOff - rightOff,
				size.y - 5, tloColor, broColor);
		drawBevelRect(gc, 1 + leftOff, 3, size.x - 3 - leftOff - rightOff,
				size.y - 7, tliColor, briColor);

		// 根据控件是否能用设置它的颜色
		if (isEnabled()) {
			gc.setForeground(widgetForegroundColor);
			drawImage(gc, size, pOff);
		} else {
			gc.setForeground(highlightShadowColor);
			drawImage(gc, size, 1);
			gc.setForeground(normalShadowColor);
			drawImage(gc, size, 0);
		}
	}

	// 画出图形按钮
	private void drawImage(GC gc, Point size, int pOff) {
		if ((style & SWT.CLOSE) != 0)
			drawCloseImage(gc, size, pOff);
		else if ((style & SWT.MAX) != 0) {
			if (ishell.getMaximized())
				drawRestoreImage(gc, size, pOff);
			else
				drawMaximizeImage(gc, size, pOff);
		} else if ((style & SWT.MIN) != 0)
			drawMinimizeImage(gc, size, pOff);
	}

	private static void drawBevelRect(GC gc, int x, int y, int w, int h,
			Color topleft, Color bottomright) {
		gc.setForeground(bottomright);
		gc.drawLine(x + w, y, x + w, y + h);
		gc.drawLine(x, y + h, x + w, y + h);

		gc.setForeground(topleft);
		gc.drawLine(x, y, x + w - 1, y);
		gc.drawLine(x, y, x, y + h - 1);
	}

	// 画出关闭按钮的图形
	private void drawCloseImage(GC gc, Point size, int pOff) {
		gc.drawLine(pOff + leftOff + 4, pOff + 5, pOff + size.x - leftOff
				- rightOff - 6, pOff + size.y - 7);
		gc.drawLine(pOff + leftOff + 5, pOff + 5, pOff + size.x - leftOff
				- rightOff - 5, pOff + size.y - 7);
		gc.drawLine(pOff + leftOff + 4, pOff + size.y - 7, pOff + size.x
				- leftOff - rightOff - 6, pOff + 5);
		gc.drawLine(pOff + leftOff + 5, pOff + size.y - 7, pOff + size.x
				- leftOff - rightOff - 5, pOff + 5);
	}

	// 画出恢复按钮的图形
	private void drawRestoreImage(GC gc, Point size, int pOff) {
		gc.drawRectangle(pOff + leftOff + 3, pOff + 7, size.x - leftOff
				- rightOff - 11, size.y - 13);
		gc.drawLine(pOff + leftOff + 4, pOff + 8, pOff + size.x - leftOff
				- rightOff - 9, pOff + 8);
		gc.drawLine(pOff + leftOff + 6, pOff + 5, pOff + size.x - leftOff
				- rightOff - 7, pOff + 5);
		gc.drawLine(pOff + leftOff + 5, pOff + 4, pOff + size.x - leftOff
				- rightOff - 6, pOff + 4);
		gc.drawLine(pOff + size.x - leftOff - rightOff - 7, pOff + size.y - 9,
				pOff + size.x - leftOff - rightOff - 6, pOff + size.y - 9);
		gc.drawLine(pOff + size.x - leftOff - rightOff - 6, pOff + size.y - 10,
				pOff + size.x - leftOff - rightOff - 6, pOff + 5);
		gc.drawLine(pOff + leftOff + 5, pOff + 5, pOff + leftOff + 5, pOff + 6);
	}

	// 画出最大化按钮的图形
	private void drawMaximizeImage(GC gc, Point size, int pOff) {
		gc.drawRectangle(pOff + leftOff + 3, pOff + 4, size.x - leftOff
				- rightOff - 8, size.y - 10);
		gc.drawLine(pOff + leftOff + 4, pOff + 5, pOff + size.x - leftOff
				- rightOff - 6, pOff + 5);
	}

	// 画出最小化按钮的图形
	private void drawMinimizeImage(GC gc, Point size, int pOff) {
		gc.drawLine(pOff + leftOff + 4, pOff + size.y - 6, pOff + size.x
				- leftOff - rightOff - 5, pOff + size.y - 6);
		gc.drawLine(pOff + leftOff + 4, pOff + size.y - 7, pOff + size.x
				- leftOff - rightOff - 5, pOff + size.y - 7);
	}
}

⌨️ 快捷键说明

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