我们有些程序会想要托盘处显示图标,最小化到系统栏;关闭按钮不关闭程序,也是最小化到系统栏;点击托盘图标激活窗口,通过托盘图标的弹出菜单来退出程序。
本段代码就是要完成这样的功能,是 SWT 来实现的。
直接代码给出,代码中有较详细的注释,说明了本程序的功能及实现。文中的任务栏和系统栏应该知道是指哪一段吧,微软就是这么定义的,用 spyxx 的 findwindow 窥探一下就知道了。
package tray;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuDetectEvent;
import org.eclipse.swt.events.MenuDetectListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
public class TrayApp {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("最小化到系統托盤");
//取消系統中預設的圖標,預設圖標在托盤不能顯示
shell.setImage(display.getSystemImage(SWT.ICON_INFORMATION));
//構建系統托盤
final Tray tray = display.getSystemTray();
final TrayItem trayItem = new TrayItem(tray, SWT.NONE);
//設置在托盤中顯示的程序圖標
trayItem.setImage(display.getSystemImage(SWT.ICON_INFORMATION));
//程序啟動時,窗口是顯示的,所以托盤圖標隱藏
trayItem.setVisible(false);
trayItem.setToolTipText(shell.getText());
trayItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
toggleDisplay(shell, tray);
}
});
final Menu trayMenu = new Menu(shell, SWT.POP_UP);
MenuItem showMenuItem = new MenuItem(trayMenu, SWT.PUSH);
showMenuItem.setText("顯示窗口(&s)");
//顯示窗口,并隱藏托盤圖標
showMenuItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
toggleDisplay(shell, tray);
}
});
trayMenu.setDefaultItem(showMenuItem);
new MenuItem(trayMenu, SWT.SEPARATOR);
//托盤中的退出菜單,程式只能通過這個菜單退出
MenuItem exitMenuItem = new MenuItem(trayMenu, SWT.PUSH);
exitMenuItem.setText("退出程式(&x)");
exitMenuItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
shell.dispose();
}
});
//在托盤圖標點擊鼠標右鍵時的事件,彈出系統菜單
trayItem.addMenuDetectListener(new MenuDetectListener() {
public void menuDetected(MenuDetectEvent e) {
trayMenu.setVisible(true);
}
});
//注冊窗口監聽
shell.addShellListener(new ShellAdapter() {
//點擊窗口最小化按鈕時,窗口隱藏,托盤中顯示圖標
public void shellIconified(ShellEvent e) {
toggleDisplay(shell, tray);
}
//點擊窗口關閉時,并不終止程序,而是隱藏窗口,同時托盤中顯示圖標
public void shellClosed(ShellEvent e) {
e.doit = false; //取消關閉操作
toggleDisplay(shell, tray);
}
});
shell.setSize(320, 240);
center(shell);
shell.open();
while(!shell.isDisposed()) {
if(!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
/**
* 窗口是可見狀態時,則隱藏窗口,在托盤中顯示程序圖標
* 窗口是隱藏狀態時,則顯示窗口,將托盤中圖標刪除
*/
private static void toggleDisplay(Shell shell, Tray tray) {
try {
shell.setVisible(!shell.isVisible()); //控制窗口顯示
tray.getItem(0).setVisible(!shell.isVisible()); //控制托盤圖標顯示
//如果窗口是顯示狀態
if(shell.getVisible()) {
shell.setMinimized(false); //阻止窗口最小化
shell.setActive(); //激活窗口
}
} catch(Exception e) {
e.printStackTrace();
}
}
/**
* 窗口居中顯示
*/
private static void center(Shell shell) {
Monitor monitor = shell.getMonitor();
Rectangle bounds = monitor.getBounds();
Rectangle rect = shell.getBounds();
int x = bounds.x + (bounds.width - rect.width)/2;
int y = bounds.y + (bounds.height - rect.height)/2;
shell.setLocation(x, y);
}
}
实现效果如下:
左图是窗口显示时,系统栏中无图标,而任务栏中有图标。右图是窗口隐藏时,只有系统栏有图标。
过后,看了翻译软件 LINGOES 灵格斯的表现形式是:
1. 任何时候系统栏都有图标
2. 最小化按钮不会隐藏窗口,只是最小化到任务栏
3. 关闭按钮也是不会关闭程序,而是最小化到系统栏
4. 也是只能通过托盘图标的弹出菜单项“退出” 来关闭程序
原文地址:http://www.blogjava.net/Unmi/archive/2008/03/23/188040.html