windowstrayicon.java

来自「OSGI这是一个中间件,与UPNP齐名,是用于移植到嵌入式平台之上」· Java 代码 · 共 682 行 · 第 1/2 页

JAVA
682
字号
/*** * Windows Tray Icon * ----------------- * * Written by Jan Struyf * *  jan.struyf@cs.kuleuven.ac.be *  http://jeans.studentenweb.org/java/trayicon/trayicon.html * * Please mail me if you *	- 've found bugs *	- like this program *	- don't like a particular feature *	- would like something to be modified * * I always give it my best shot to make a program useful and solid, but * remeber that there is absolutely no warranty for using this program as * stated in the following terms: * * THERE IS NO WARRANTY FOR THIS PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE * LAW. THE COPYRIGHT HOLDER AND/OR OTHER PARTIES WHO MAY HAVE MODIFIED THE * PROGRAM, PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS * TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE * PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, * REPAIR OR CORRECTION. * * IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL ANY COPYRIGHT HOLDER, * OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM, * BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR * CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE * PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED * INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE * PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER * PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * May the Force be with you... Just compile it & use it! */package com.jeans.trayicon;import java.awt.image.*;import java.awt.event.*;import java.awt.*;import java.util.*;/** * WindowsTrayIcon * A Java Implementation for showing icons in the Windows System Tray * * Written by Jan Struyf *	(jan.struyf@cs.kuleuven.ac.be) *	(http://ace.ulyssis.org/~jeans) * * Instantiate this class for each icon * This file comes with native code in TRAYICON.DLL * The DLL should go in C:/WINDOWS/SYSTEM or in your current directory */public class WindowsTrayIcon {    public final static String TRAY_VERSION = "1.7.9b";    private static TrayIconKeeper m_Keeper;    private static TrayDummyComponent m_Dummy;    private static MouseListener m_MouseHook;    private static Window m_CurrentWindow;/**************************************************************************************************************** *                                                                                                              * * Initialisation / Termination                                                                                 * *                                                                                                              * ****************************************************************************************************************//** * Init native library - call this method in the main() method of your app * * Param appName = the title for the hidden window *	Each app has it's own hidden window that receives the mouse/menu messages for it's Tray Icons *      The window title is used by sendWindowsMessage() and isRunning() to identify an app */	public static void initTrayIcon(String appName) {		initTrayIcon(appName, new WindowsTrayIcon());	}	/** * Free all native resources - call this method before System.exit() */	public static void cleanUp() {		if (m_Keeper != null) {			m_Keeper.doNotify();			m_Keeper = null;		}		termTrayIcon();	}	/**************************************************************************************************************** *                                                                                                              * * Constructor                                                                                                  * *                                                                                                              * ****************************************************************************************************************//** * Construct a new Tray Icon * Using a Java Image - This can be loaded from a 16x16 GIF or JPG file * * Param image	16x16 icon - make sure it's loaded in memory - use MediaTracker * Param w	the icon width - eg. 16 * Param h	the icon height - eg. 16 * * Exception TrayIconException - if something goes wrong :O( *	- Too many icons allocated *	- Error initializing native code DLL *	- Error setting up Windows notify procedure *	- Error loading icon image * Exception InterruptedException - if the thread loading the image is interrupted */	public WindowsTrayIcon(Image image, int w, int h) throws TrayIconException, InterruptedException {		// Allocate new id for icon (native routine)		m_ID = getFreeId();		if (m_ID == TOOMANYICONS)			throw new TrayIconException("Too many icons allocated");		if (m_ID == DLLNOTFOUND)			throw new TrayIconException("Error initializing native code DLL");		if (m_ID == NOTIFYPROCERR)			throw new TrayIconException("Error setting up Windows notify procedure");		// Store image data and size		setImage(image, w, h);	}	/**************************************************************************************************************** *                                                                                                              * * Methods                                                                                                      * *                                                                                                              * ****************************************************************************************************************//** * Change this icon's Image * Using a Java Image - This can be loaded from a 16x16 GIF or JPG file * * Param image	16x16 icon - make sure it's loaded in memory - use MediaTracker * Param w	the icon width * Param h	the icon height * * Exception TrayIconException - if something goes wrong :O( *	- Error loading icon image * Exception InterruptedException - if the thread loading the image is interrupted */	public void setImage(Image image, int w, int h) throws TrayIconException, InterruptedException {		try {			// Collect pixel data in array			int[] pixels = new int[w * h];			PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);			pg.grabPixels();			if ((pg.getStatus() & ImageObserver.ABORT) != 0) {				freeIcon();				throw new TrayIconException("Error loading icon image");			}			// Send image data to the native library			setIconData(m_ID, w, h, pixels);		} catch (InterruptedException ex) {			freeIcon();			throw ex;		} catch (NullPointerException ex) {			freeIcon();			throw ex;		}	}/** * Show/Hide this icon in the Windows System Tray * * Param status true = show, false = hide */	public void setVisible(boolean status) {		showIcon(m_ID, status);	}/** * Test if this icon is currently visible in the Windows System Tray * * Returns true if visible */	public boolean isVisible() {		return testVisible(m_ID) == 1;	}/** * Changes the text for the ToolTip of this icon * The ToolTip is displayed when the user mouses over the icon * * Param tip = the new text for the ToolTip */	public void setToolTipText(String tip) {		setToolTip(m_ID, tip);	}	/** * Display a balloon message for the icon */	public final static int BALLOON_NONE = 0;	public final static int BALLOON_INFO = 1;	public final static int BALLOON_WARNING = 2;	public final static int BALLOON_ERROR = 3;	public final static int BALLOON_NOSOUND = 0x10;	public void showBalloon(String msg, String title, int timeout, int flags) throws TrayIconException {		if (showBalloon(m_ID, msg, title, timeout, flags) == 0) {			throw new TrayIconException("Error showing Balloon message");		}	}/** * Add an ActionLister to this icon * Just like with java.awt.Button or javax.swing.JButton * * Param listener = your listener */	public void addActionListener(ActionListener listener) {		if (m_ActList == null) {			m_ActList = new Vector();			clickEnable(this, m_ID, true);		}		m_ActList.addElement(listener);	}		public void removeActionListener(ActionListener listener) {		m_ActList.removeElement(listener);	}		public void addMouseListener(MouseListener listener) {		if (m_MouseList == null) {			m_MouseList = new Vector();			clickEnable(this, m_ID, true);		}		m_MouseList.addElement(listener);	}		public void removeMouseListener(MouseListener listener) {		m_MouseList.removeElement(listener);	}	public void addBalloonListener(TrayBalloonListener listener) {		if (m_BalloonList == null) {			m_BalloonList = new Vector();			clickEnable(this, m_ID, true);		}				m_BalloonList.addElement(listener);		}		public void removeBalloonListener(TrayBalloonListener listener) {		m_BalloonList.removeElement(listener);		}	/** * Set new popup menu * The popup menu is displayed when the user right clicks the icon * See class TrayIconPopup, TrayIconPopupSimpleItem, .. * * Param popup = the popup menu */	public void setPopup(TrayIconPopup popup) {		if (popup == null) {			m_Popup = null;			initPopup(m_ID, -1);		} else {			if (m_Popup == null) clickEnable(this, m_ID, true);			int levels = popup.getNbLevels();			initPopup(m_ID, levels);			popup.setTrayIcon(this, m_ID, -1);			m_Popup = popup;		    		}	}/** * Free all native resources for this icon * On exit use cleanUp() */	public void freeIcon() {		clickEnable(this, m_ID, false);		freeIcon(m_ID);	}			public static native void setAlwaysOnTop(Component wnd, boolean onTop);	public final static int UNICODE_CONV_BALLOON = 2;public final static int UNICODE_CONV_SUPPORT = 1;	public static native void enableUnicodeConversion(int component, boolean enable);		public static native boolean hasUnicodeConversion(int component);		public static native boolean supportsBalloonMessages();/** * Return error code from native library - use for debugging */	public static native int getLastError();// No error occured since the last call to getLastError()// There are a lot errors declared but they are only there for debug reasons	public static final int NOERR = 0;// The ActionListeners of the icon need to be notified when the user clicks it// In the Windows API this is accomplished using a Notify Procedure 	public static final int NOTIFYPROCERR = -1;// The DLL has a fixed data structure that can contain up to 100 icons// Hope that's enough for you	public static final int TOOMANYICONS = -2;// This happens when C++ is out of memory	public static final int NOTENOUGHMEM = -3;// Each icon has one unique id number	public static final int WRONGICONID = -4;// The native code can't locate the DLL// Try moving it to C:/WINDOWS/SYSTEM or something like that	public static final int DLLNOTFOUND = -5;// Invocation code can't find your Java VM during callback	public static final int NOVMS = -6;// Invocation API can't attach native thread to your Java VM	public static final int ERRTHREAD = -7;// Error in lookup of the notifyListeners() method in this class// The DLL has to do this when the user clicks one of your icons	public static final int METHODID = -8;// Not really an error..// This happens when the user clicks an icon that has no ActionListener yet	public static final int NOLISTENER = -9;// One of the Invocation JNI Functions returned an error	public static final int JNIERR = -10;// Error showing balloon	public static final int ERRORBALLOON = -18;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?