⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 windowstrayicon.cpp

📁 本组件可以定制各种最小化图标,为应用程序添加图标,就是系统托盘图标
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/**
 * WindowsTrayIcon.cpp - a tray icon for Java
 * Best viewed with tab size 8
 *
 * Written by Jan Struyf
 *
 *  jan.struyf@cs.kuleuven.ac.be
 *  http://jeans.studentenweb.org/java/trayicon/trayicon.html
 *
 * Changelog
 *
 * Version 1.7.9b (02/26/04)
 *  * Added method to flash window in task bar (thanks to Joe Page)
 *
 * Version 1.7.9 (02/03/04)
 *  * Callback for clicks on balloon message
 *  * Fix for working with Java Web Start
 *  * Swing menu is now set Always-On-Top
 *  * Fix in balloon message that caused VM to crash
 *  * Fixed bug in balloon message specific to Windows 2000
 *
 * Version 1.7.8 (11/2/03)
 *  * Unicode support also for 95/98/Me
 *  * Exception added for balloon message
 *
 * Version 1.7.7 (10/23/03)
 *  * Unicode support for native components
 *
 * Version 1.7.6 (08/17/03)
 *  * Support for Balloon Messages
 *  * Fix for Swing menu (menu is removed if user clicks on desktop area)
 *
 * Version 1.7.5 (02/20/03)
 *  * Support for MouseListeners (icon can respond to double clicks)
 *  * TrayIcons are not lost anymore when Explorer crashes ;-) (thanks to Laurent Hauben)
 *  * JAWT is only used if Swing Menu is used (call initJAWT()!)
 *
 * Version 1.7.4 (01/08/03)
 *  * setAlwaysOnTop implemented for Swing menu (thanks to Mike Hicks)
 *  * SwingTrayIcon demo updated with nice Swing menu
 * 
 * Version 1.7.3 (01/03/03)
 *  * Bold and disabled AWT menu item support 
 *  * Fixed bug in keepAlive()  
 *
 * Version 1.7.2 (12/17/02)
 *  * Transparency bug fix for Windows ME 
 *  * Fixed naming problem with setWindowsMessageCallback (introduced in 1.7.1) 
 *
 * Version 1.7.1 (11/01/02)
 *  * Works with Java 1.4 
 *  * Bug fix for setCheck(boolean selected) 
 *  * Dependency on jvm.lib removed (thanks to Justin Chapweske) 
 *  *Includes some support for using a Swing popup menu 
 * 
 * Version pre1.6c (07/16/01)
 *	* Fixed minor compilation warnings reported by the more strict VC++ 6.0
 *
 * Version pre1.6b (12/16/00)
 *	* Fixed memory leak for 'animating icons'
 *      * ReleaseDC -> DeleteDC
 *
 * Version pre1.6 (09/02/00)
 *	* Support for old JDK/JRE 1.1.x
 *	* TrayIcon 1.6 will support Microsoft Visual J++
 *
 * Version 1.5 (07/03/00)
 *	* Tray icon popup menu support
 *	* Added code for sendWindowsMessage()
 *
 * Version 1.4 (06/29/00)
 *	* Added DllMain function to clean init code
 *	* Removed redundant LoadLibrary/FreeLibrary calls
 *	* Added code for isRunning()
 *
 * Version 1.3 (06/09/00)
 *	* Trail bug fix for NT (no public release)
 *		(Patch from 'Daniel Hope <danielh@inform.co.nz>')
 *
 * Version 1.2 (05/03/00)
 *	* Message handler for first icon fixed
 *	* WM_RBUTTONDOWN message handler fixed
 *	* Classes are now unregistered on exit
 *		(Patch from 'Daniel Rejment <daniel@rejment.com>')
 *
 * Version 1.0 (06/29/99)
 *	* Initial release
 *
 * Please mail me if you
 *	- 've found bugs
 *	- like this program
 *	- don't like a particular feature
 *	- would like something to be modified
 *
 * To compile:
 *	- Use the MDP project file in the VC++ IDE
 *	- Use Makefile.vc as in:
 *		VCVARS32
 *		nmake /f makefile.vc
 *
 * 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!
 */

#define _WIN32_IE 0x0500
#define USE_GLOBAL_REF

#include <windows.h>
#include <windowsx.h>
#include <process.h>
#include <shlwapi.h>
#include <jni.h>

#include <jawt.h>
#include <win32/jawt_md.h>

#include "com_jeans_trayicon_WindowsTrayIcon.h"

// Message ID for sendWindowsMessage() function
#define MYWM_APPTALK		(WM_APP+10)
#define MYWM_APPMOUSE		(WM_APP+11)
// Message ID for Tray icons
#define MYWM_NOTIFYICON		(WM_APP+100)
// Max number of icons supported by this DLL for each application
#define MY_MAX_ICONS		100
// Max length for the name of the hidden window (used for sendWindowsMessage/isRunning)
#define WNDNAME_MAX             20

// Class for the icon bitmap
class IconData {
private:
	HBITMAP hBitmapXOR;	// Colors
	HBITMAP	hBitmapAND;	// Transparancy info
	int wd, hi;
public:
	IconData();
	~IconData();
	int setData(unsigned long *data, int wd, int hi);
	HICON makeIcon(HINSTANCE hInst);
};

// Class for a popup submenu
class PopupSubMenu {
private:
	HMENU hMenu;
	BOOL bSub;
public:
	PopupSubMenu();
	~PopupSubMenu();
	BOOL isSub();
	void makeSub();
	void reNewMenu();
	HMENU getMenu();
	void TrackPopupMenu(UINT flags, POINT* pos, HWND hWnd);
};

typedef PopupSubMenu* PtrPopupSubMenu;

// Class for a popup menu (the main popup)
class PopupMenu {
private:
	PopupSubMenu **popup;
	int nbLevels;
public:
	PopupMenu(int levels);
	~PopupMenu();
	void TrackPopupMenu(UINT flags, POINT* pos, HWND hWnd);
	HMENU getMenu(int level);
	void initMenu(int level);
	PopupSubMenu *getSubMenu(int level);
};

// Growable array for integers
// Used to store the allocated menu id's
class QSIntArray {
	protected:
		int *m_Array;
		long m_Size, m_Grow, m_ArrSize;
	public:
		QSIntArray();
		QSIntArray(long size, long grow);
		~QSIntArray();
		long getSize();
		int getElementAt(long idx);
		void addElement(int element);
		void setElementAt(long idx, int element);
		void removeElementAt(long idx);
		void removeAll();
	private:
		void shrink();
		void grow();
};

#define JNIProcPtr(name) int (FAR *name) (JNIEnv*, int, int)

// Information for each tray icon
typedef struct {
	BOOL used;		// Is this record in use?
	BOOL visible;		// Icon visible on screen or hidden?
	IconData *icon;		// Icon bitmap data
	char *tooltip;		// Icon tooltip
	PopupMenu *popup;	// Main popup for icon
	jobject globalClass;	// Pointer to Java class for callbacks
} TrayIconData;

#define MOUSE_BTN_UP        1
#define MOUSE_BTN_DOUBLE    2

// Some error codes
#define TRAY_NOERR		  0
#define TRAY_NOTIFYPROCERR	 -1
#define TRAY_TOOMANYICONS	 -2
#define TRAY_NOTENOUGHMEM	 -3
#define TRAY_WRONGICONID	 -4
#define TRAY_DLLNOTFOUND	 -5
#define TRAY_NOVMS		 -6
#define TRAY_ERRTHREAD		 -7
#define TRAY_METHODID		 -8
#define TRAY_NOLISTENER		 -9
#define TRAY_JNIERR		 -10
#define TRAY_CALLBACKDLLERR	 -11
#define TRAY_NO_JAWT             -12
#define TRAY_NO_GET_JAWT         -13
#define TRAY_GET_JAWT_FAILS      -14
#define TRAY_JAWT_DS_FAILS       -15
#define TRAY_JAWT_LOCK_FAILS     -16
#define TRAY_ERRHOOK             -17
#define TRAY_ERRORBALLOON        -18

#define TRAY_WIN_VER_UNKNOWN	0
#define TRAY_WIN_VER_WIN32	1
#define TRAY_WIN_VER_95		2
#define TRAY_WIN_VER_98		3
#define TRAY_WIN_VER_ME		4
#define TRAY_WIN_VER_NT		5
#define TRAY_WIN_VER_2K		6
#define TRAY_WIN_VER_XP		7
#define TRAY_WIN_VER_NET	8

#define TRAY_BALLOON_NONE          0
#define TRAY_BALLOON_INFO          1
#define TRAY_BALLOON_WARNING       2
#define TRAY_BALLOON_ERROR         3
#define TRAY_BALLOON_NOSOUND       0x10

// Constants for the popup menu system
#define POPUP_TYPE_ITEM       0
#define POPUP_TYPE_SEPARATOR  1
#define POPUP_TYPE_CHECKBOX   2
#define POPUP_TYPE_INIT_LEVEL 3
#define POPUP_TYPE_DONE_LEVEL 4
#define POPUP_MODE_ENABLE     1
#define POPUP_MODE_CHECK      2
#define POPUP_MODE_DEFAULT    4

#define NIN_BALLOONSHOW       (WM_USER + 2)
#define NIN_BALLOONHIDE       (WM_USER + 3)
#define NIN_BALLOONTIMEOUT    (WM_USER + 4)
#define NIN_BALLOONUSERCLICK  (WM_USER + 5) 

#define NIN_SELECT            (WM_USER + 0)
#define NINF_KEY              1
#define NIN_KEYSELECT         (NIN_SELECT | NINF_KEY) 

#define UNICODE_CONVERSION_NB       10
#define UNICODE_CONVERSION_SUPPORT  1		// OS has support for Unicode
#define UNICODE_CONVERSION_BALLOON  2

#define BALLOON_MSG_SHOW    1
#define BALLOON_MSG_HIDE    2
#define BALLOON_MSG_CLICK   4
#define BALLOON_MSG_TIMEOUT 8

// Current instance of the TrayIcon DLL
HINSTANCE g_hinst = NULL;
// Handle to the hidden window used to receive the tray icon messages
HWND my_hDlg = NULL;
// App name for the hidden window's registered class
CHAR szAppName[] = "QSJavaTray";
// Title for the hidden window (used for sendWindowsMessage/isRunning)
CHAR szWndName[WNDNAME_MAX+1];
// Number of icons allocated
int nb_tray_icons = 0;
// Record for each icon
TrayIconData tray_icons[MY_MAX_ICONS];
// Contains last error message (see error codes above)
jint last_error = TRAY_NOERR;
// Wait while creating the hidden window
HANDLE wait_event = NULL;
// Wait while destroying hidden window
HANDLE exit_event = NULL;
// Maps menu id's to icon id's
QSIntArray *arrUsedMenuIds = NULL;
// Handle to JAWT
HMODULE hJAWT = NULL;
int hJAWTTried = 0;
// Handle to Java VM
JavaVM *hJavaVM = NULL;
// Taskbar restart id
UINT g_TaskbarRestart = 0;
// Use UNICODE
int bUseUnicode = 0;
// Global ref to WindowsTrayIcon class
jobject hGlobalWinTrayClass = 0;
// Unicode conversion
jboolean g_UnicodeConversion[UNICODE_CONVERSION_NB];

// Shared memory variables
#ifdef CYGWIN
	HHOOK hMouseClickHook  __attribute__((section (".shared"), shared)) = NULL;
	HWND  hMouseClickWin   __attribute__((section (".shared"), shared)) = NULL;
	int   hMouseClickEna   __attribute__((section (".shared"), shared)) = 0;
#else
        #pragma data_seg("Shared")
        HHOOK hMouseClickHook = NULL;
        HWND  hMouseClickWin  = NULL;
        int   hMouseClickEna  = 0;
        #pragma data_seg()
        #pragma comment(linker,"/SECTION:Shared,RWS")
#endif

// Get free menu id (for icon id - see arrUsedMenuIds)
long getFreeMenuId(int id_num);
// Free menu id (when removing menu from icon)
void setFreeMenuId(int id_num);
// Map menu id to icon id
int getMenuItemIdNum(int id);
// Add/Remove/Modify tray icon to system tray
BOOL TrayMessage(HWND hDlg, DWORD dwMessage, UINT uID, HICON hIcon, PSTR pszTip);
BOOL TrayMessageW(HWND hDlg, DWORD dwMessage, UINT uID, HICON hIcon, const jchar* pszTip);
bool TrayBalloon(HWND hDlg, UINT uID, const char* msg, const char* title, UINT time, DWORD flags);
bool TrayBalloonW(HWND hDlg, UINT uID, const jchar* msg, const jchar* title, UINT time, DWORD flags);
bool TraySetVersion(HWND hDlg, UINT uID);
// Create the hidden window to receive the icon's mouse messages
HWND MakeHWND(HINSTANCE hInst);
// Remove the hidden window (on app closing)
void RemoveHWND();
// The thread proc that creates the hidden window an listens for messages
void DialogThread( void *dummy );
// Free an icon's menu resources
void freeMenu(int id_num);
// Free an icon's other resources
void freeIcon(JNIEnv *env, int id_num);
// Clean up @ exit of app
void cleanUpExit(JNIEnv *env);
// Call TrayMessage for the specified icon id
void updateIcon(jint id_num);
// Make an icon dissapear from the system tray
void makeInvisible(jint id_num);
// Delete global reference to icon's Java class
int DeleteGlobalCallback(JNIEnv *env, int id_num, int dummy);
// Call to get JAWT handle
typedef jboolean (JNICALL *PJAWT_GETAWT)(JNIEnv*, JAWT*);
// Version of Shell32 DLL
DWORD hShell32Version = 4;
// Windows Version
DWORD hWindowsVersion = TRAY_WIN_VER_95;
// Initialize Unicode Functions
int initUnicodeFunctions();
// Max chars per multi-byte char
int nbWideMaxChars = 2;


class DoWithHWND {
public:
    virtual int execute(HWND hwnd) = 0;
};

class DoAlwaysOnTop : public DoWithHWND {
    virtual int execute(HWND hwnd);
};

class ThreadJavaCallback {
public:
    virtual int execute(JNIEnv* env) = 0;
};

// Call a Java method in a given virtual machine
int CallJavaVM(JavaVM* vm, ThreadJavaCallback* call);

class SimpleJavaCallback : public ThreadJavaCallback {
protected:
	JNIProcPtr(m_JNIProc);
	int m_Arg1, m_Arg2;
public:
    SimpleJavaCallback(JNIProcPtr(jniproc), int arg1, int arg2);
    virtual int execute(JNIEnv* env);    
};

class MouseJavaCallback : public ThreadJavaCallback {
protected:

⌨️ 快捷键说明

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