love fish大鹏一曰同风起,扶摇直上九万里

常用链接

统计

积分与排名

friends

link

最新评论

SWT实现任务栏图标(系统托盘),带菜单和ToolTip(轉)

 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, 1616);
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(5050300200);
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.
 * 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(
5050300200);
        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
        }

    }

}

posted on 2007-01-12 15:33 liaojiyong 阅读(1787) 评论(0)  编辑  收藏 所属分类: Eclipse


只有注册用户登录后才能发表评论。


网站导航: