📄 windowstrayicon.java
字号:
/***
* 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 {
private static TrayIconKeeper m_Keeper;
/****************************************************************************************************************
* *
* 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 native void initTrayIcon(String appName);
/**
* Free all native resources - call this method before System.exit()
*/
public static void cleanUp() {
if (m_Keeper != null) {
m_Keeper.notify();
m_Keeper = null;
}
termTrayIcon();
}
private static native void 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)
my_id = getFreeId();
if (my_id == TOOMANYICONS)
throw new TrayIconException("Too many icons allocated");
if (my_id == DLLNOTFOUND)
throw new TrayIconException("Error initializing native code DLL");
if (my_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(my_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(my_id, status);
}
/**
* Test if this icon is currently visible in the Windows System Tray
*
* Returns true if visible
*/
public boolean isVisible() {
return testVisible(my_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(my_id, tip);
}
/**
* 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 (listeners == null) {
listeners = new Vector();
clickEnable(this, my_id, true);
}
listeners.addElement(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 (mPopup == null) clickEnable(this, my_id, true);
mPopup = popup;
int levels = popup.getNbLevels();
initPopup(my_id, levels);
popup.setTrayIcon(this, my_id, -1);
}
/**
* Free all native resources for this icon
* On exit use cleanUp()
*/
public void freeIcon() {
clickEnable(this, my_id, false);
freeIcon(my_id);
}
/**
* Get coordinates of last mouse click
*/
public static Point getMousePos() {
return new Point(getMouseX(), getMouseY());
}
/**
* 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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -