在RCP中实现系统托盘功能
系统
2019-08-29 22:22:57
1345 0
在RCP中实现系统托盘功能
在RCP中加入系统托盘功能,类似msn和QQ。功能就不说了。
在RCP的WorkbenchWindowAdvisor的postWindowOpen()中创建系统托盘,另外要override preWindowShellClose()方法,否则按程序右上角的X,整个程序就会被关闭,重写后实现程序最小化的功能。
ApplicationWorkbenchWindowAdvisor.java
<!--CTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt--><style type="text/css"> <!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }--> </style> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <!-- ======================================================== --><!-- = Java Sourcecode to HTML automatically converted code = --><!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard markus@jave.de = --><!-- = Further information: http://www.java2html.de = -->
<!-- start source code -->
<!-- end source code -->
/**
*ApplicationWorkbenchWindowAdvisor.java
*/
package
com.mi.pwdgate;
import
org.eclipse.swt.graphics.Point;
import
org.eclipse.ui.IWorkbenchPreferenceConstants;
import
org.eclipse.ui.PlatformUI;
import
org.eclipse.ui.application.ActionBarAdvisor;
import
org.eclipse.ui.application.IActionBarConfigurer;
import
org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import
org.eclipse.ui.application.WorkbenchWindowAdvisor;
import
com.mi.pwdgate.util.SystemTrayMaster;
publicclass
ApplicationWorkbenchWindowAdvisor
extends
WorkbenchWindowAdvisor{
private
SystemTrayMastertrayMaster;
/*
*(non-Javadoc)
*
*@seeorg.eclipse.ui.application.WorkbenchWindowAdvisor#postWindowOpen()
*/
public
void
postWindowOpen(){
createSystemTray();
}
private
void
createSystemTray(){
trayMaster=
new
SystemTrayMaster();
trayMaster.createSystemTray();
}
/*
*(non-Javadoc)
*
*@seeorg.eclipse.ui.application.WorkbenchWindowAdvisor#preWindowShellClose()
*/
public
boolean
preWindowShellClose(){
trayMaster.minimizeWindow();
returnfalse
;
}
}
<!--CTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt--><style type="text/css"> <!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }--> </style> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <!-- ======================================================== --><!-- = Java Sourcecode to HTML automatically converted code = --><!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard markus@jave.de = --><!-- = Further information: http://www.java2html.de = -->
<!-- start source code -->
/**
*SystemTrayMaster.java
*/
package
com.mi.pwdgate.util;
import
org.eclipse.swt.SWT;
import
org.eclipse.swt.events.SelectionAdapter;
import
org.eclipse.swt.events.SelectionEvent;
import
org.eclipse.swt.events.SelectionListener;
import
org.eclipse.swt.graphics.Image;
import
org.eclipse.swt.widgets.Display;
import
org.eclipse.swt.widgets.Event;
import
org.eclipse.swt.widgets.Listener;
import
org.eclipse.swt.widgets.Menu;
import
org.eclipse.swt.widgets.MenuItem;
import
org.eclipse.swt.widgets.Shell;
import
org.eclipse.swt.widgets.Tray;
import
org.eclipse.swt.widgets.TrayItem;
import
org.eclipse.ui.PlatformUI;
/**
*@authormoneyice2006-5-9
*/
publicclass
SystemTrayMaster
implements
SelectionListener,Listener{
privatefinalclass
RestoreWindowListener
extends
SelectionAdapter{
public
void
widgetSelected(SelectionEvente){
restoreWindow();
}
}
private
Menumenu;
private
MenuItem[]menuItems=new MenuItem[0];
private
RestoreWindowListenerrestoreWindowListener;
public
SystemTrayMaster(){
this
.restoreWindowListener=
new
RestoreWindowListener();
}
//ClosestheApplication
protected
void
closeApplication(){
PlatformUI.getWorkbench().close();
}
//clickthetray
public
void
widgetSelected(SelectionEvente){
}
//doubleclickthetray
public
void
widgetDefaultSelected(SelectionEvente){
restoreWindow();
}
//Getssystemshell
private
ShellgetShell(){
return
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
}
//MinimizestheWindow
public
void
minimizeWindow(){
getShell().setMinimized(
true
);
getShell().setVisible(
false
);
}
//Restoresthewindow
protected
void
restoreWindow(){
Shellshell=PlatformUI.getWorkbench().getActiveWorkbenchWindow()
.getShell();
shell.open();
shell.setMinimized(
false
);
shell.forceActive();
shell.forceFocus();
}
public
void
showMenu(){
clearItems();
MenuItemopenItem;
MenuItemcloseItem;
openItem=
new
MenuItem(
this
.menu,SWT.PUSH);
closeItem=
new
MenuItem(
this
.menu,SWT.NONE);
closeItem.setText("Close");
closeItem.addSelectionListener(
new
SelectionAdapter(){
public
void
widgetSelected(SelectionEvente){
closeApplication();
}
});
this
.menuItems=
new
MenuItem[]{openItem,closeItem};
openItem.setText("OpenPasswordGate");
openItem.addSelectionListener(
this
.restoreWindowListener);
this
.menu.setVisible(
true
);
}
private
void
clearItems(){
for
(
int
i=0;i<
this
.menuItems.length;i++){
MenuItemitem=
this
.menuItems[i];
item.removeSelectionListener(
this
.restoreWindowListener);
this
.menuItems[i].dispose();
}
}
public
void
handleEvent(Eventevent){
showMenu();
}
public
void
createSystemTray(){
//Getssystemtray
Traytray=Display.getDefault().getSystemTray();
//Createstrayitem
TrayItemitem=
new
TrayItem(tray,SWT.NONE);
item.setText("PasswordGate");
item.setToolTipText("PasswordGate");
//Setsimagefortray
Imageimage=ImageShop.get("default");
item.setImage(image);
item.addSelectionListener(
this
);
item.addListener(SWT.MenuDetect,
this
);
menu=
new
Menu(getShell(),SWT.POP_UP);
}
}
ç¨åºæ§è¡åææ
在RCP中实现系统托盘功能
更多文章、技术交流、商务合作、联系博主
微信扫码或搜索:z360901061
微信扫一扫加我为好友
QQ号联系: 360901061
您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。
【本文对您有帮助就好】 元
喜欢作者