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


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