?1?
import?org.eclipse.swt.SWT;
?2?
import?org.eclipse.swt.events.ShellListener;
?3?
import?org.eclipse.swt.graphics.Image;
?4?
import?org.eclipse.swt.internal.win32.OS;
?5?
import?org.eclipse.swt.widgets.Display;
?6?
import?org.eclipse.swt.widgets.Event;
?7?
import?org.eclipse.swt.widgets.Listener;
?8?
import?org.eclipse.swt.widgets.Menu;
?9?
import?org.eclipse.swt.widgets.MenuItem;
10?
import?org.eclipse.swt.widgets.Shell;
11?
import?org.eclipse.swt.widgets.Tray;
12?
import?org.eclipse.swt.widgets.TrayItem;
13?
14?
public?class?MyTray?{
15?
????public?static?void?main(String[]?args)?{
16?
????????Display?display?=?new?Display();
17?
????????Shell?shell?=?new?Shell(display);
18?
????????//?下面兩句的效果是:在任務(wù)欄不顯示
19?
????????//?感謝CSDN會(huì)員:GhostValley(鬼谷)
20?
????????final?int?hWnd?=?shell.handle;
21?
????????OS.SetWindowLong(hWnd,?OS.GWL_EXSTYLE,?OS.WS_EX_CAPTIONOKBTN);
22?
????????Image?image?=?new?Image(display,?16,?16);
23?
????????final?Tray?tray?=?display.getSystemTray();
24?
????????if?(tray?==?null)?{
25?
????????????System.out.println("The?system?tray?is?not?available");
26?
????????}?else?{
27?
????????????final?TrayItem?item?=?new?TrayItem(tray,?SWT.NONE);
28?
????????????item.setToolTipText("SWT?TrayItem");
29?
????????????item.addListener(SWT.Show,?new?Listener()?{
30?
????????????????public?void?handleEvent(Event?event)?{
31?
????????????????????System.out.println("show");
32?
????????????????}
33?
????????????});
34?
????????????item.addListener(SWT.Hide,?new?Listener()?{
35?
????????????????public?void?handleEvent(Event?event)?{
36?
????????????????????System.out.println("hide");
37?
????????????????}
38?
????????????});
39?
????????????item.addListener(SWT.Selection,?new?Listener()?{
40?
????????????????public?void?handleEvent(Event?event)?{
41?
????????????????????System.out.println("selection");
42?
????????????????}
43?
????????????});
44?
????????????item.addListener(SWT.DefaultSelection,?new?Listener()?{
45?
????????????????public?void?handleEvent(Event?event)?{
46?
????????????????????System.out.println("default?selection");
47?
????????????????????//?show?main
48?
????????????????????Shell?s?=?event.display.getShells()[0];
49?
????????????????????s.setVisible(true);
50?
????????????????????s.setMinimized(false);
51?
????????????????}
52?
????????????});
53?
????????????final?Menu?menu?=?new?Menu(shell,?SWT.POP_UP);
54?
????????????for?(int?i?=?0;?i?<?8;?i++)?{
55?
????????????????MenuItem?mi?=?new?MenuItem(menu,?SWT.PUSH);
56?
????????????????mi.setText("Item"?+?i);
57?
????????????}
58?
????????????item.addListener(SWT.MenuDetect,?new?Listener()?{
59?
????????????????public?void?handleEvent(Event?event)?{
60?
????????????????????menu.setVisible(true);
61?
????????????????}
62?
????????????});
63?
????????????item.setImage(image);
64?
????????}
65?
????????shell.addShellListener(new?ShellListener()?{
66?
????????????public?void?shellDeactivated(org.eclipse.swt.events.ShellEvent?e)?{
67?
????????????}
68?
69?
????????????public?void?shellActivated(org.eclipse.swt.events.ShellEvent?e)?{
70?
????????????}
71?
72?
????????????public?void?shellClosed(org.eclipse.swt.events.ShellEvent?e)?{
73?
????????????}
74?
75?
????????????public?void?shellDeiconified(org.eclipse.swt.events.ShellEvent?e)?{
76?
????????????}
77?
78?
????????????public?void?shellIconified(org.eclipse.swt.events.ShellEvent?e)?{
79?
????????????????//?最小化時(shí)不顯示在任務(wù)欄
80?
????????????????//?感謝CSDN會(huì)員:pdvv(我愛花貓)
81?
????????????????((Shell)?e.getSource()).setVisible(false);
82?
????????????}
83?
????????});
84?
????????shell.setBounds(50,?50,?300,?200);
85?
????????shell.open();
86?
????????while?(!shell.isDisposed())?{
87?
????????????if?(!display.readAndDispatch())
88?
????????????????display.sleep();
89?
????????}
90?
????????image.dispose();
91?
????????display.dispose();
92?
????}
93?
}
這個(gè)程序可以在點(diǎn)擊最小化按鈕后, 主窗體最小化到系統(tǒng)托盤,? 再點(diǎn)擊托盤的圖標(biāo), 主窗體就恢復(fù)顯示. 是 Eclipse Tray 插件的原型. 代碼修改自 Eclipser.org 的官方展示代碼:

/**?*//*******************************************************************************
?*?Copyright?(c)?2000,?2005?IBM?Corporation?and?others.
?*?All?rights?reserved.?This?program?and?the?accompanying?materials
?*?are?made?available?under?the?terms?of?the?Eclipse?Public?License?v1.0
?*?which?accompanies?this?distribution,?and?is?available?at
?*?http://www.eclipse.org/legal/epl-v10.html
?*
?*?Contributors:
?*?IBM?Corporation?-?initial?API?and?implementation
?*******************************************************************************/

/**//*
?*?Tray?example?snippet:?place?an?icon?with?a?popup?menu?on?the?system?tray
?*
?*?For?a?list?of?all?SWT?example?snippets?see
?*?http://www.eclipse.org/swt/snippets/
?*?
?*?@since?3.0
?*/
import?org.eclipse.jface.dialogs.MessageDialog;
import?org.eclipse.swt.*;
import?org.eclipse.swt.events.ShellEvent;
import?org.eclipse.swt.events.ShellListener;
import?org.eclipse.swt.graphics.*;
import?org.eclipse.swt.widgets.*;


public?class?Snippet143?
{


????public?static?void?main(String[]?args)?
{
????????Display?display?=?new?Display();
????????final?Shell?shell?=?new?Shell(display);
????????shell.setText("Eclipse?SDK");
????????
????????shell.setImage(new?org.eclipse.swt.graphics.Image(Display.getCurrent(),
????????????????"icons/sample.gif"));

????????Image?image?=?shell.getImage();
????????final?Tray?tray?=?display.getSystemTray();

????????if?(tray?==?null)?
{
????????????System.out.println("The?system?tray?is?not?available");

????????}?else?
{
????????????final?TrayItem?item?=?new?TrayItem(tray,?SWT.NONE);
????????????item.setVisible(false);
????????????item.setToolTipText("SWT?TrayItem");

????????????item.addListener(SWT.Show,?new?Listener()?
{

????????????????public?void?handleEvent(Event?event)?
{
????????????????????System.out.println("show");
????????????????}
????????????});

????????????item.addListener(SWT.Hide,?new?Listener()?
{

????????????????public?void?handleEvent(Event?event)?
{
????????????????????System.out.println("hide");
????????????????}
????????????});

????????????item.addListener(SWT.Selection,?new?Listener()?
{

????????????????public?void?handleEvent(Event?event)?
{
????????????????????System.out.println("selection");
????????????????????toggleDisplay(shell,?tray);
????????????????}
????????????});

????????????item.addListener(SWT.DefaultSelection,?new?Listener()?
{

????????????????public?void?handleEvent(Event?event)?
{
????????????????????System.out.println("default?selection");

????????????????}
????????????});
????????????final?Menu?menu?=?new?Menu(shell,?SWT.POP_UP);
????????????MenuItem?mi?=?new?MenuItem(menu,?SWT.PUSH);
????????????mi.setText("Show?"?+?shell.getText());

????????????mi.addListener(SWT.Selection,?new?Listener()?
{

????????????????public?void?handleEvent(Event?event)?
{
????????????????????toggleDisplay(shell,?tray);
????????????????}
????????????});
????????????menu.setDefaultItem(mi);
????????????
????????????new?MenuItem(menu,?SWT.SEPARATOR);
????????????
????????????mi?=?new?MenuItem(menu,?SWT.PUSH);
????????????mi.setText("&About");
????????????

????????????mi.addListener(SWT.Selection,?new?Listener()?
{

????????????????public?void?handleEvent(Event?event)?
{
????????????????????MessageDialog.openInformation(
????????????????????????????shell,
????????????????????????????"EclipseTray?Plug-in?1.0b?by?BeanSoft",
????????????????????????????"This?plugin?will?minimize?Eclipse?window?to?tray.\n"?+
????????????????????????????"Author:?beansoft@126.com\n"?+
????????????????????????????"Eclipse?version?tested:?3.2?win32\nDate:?2006-12-14");
????????????????}
????????????});
????????????

????????????item.addListener(SWT.MenuDetect,?new?Listener()?
{

????????????????public?void?handleEvent(Event?event)?
{
????????????????????menu.setVisible(true);
????????????????}
????????????});
????????????item.setImage(image);
????????}


????????shell.addShellListener(new?ShellListener()?
{


????????????public?void?shellActivated(ShellEvent?e)?
{
????????????????//?TODO?Auto-generated?method?stub
????????????????
????????????}


????????????public?void?shellClosed(ShellEvent?e)?
{
????????????????//?TODO?Auto-generated?method?stub
????????????????
????????????}


????????????public?void?shellDeactivated(ShellEvent?e)?
{
????????????????//?TODO?Auto-generated?method?stub
????????????????
????????????}


????????????public?void?shellDeiconified(ShellEvent?e)?
{
????????????????//?TODO?Auto-generated?method?stub
????????????????
????????????}


????????????public?void?shellIconified(ShellEvent?e)?
{
????????????????toggleDisplay(shell,?tray);
????????????}
????????????
????????});
????????
????????shell.setBounds(50,?50,?300,?200);
????????shell.open();

????????while?(!shell.isDisposed())?
{
????????????if?(!display.readAndDispatch())
????????????????display.sleep();
????????}
????????image.dispose();
????????display.dispose();
????}
????

????/**?*//**
?????*?Toggle?the?display?of?current?shell?and?tray?icon.
?????*?
?????*?@author?BeanSoft(beansoft@126.com)
?????*?@param?shell
?????*?@param?tray
?????*/

????protected?static?void?toggleDisplay(Shell?shell,?Tray?tray)?
{

????????try?
{
????????????shell.setVisible(!shell.isVisible());
????????????tray.getItem(0).setVisible(!shell.isVisible());
????????????//?TODO?Get?focus???!?Like?QQ?
//????????????shell.setFocus();
//????????????shell.setActive();

????????}?catch?(Exception?e)?
{
????????????e.printStackTrace();
????????????//?TODO:?handle?exception
????????}
????}
}
