/**
*
*/
package com.digitalcontent.communication;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.graphics.Rectangle;
/**
* @author 廖龍龍
*
* 2008-7-26
*/
public class WorkSpace {
private Shell mainwindowShell = null;
private HookSysTray sysTray=null;
/**
* @param args
*/
public static void main(String[] args) {
WorkSpace thisClass = new WorkSpace();
thisClass.openmain();
}
/**
* This method initializes sShell
*/
private void createSShell() {
mainwindowShell = new Shell();
mainwindowShell.setText("主窗口");
mainwindowShell.setSize(new Point(260, 200));
mainwindowShell.setLayout(new GridLayout());
//設(shè)置窗口在屏幕上的初始位置,在屏幕的中間顯示主窗口
Monitor primary = mainwindowShell.getMonitor();
Rectangle bounds = primary.getBounds();
Rectangle rect =mainwindowShell.getBounds();
// 獲取屏幕高度(screenH)和寬度(screenW)
// int screenH = bounds.height;
// int screenW = bounds.width;
// System.out.println("屏幕的分辨率為:"+screenW+"*"+screenH);
int x = bounds.x + (bounds.width - rect.width) / 2;
int y = bounds.y + (bounds.height - rect.height) / 2;
if (x < 0)
x = 0;
if (y < 0)
y = 0;
//定位對象窗口坐標(biāo)
mainwindowShell.setLocation(x, y);
//關(guān)閉主窗口
mainwindowShell
.addShellListener(new org.eclipse.swt.events.ShellAdapter() {
public void shellClosed(org.eclipse.swt.events.ShellEvent e) {
//重載關(guān)閉對話框方法
closeWindow();
}
});
}
public void openmain(){
Display display = Display.getDefault();
createSShell();
//顯示系統(tǒng)托盤
sysTray=new HookSysTray();
sysTray.createSysTray(mainwindowShell);
//顯示主窗口
mainwindowShell.open();
// 當(dāng)窗體未被關(guān)閉時執(zhí)行循環(huán)體內(nèi)的代碼
while (!mainwindowShell.isDisposed()) {
// 如果未發(fā)生事件,通過sleep方法進(jìn)行監(jiān)視事件隊列
if (!display.readAndDispatch())
display.sleep();
}
}
public void closeWindow(){
sysTray.trayDispose();// 釋放托盤及其相關(guān)資源
// Display display =mainwindowShell.getDisplay();
// display.dispose();// 釋放底層的資源
System.exit(1);//退出主程序
}
}
/**
* 創(chuàng)建并管理系統(tǒng)托盤
* 在托盤圖標(biāo)上單擊鼠標(biāo)右鍵彈出托盤菜單,雙擊顯示程序主界面,單擊(左鍵)顯示氣泡提示文本
*/
package com.digitalcontent.communication;
/**
* @author 廖龍龍
*
* 2008-8-30
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolTip;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import java.util.Timer;
import java.util.TimerTask;
public class HookSysTray {
private TrayItem trayItem;// 托盤項
private Image trayicon;// 托盤圖標(biāo)
//構(gòu)造方法
public HookSysTray() {
}
/**
* @param mainshell為主窗口對應(yīng)的shell對象
*/
public void createSysTray(final Shell mainshell) {
trayItem = initTrayItem(mainshell);//初始化托盤
if (trayItem != null) {
trayMinimize(mainshell);// 最小化主程序到系統(tǒng)托盤
}
}
/**
* 初始化托盤項目的文字和圖標(biāo)
* @param mainShell 主程序窗口對象
*/
private TrayItem initTrayItem(final Shell mainshell) {
// 獲取系統(tǒng)托盤
final Tray tray =mainshell.getDisplay().getSystemTray();
// 在某些平臺上,可能不存在或不支持系統(tǒng)托盤。需檢查當(dāng)前的系統(tǒng)是否支持系統(tǒng)托盤。
if (tray == null) {
System.out.println("當(dāng)前系統(tǒng)不支持系統(tǒng)托盤");
return null;
}
//當(dāng)系統(tǒng)支持系統(tǒng)托盤時
else {
trayItem = new TrayItem(tray, SWT.NONE);// 創(chuàng)建托盤項
Display maindisplay = mainshell.getDisplay();
trayicon = new Image(maindisplay, "icons//19-1.bmp");// 創(chuàng)建托盤圖像
trayItem.setImage(trayicon);// 設(shè)置托盤圖標(biāo)
// 添加托盤右鍵菜單
fillTrayItem(mainshell);
// 鼠標(biāo)放在托盤圖標(biāo)上時,顯示的提示文本信息
trayItem.setToolTipText("廖龍龍");
// 顯示托盤氣泡提示文本
final ToolTip tip = new ToolTip(mainshell,SWT.BALLOON | SWT.ICON_INFORMATION);
//自動隱藏氣泡提示文本
tip.setAutoHide(true);
//設(shè)置提示信息
tip.setMessage("3D數(shù)字內(nèi)容產(chǎn)品開發(fā)");
tip.setText("歡迎使用");
trayItem.setToolTip(tip);
//單擊選中托盤圖標(biāo)的時候,顯示氣泡提示
trayItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
// 顯示氣泡提示
tip.setVisible(true);
// 采用定時器,自動關(guān)閉氣泡提示文本
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
// 非用戶界面線程不能直接操作用戶界面線程
// 要想在另外一個線程中嘗試修改用戶界面,應(yīng)采用以下方法:
mainshell.getDisplay().syncExec(new Runnable() {
public void run() {
// 數(shù)秒后處理氣泡提示文本
// tip.setVisible(false);// 使當(dāng)前氣泡提示文本不可見
tip.setAutoHide(true);// 自動隱藏
// System.out.println("氣泡提示文本消失了!");
}
});
timer.cancel();
}
}, 2 * 3 * 100);
//單擊托盤圖標(biāo),圖標(biāo)不斷閃爍
trayIconFlicker(mainshell);
}
});
// 雙擊托盤,顯示程序主界面并恢復(fù)為正常大小
trayItem.addListener(SWT.DefaultSelection, new Listener() {
public void handleEvent(Event event) {
mainshell.setVisible(true);
mainshell.setMinimized(false);
}
});
return trayItem;
}
}
/**
* 接收到新消息時,托盤圖標(biāo)閃爍
* @param minute 執(zhí)行一次操作的時間間隔
* TODO 單擊系統(tǒng)托盤圖標(biāo),都會執(zhí)行這個方法,導(dǎo)致圖標(biāo)的閃爍不斷加快
* 考慮采用全局變量來實(shí)現(xiàn)計時器類final Timer timer
*/
private void trayIconFlicker(final Shell mainshell) {
int minute=1;//執(zhí)行一次操作的時間間隔
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
// 非用戶界面線程不能直接操作用戶界面線程
// 要想在另外一個線程中嘗試修改用戶界面,應(yīng)采用以下方法:
mainshell.getDisplay().syncExec(new Runnable() {
public void run() {
// 每個200ms間隔顯示托盤圖標(biāo),以實(shí)現(xiàn)圖標(biāo)閃爍的效果
trayItem.setImage(null);//設(shè)置托盤圖標(biāo)為空
try {
Thread.sleep(200);
} catch (InterruptedException e) {
}
trayItem.setImage(trayicon);//設(shè)置托盤圖標(biāo)
}
});
}
},0,minute * 6 * 100);
}
// 3. 接收到新消息的時候,自動彈出沒有圖標(biāo)的消息提示框,26ms之后消失
/**
* 最小化程序到托盤
* @param mainShell 主程序窗口對象
*/
private void trayMinimize(final Shell mainShell) {
mainShell.addShellListener(new ShellAdapter() {
// 最小化時不顯示在任務(wù)欄
public void shellIconified(ShellEvent e) {
// 主程序窗口不可見
mainShell.setVisible(false);
}
});
//用戶雙擊托盤圖標(biāo),顯示主程序窗口并恢復(fù)為正常大小
// trayItem.addListener(SWT.Selection, new Listener() {
// public void handleEvent(Event event) {
// if (mainShell.isVisible()==false) {
// mainShell.setVisible(true);
// mainShell.setMinimized(false);
// }
// }
// });
}
/**
* 構(gòu)造托盤菜單項
* @param mainShell 主程序窗口對象
*/
private Menu fillTrayItem(final Shell mainshell) {
final Menu menu = new Menu(mainshell, SWT.POP_UP);
//主程序退出菜單項
final MenuItem exitItem = new MenuItem(menu, SWT.PUSH);//主程序退出菜單
exitItem.setText("退出程序");
exitItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
MessageBox box = new MessageBox(mainshell, SWT.YES | SWT.NO
| SWT.ICON_QUESTION);
box.setMessage("退出主程序?");
int response = box.open();//打開消息對話框
if (response == SWT.YES) {
trayDispose();//釋放托盤相關(guān)的資源
System.exit(1);//退出主程序
}
}
});
new MenuItem(menu, SWT.SEPARATOR);// 分割條
//平臺開發(fā)者信息
final MenuItem authorItem = new MenuItem(menu, SWT.PUSH);// 開發(fā)者信息菜單
authorItem.setText("平臺開發(fā)者");
authorItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
MessageBox box = new MessageBox(mainshell, SWT.OK);
box.setMessage("該平臺由廖龍龍設(shè)計并開發(fā)");
box.open();
}
});
MenuItem menuItemMaximize = new MenuItem(menu, SWT.PUSH);// 窗口恢復(fù)菜單
menuItemMaximize.setText("恢復(fù)主窗口");
menuItemMaximize.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
if (mainshell.isVisible() == false) {
mainshell.setVisible(true);
mainshell.setMinimized(false);
}
}
});
MenuItem menuItemMinimize = new MenuItem(menu, SWT.PUSH);// 最小化菜單
menuItemMinimize.setText("最小化窗口到托盤");
menuItemMinimize.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
mainshell.setMinimized(true);
}
});
// 當(dāng)前菜單項不可用時,顯示為灰色
trayItem.addListener(SWT.MenuDetect, new Listener() {
public void handleEvent(Event event) {
menu.setVisible(true);
if (mainshell.isVisible()) {
menu.getItem(3).setEnabled(false);
menu.getItem(4).setEnabled(true);
}
else if(mainshell.getMinimized()==true){
menu.getItem(3).setEnabled(true);
menu.getItem(4).setEnabled(false);
}
}
});
// 選中托盤圖標(biāo)并單擊鼠標(biāo)右鍵,顯示托盤菜單項
// trayItem.addSelectionListener(new SelectionAdapter() {
// public void widgetSelected(Event e) { // 使托盤菜單可見
// if ((menu.getVisible() == false) && (e.button == 3)) {
// menu.setVisible(true);
// }
// }
// });
return menu;
}
/**
* 釋放托盤及其相關(guān)資源
*/
public void trayDispose() {
// 釋放系統(tǒng)托盤
if (trayItem != null)
trayItem.dispose();
// 釋放創(chuàng)建的圖像資源(系統(tǒng)托盤圖標(biāo))
if (trayicon != null) {
trayicon.dispose();
}
}
}
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點(diǎn)擊舉報。