国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
SWT 中實(shí)現(xiàn)最小化到托盤圖標(biāo),并只能通過(guò)托盤的彈出菜單關(guān)閉程序

我們有些程序會(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 窺探一下就知道了。

  1. package com.unmi;  
  2.   
  3. import org.eclipse.swt.*;  
  4. import org.eclipse.swt.events.*;  
  5. import org.eclipse.swt.graphics.*;  
  6. import org.eclipse.swt.widgets.*;  
  7.   
  8. /** 
  9.  * SWT 3.0 開始引入了 Tray,可以在系統(tǒng)欄放置你的程序圖標(biāo)了 
  10.  * 本程序?qū)崿F(xiàn)的功能有四: 
  11.  * 1. 點(diǎn)擊窗口的最小化或關(guān)閉按鈕都是隱藏窗口--任務(wù)欄里不顯示,不退出程序 
  12.  * 2. 窗口隱藏時(shí),任務(wù)欄無(wú)圖標(biāo),系統(tǒng)欄有圖標(biāo);窗口處于顯示狀態(tài)時(shí)則恰好相反 
  13.  * 3. 窗口隱藏時(shí)可通過(guò)單擊系統(tǒng)欄圖標(biāo)或點(diǎn)擊系統(tǒng)欄的 "顯示窗口" 菜單顯示窗口 
  14.  * 4. 程序只能通過(guò)點(diǎn)擊系統(tǒng)欄的 "退出程序" 菜單項(xiàng)退出,窗口的 X 按鈕無(wú)效 
  15.  * @author Unmi 
  16.  * 
  17.  */  
  18. public class TrayExample {  
  19.   
  20.     public static void main(String[] args) {  
  21.         Display display = new Display();  
  22.           
  23.         //禁用掉了最大化按鈕  
  24.         final Shell shell = new Shell(display,SWT.SHELL_TRIM ^ SWT.MAX);  
  25.         shell.setText("TrayExample");  
  26.   
  27.         //取系統(tǒng)中預(yù)置的圖標(biāo),省得測(cè)試運(yùn)行時(shí)還得加個(gè)圖標(biāo)文件  
  28.         shell.setImage(display.getSystemImage(SWT.ICON_WORKING));  
  29.   
  30.         //構(gòu)造系統(tǒng)欄控件  
  31.         final Tray tray = display.getSystemTray();  
  32.         final TrayItem trayItem = new TrayItem(tray, SWT.NONE);  
  33.           
  34.         //程序啟動(dòng)時(shí),窗口是顯示的,所以系統(tǒng)欄圖標(biāo)隱藏  
  35.         trayItem.setVisible(false);  
  36.         trayItem.setToolTipText(shell.getText());  
  37.                       
  38.         trayItem.addSelectionListener(new SelectionAdapter() {  
  39.             public void widgetSelected(SelectionEvent e) {  
  40.                 toggleDisplay(shell, tray);  
  41.             }  
  42.         });  
  43.   
  44.         final Menu trayMenu = new Menu(shell, SWT.POP_UP);  
  45.         MenuItem showMenuItem = new MenuItem(trayMenu, SWT.PUSH);  
  46.         showMenuItem.setText("顯示窗口(&s)");  
  47.           
  48.         //顯示窗口,并隱藏系統(tǒng)欄中的圖標(biāo)  
  49.         showMenuItem.addSelectionListener(new SelectionAdapter() {  
  50.             public void widgetSelected(SelectionEvent event) {  
  51.                 toggleDisplay(shell, tray);  
  52.             }  
  53.         });  
  54.           
  55.         trayMenu.setDefaultItem(showMenuItem);  
  56.   
  57.         new MenuItem(trayMenu, SWT.SEPARATOR);  
  58.   
  59.         //系統(tǒng)欄中的退出菜單,程序只能通過(guò)這個(gè)菜單退出  
  60.         MenuItem exitMenuItem = new MenuItem(trayMenu, SWT.PUSH);  
  61.         exitMenuItem.setText("退出程序(&x)");  
  62.   
  63.         exitMenuItem.addSelectionListener(new SelectionAdapter() {  
  64.             public void widgetSelected(SelectionEvent event) {  
  65.                 shell.dispose();  
  66.             }  
  67.         });  
  68.   
  69.         //在系統(tǒng)欄圖標(biāo)點(diǎn)擊鼠標(biāo)右鍵時(shí)的事件,彈出系統(tǒng)欄菜單  
  70.         trayItem.addMenuDetectListener(new MenuDetectListener(){  
  71.             public void menuDetected(MenuDetectEvent e) {  
  72.                 trayMenu.setVisible(true);  
  73.             }  
  74.         });  
  75.   
  76.         trayItem.setImage(shell.getImage());  
  77.   
  78.         //注冊(cè)窗口事件監(jiān)聽器  
  79.         shell.addShellListener(new ShellAdapter() {  
  80.               
  81.             //點(diǎn)擊窗口最小化按鈕時(shí),窗口隱藏,系統(tǒng)欄顯示圖標(biāo)  
  82.             public void shellIconified(ShellEvent e) {  
  83.                 toggleDisplay(shell, tray);  
  84.             }  
  85.               
  86.             //點(diǎn)擊窗口關(guān)閉按鈕時(shí),并不終止程序,而時(shí)隱藏窗口,同時(shí)系統(tǒng)欄顯示圖標(biāo)  
  87.             public void shellClosed(ShellEvent e) {  
  88.                 e.doit = false//消耗掉原本系統(tǒng)來(lái)處理的事件  
  89.                 toggleDisplay(shell, tray);  
  90.             }  
  91.         });  
  92.   
  93.         shell.setSize(320240);  
  94.         center(shell);  
  95.         shell.open();  
  96.         while (!shell.isDisposed()) {  
  97.             if (!display.readAndDispatch())  
  98.                 display.sleep();  
  99.         }  
  100.         display.dispose();  
  101.     }  
  102.   
  103.     /** 
  104.      * 窗口是可見狀態(tài)時(shí),則隱藏窗口,同時(shí)把系統(tǒng)欄中圖標(biāo)刪除 
  105.      * 窗口是隱藏狀態(tài)時(shí),則顯示窗口,并且在系統(tǒng)欄中顯示圖標(biāo) 
  106.      * @param shell 窗口 
  107.      * @param tray 系統(tǒng)欄圖標(biāo)控件 
  108.      */  
  109.     private static void toggleDisplay(Shell shell, Tray tray) {  
  110.         try {  
  111.             shell.setVisible(!shell.isVisible());  
  112.             tray.getItem(0).setVisible(!shell.isVisible());  
  113.             if (shell.getVisible()) {  
  114.                 shell.setMinimized(false);  
  115.                 shell.setActive();  
  116.             }  
  117.         } catch (Exception e) {  
  118.             e.printStackTrace();  
  119.         }  
  120.     }  
  121.       
  122.     /** 
  123.      * 窗口居中顯示 
  124.      * @param shell 要顯示的窗口 
  125.      */  
  126.     private static void center(Shell shell){  
  127.         Monitor monitor = shell.getMonitor();  
  128.         Rectangle bounds = monitor.getBounds ();  
  129.         Rectangle rect = shell.getBounds ();  
  130.         int x = bounds.x + (bounds.width - rect.width) / 2;  
  131.         int y = bounds.y + (bounds.height - rect.height) / 2;  
  132.         shell.setLocation (x, y);  
  133.     }  
  134. }  


實(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ō)明,重之之重。
posted on 2008-03-23 16:05 隔葉黃鶯 閱讀(796) 評(píng)論(3)  編輯  收藏
# 

Feedback

# re: SWT 中實(shí)現(xiàn)最小化到托盤圖標(biāo),并只能通過(guò)托盤的彈出菜單關(guān)閉程序2008-03-24 23:45千里冰封
你寫的窗口如果是繼承自JDialog的話,則任務(wù)欄什么也不會(huì)顯示  回復(fù)  更多評(píng)論
  

# re: SWT 中實(shí)現(xiàn)最小化到托盤圖標(biāo),并只能通過(guò)托盤的彈出菜單關(guān)閉程序2008-03-24 23:46千里冰封
# while (!shell.isDisposed()) {
# if (!display.readAndDispatch())
# display.sleep();
# }
怎么像MFC的事件循環(huán)哦,
唉,SWT就是和SWING沒(méi)法比了  回復(fù)  更多評(píng)論
  

# re: SWT 中實(shí)現(xiàn)最小化到托盤圖標(biāo),并只能通過(guò)托盤的彈出菜單關(guān)閉程序2008-03-25 10:38隔葉黃鶯
SWT的程序如果繼承自 JDialog 就不倫不類了,也沒(méi)這么試過(guò),用SWT一般也不建議這么做,雖然SWT也提供了 SWT-AWT 相應(yīng)類

SWT 確實(shí)很多東西太像 MFC 的,比如
窗口構(gòu)造方式:
final Shell shell = new Shell(display,SWT.SHELL_TRIM ^ SWT.MAX);

SWT中很多變量在MFC都有相應(yīng)的全局常量對(duì)應(yīng)

資源的獲取方式:
display.getSystemImage(SWT.ICON_WORKING

不過(guò)SWT可還是比MFC簡(jiǎn)單多了。

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
SWT復(fù)雜托盤程序的實(shí)現(xiàn)
在RCP中實(shí)現(xiàn)系統(tǒng)托盤功能
"顯示桌面"圖標(biāo)丟失怎么辦?
Outlook2007 最小化時(shí)隱藏到托盤區(qū)
VB如何實(shí)現(xiàn)托盤圖標(biāo)
什么是Windows任務(wù)欄
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服