📄 windowstrayicon.java
字号:
// 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;
/****************************************************************************************************************
* *
* Windows messaging code for detecting previous instance *
* *
****************************************************************************************************************/
/**
* Checks if there's an instance with hidden window title = appName running
* Can be used to detect that another instance of your app is already running (so exit..)
*
* Param appName = the title of the hidden window to search for
*/
public static native boolean isRunning(String appName);
/**
* Send a message to another app (message can contain an integer)
* Can be used to detect that another instance of your app is already running
* That instance can for example restore it's window after it receives the windows
* message - see demo app for more info
*
* Param appName = the title of the hidden window to search for
* Param message = the integer message to send (only native int size supported)
*/
public static native int sendWindowsMessage(String appName, int message);
/**
* Set callback method for receiving windows messages
* See sendWindowsMessage() for more information or take a look at the demo app
*
* Param callback = the callback method for this app
*/
public static void setWindowsMessageCallback(TrayIconCallback callback) {
mWindowsMessageCallback = callback;
}
/****************************************************************************************************************
* *
* Next section is for inter use only -- or for hackers :O) *
* *
****************************************************************************************************************/
// Each icon has a unique id ranging from 0..99
private int my_id;
// Each icon can have a popup menu - activated when user right clicks the icon
private TrayIconPopup mPopup;
// Each icon can have any number of ActionListeners - notified when user clicks (left/right) the icon
private Vector listeners;
// Each application can have one WindowsMessageCallback - notified when another app uses sendWindowsMessage
private static TrayIconCallback mWindowsMessageCallback;
/**
* Private method called by native library when user clicks mouse button
*
* Param button = "Left" or "Right"
*/
private void notifyMouseListeners(int button) {
if (listeners != null) {
ActionEvent evt = null;
if (button == 0) evt = new ActionEvent(this,0,"Left");
else evt = new ActionEvent(this,0,"Right");
for (Enumeration enum = listeners.elements(); enum.hasMoreElements(); ) {
ActionListener listener = (ActionListener)enum.nextElement();
listener.actionPerformed(evt);
}
}
}
/**
* Private method called by native library when user selects popup menu item
*
* Param id = id of menu item (each menu item has unique id)
*/
private void notifyMenuListeners(int id) {
if (mPopup != null) mPopup.onSelected(id);
}
/**
* Private method called by native library when it receives a sendWindowsMessage event
* See sendWindowsMessage() for more information or take a look at the demo app
*
* Param lParam = parameter send along with windows message
*/
private static int callWindowsMessage(int lParam) {
if (mWindowsMessageCallback != null) return mWindowsMessageCallback.callback(lParam);
else return 0;
}
/**
* Set check mark for TrayIconPopupCheckItem
* Used by TrayIconPopupCheckItem.setCheck()
*
* Param menuId = the id of the menu item
* Param selected = true if check mark
*/
void checkPopup(int menuId, boolean selected) {
checkPopup(my_id, menuId, selected);
}
/**
* Init new popup menu - used by setPopup()
*
* Param id = the icon's id
* Param nblevels = the submenu depth of the new popup
*/
static native void initPopup(int id, int nblevels);
// Constants for builing a popup menu
// Used by subclasses of TrayIconPopupItem
final static int POPUP_TYPE_ITEM = 0; // Simple item
final static int POPUP_TYPE_SEPARATOR = 1; // Separator
final static int POPUP_TYPE_CHECKBOX = 2; // Checkbox item
final static int POPUP_TYPE_INIT_LEVEL = 3; // First item of submenu
final static int POPUP_TYPE_DONE_LEVEL = 4; // Last item of submenu
/**
* Add popup menu item - used by setTrayIcon() in subclasses of TrayIconPopupItem
*
* Param id = the icon's id
* Param level = the submenu level
* Param name = the name of the menu item
* Param type = POPUP_TYPE_ITEM or POPUP_TYPE_SEPARATOR or..
*/
static native int subPopup(int id, int level, String name, int type);
/**
* Set check mark for TrayIconPopupCheckItem
* Used by checkPopup(int menuId, boolean selected)
*
* Param id = the icon's id
* Param menuId = the id of the menu item
* Param selected = true if check mark
*/
private static native void checkPopup(int id, int menuId, boolean selected);
/**
* Allocate a new id for icon - used in constructor
*/
private static native int getFreeId();
/**
* Set bitmap data for icon - used in constructor and setImage()
*
* Param id = the icon's id
* Param w, h = the images size
* Param pixels = the pixel array
*/
private static native void setIconData(int id, int w, int h, int pixels[]);
/**
* Make Tray Icon visible/invisible - used by setVisible()
*
* Param id = the icon's id
* Param hide = visible/invisible?
*/
private static native void showIcon(int id, boolean hide);
/**
* Test if Tray Icon is in the system tray - used by isVisible()
*
* Param id = the icon's id
*/
private static native int testVisible(int id);
/**
* Enable mouse/menu messages for icon - used by addActionListener() and setPopup()
*
* Param ico = the icons class (this)
* Param id = the icon's id
* Param enable = enable/disable mouse events?
*/
private static native void clickEnable(WindowsTrayIcon ico, int id, boolean enable);
/**
* Set tooltip - used by setToolTip(String tip)
*
* Param id = the icon's id
* Param tip = the new tooltip
*/
private static native void setToolTip(int id, String tip);
/**
* Free all native resources for this icon - used by freeIcon()
*
* Param id = the icon's id
*/
private static native void freeIcon(int id);
private static native void detectAllClicks(int id);
/**
* Get coordinates of last mouse click
*/
private static native int getMouseX();
private static native int getMouseY();
/**
* Keep TrayIcon alive
*/
public static void keepAlive() {
if (m_Keeper == null) {
m_Keeper = new TrayIconKeeper();
m_Keeper.start();
}
}
// Init the native library
static {
boolean ok = false;
String version = System.getProperty("java.version");
if (version.length() >= 3) {
String v1 = version.substring(0,3);
if (v1.equals("1.1")) {
System.loadLibrary("TrayIcon11");
ok = true;
}
if (v1.equals("1.2") || v1.equals("1.3") || v1.equals("1.4")) {
System.loadLibrary("TrayIcon12");
ok = true;
}
}
if (!ok) {
System.out.println("Wrong Java VM version: "+version);
System.exit(-1);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -