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         // 下面两句的效果是:在任务栏不显示
19         // 感谢CSDN会员: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                 // 最小化时不显示在任务栏
80                 // 感谢CSDN会员: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 }
这个程序可以在点击最小化按钮后, 主窗体最小化到系统托盘,  再点击托盘的图标, 主窗体就恢复显示. 是 Eclipse Tray 插件的原型. 代码修改自 Eclipser.org 的官方展示代码:

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

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


 public class Snippet143
public class Snippet143  {
{


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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


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


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


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


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


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

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

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

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

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

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