看到有位朋友在偶“为RCP程序增加关闭提示和关闭到系统托盘”的文章里面的留言,忍不住把自己以前N老的一篇帖子扒出来了。
此文可能是当初最早最完善的SWT实现任务栏图标,带菜单、带ToolTip的完整的例子。
期间还有CSDN会员GhostValley;pdvv的指教。
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 }
posted on 2006-12-26 15:47
交口称赞 阅读(4702)
评论(6) 编辑 收藏 所属分类:
Eclipse RCP SWT 、
java相关