随笔 - 119  文章 - 3173  trackbacks - 0
<2006年12月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

交友莫独酒,茅台西凤游。
口干古井贡,心徜洋河流。
称多情杜康,趟无量双沟。
赞中华巍巍,无此不销愁。

常用链接

留言簿(68)

随笔分类(136)

随笔档案(122)

最新随笔

搜索

  •  

积分与排名

  • 积分 - 520462
  • 排名 - 93

最新评论

看到有位朋友在偶“为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, 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 }
posted on 2006-12-26 15:47 交口称赞 阅读(4675) 评论(6)  编辑  收藏 所属分类: Eclipse RCP SWTjava相关

FeedBack:
# re: 强!简洁!SWT实现任务栏图标(系统托盘),带菜单和ToolTip 2006-12-26 16:50 BeanSoft
哈..没想到当时找到的资料原来作者就是博主啊, 不过还有人转贴不留来源的.

看看偶当时做插件之前找的资料:

计划开发一个 Eclipse/Netbeans 最小化到托盘的插件
http://www.blogjava.net/beansoft/archive/2006/12/14/87654.aspx  回复  更多评论
  
# re: 强!简洁!SWT实现任务栏图标(系统托盘),带菜单和ToolTip 2006-12-26 17:36 交口称赞
呵呵,当年的csdn号。。。。。。。
我还提交的CSDN的F/Q里面了。
  回复  更多评论
  
# re: 强!简洁!SWT实现任务栏图标(系统托盘),带菜单和ToolTip 2006-12-27 21:46 wypicc
我会经常来学习地
呵呵  回复  更多评论
  
# re: 强!简洁!SWT实现任务栏图标(系统托盘),带菜单和ToolTip 2006-12-29 21:36 jrobot[匿名]
虽然偶不懂rcp但是偶知道 swt早就支持系统托盘了

不支持不能跨平台的代码- -  回复  更多评论
  
# re: 强!简洁!SWT实现任务栏图标(系统托盘),带菜单和ToolTip 2006-12-30 00:49 交口称赞
..........................................
这个例子跟RCP有关吗?
本来就是纯SWT的
拜托看仔细
而且这个例子也是“很早”就写出来了

拜托看看CSDN里面的原帖
我只是因为跟一个朋友讨论RCP怎么实现托盘才扒出来的  回复  更多评论
  
# re: 强!简洁!SWT实现任务栏图标(系统托盘),带菜单和ToolTip 2006-12-30 00:52 交口称赞
其实OS类现在看来是不必要的
那时候水平有限
瞎猫碰死老鼠,乱试

有时间我测试下去了OS的相关东西看能不能跨平台
而且现在java6本身就是支持系统托盘  回复  更多评论
  

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


网站导航: