我們有些程序會(huì )想要托盤(pán)處顯示圖標,最小化到系統欄;關(guān)閉按鈕不關(guān)閉程序,也是最小化到系統欄;點(diǎn)擊托盤(pán)圖標激活窗口,通過(guò)托盤(pán)圖標的彈出菜單來(lái)退出程序。
本段代碼就是要完成這樣的功能,是 SWT 來(lái)實(shí)現的。
直接代碼給出,代碼中有較詳細的注釋?zhuān)f(shuō)明了本程序的功能及實(shí)現。文中的任務(wù)欄和系統欄應該知道是指哪一段吧,微軟就是這么定義的,用 spyxx 的 findwindow 窺探一下就知道了。
- package com.unmi;
- import org.eclipse.swt.*;
- import org.eclipse.swt.events.*;
- import org.eclipse.swt.graphics.*;
- import org.eclipse.swt.widgets.*;
- /**
- * SWT 3.0 開(kāi)始引入了 Tray,可以在系統欄放置你的程序圖標了
- * 本程序實(shí)現的功能有四:
- * 1. 點(diǎn)擊窗口的最小化或關(guān)閉按鈕都是隱藏窗口--任務(wù)欄里不顯示,不退出程序
- * 2. 窗口隱藏時(shí),任務(wù)欄無(wú)圖標,系統欄有圖標;窗口處于顯示狀態(tài)時(shí)則恰好相反
- * 3. 窗口隱藏時(shí)可通過(guò)單擊系統欄圖標或點(diǎn)擊系統欄的 "顯示窗口" 菜單顯示窗口
- * 4. 程序只能通過(guò)點(diǎn)擊系統欄的 "退出程序" 菜單項退出,窗口的 X 按鈕無(wú)效
- * @author Unmi
- *
- */
- public class TrayExample {
- public static void main(String[] args) {
- Display display = new Display();
- //禁用掉了最大化按鈕
- final Shell shell = new Shell(display,SWT.SHELL_TRIM ^ SWT.MAX);
- shell.setText("TrayExample");
- //取系統中預置的圖標,省得測試運行時(shí)還得加個(gè)圖標文件
- shell.setImage(display.getSystemImage(SWT.ICON_WORKING));
- //構造系統欄控件
- final Tray tray = display.getSystemTray();
- final TrayItem trayItem = new TrayItem(tray, SWT.NONE);
- //程序啟動(dòng)時(shí),窗口是顯示的,所以系統欄圖標隱藏
- 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 event) {
- toggleDisplay(shell, tray);
- }
- });
- trayMenu.setDefaultItem(showMenuItem);
- new MenuItem(trayMenu, SWT.SEPARATOR);
- //系統欄中的退出菜單,程序只能通過(guò)這個(gè)菜單退出
- MenuItem exitMenuItem = new MenuItem(trayMenu, SWT.PUSH);
- exitMenuItem.setText("退出程序(&x)");
- exitMenuItem.addSelectionListener(new SelectionAdapter() {
- public void widgetSelected(SelectionEvent event) {
- shell.dispose();
- }
- });
- //在系統欄圖標點(diǎn)擊鼠標右鍵時(shí)的事件,彈出系統欄菜單
- trayItem.addMenuDetectListener(new MenuDetectListener(){
- public void menuDetected(MenuDetectEvent e) {
- trayMenu.setVisible(true);
- }
- });
- trayItem.setImage(shell.getImage());
- //注冊窗口事件監聽(tīng)器
- shell.addShellListener(new ShellAdapter() {
- //點(diǎn)擊窗口最小化按鈕時(shí),窗口隱藏,系統欄顯示圖標
- public void shellIconified(ShellEvent e) {
- toggleDisplay(shell, tray);
- }
- //點(diǎn)擊窗口關(guān)閉按鈕時(shí),并不終止程序,而時(shí)隱藏窗口,同時(shí)系統欄顯示圖標
- public void shellClosed(ShellEvent e) {
- e.doit = false; //消耗掉原本系統來(lái)處理的事件
- toggleDisplay(shell, tray);
- }
- });
- shell.setSize(320, 240);
- center(shell);
- shell.open();
- while (!shell.isDisposed()) {
- if (!display.readAndDispatch())
- display.sleep();
- }
- display.dispose();
- }
- /**
- * 窗口是可見(jiàn)狀態(tài)時(shí),則隱藏窗口,同時(shí)把系統欄中圖標刪除
- * 窗口是隱藏狀態(tài)時(shí),則顯示窗口,并且在系統欄中顯示圖標
- * @param shell 窗口
- * @param tray 系統欄圖標控件
- */
- 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();
- }
- }
- /**
- * 窗口居中顯示
- * @param shell 要顯示的窗口
- */
- 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);
- }
- }
實(shí)現效果如下:


左圖是窗口顯示時(shí),系統欄中無(wú)圖標,而任務(wù)欄中有圖標。右圖是窗口隱藏時(shí),只有系統欄有圖標。
過(guò)后,看了翻譯軟件 LINGOES 靈格斯的表現形式是:
1. 任何時(shí)候系統欄都有圖標
2. 最小化按鈕不會(huì )隱藏窗口,只是最小化到任務(wù)欄
3. 關(guān)閉按鈕也是不會(huì )關(guān)閉程序,而是最小化到系統欄
4. 也是只能通過(guò)托盤(pán)圖標的彈出菜單項“退出” 來(lái)關(guān)閉程序
參考:http://www.eclipseworld.org/bbs/read-cec-tid-15458-fpage-9.html
但最后還留有一個(gè)問(wèn)題:如何實(shí)現窗口可見(jiàn)狀態(tài)時(shí),任務(wù)欄里什么都不顯示呢?
[版權聲明]本站內文章,如未標注 [轉載],均系原創(chuàng )或翻譯之作,本人 Unmi保留一切權利。本站原創(chuàng )及譯作未經(jīng)本人許可,不得用于商業(yè)用途及傳統媒體。網(wǎng)絡(luò )媒體可隨意轉載,或以此為基礎進(jìn)行演譯,但務(wù)必以鏈接形式注明原始出處和作者信息,否則屬于侵權行為。另對本站轉載他處文章,俱有說(shuō)明,如有侵權請聯(lián)系本人,本人將會(huì )在第一時(shí)間刪除侵權文章。及此說(shuō)明,重之之重。

