VB icon

System Tray Class (Very Easy To Use)

Email
Submitted on: 1/1/2015 8:58:00 AM
By: Mike A. Leonetti (from psc cd)  
Level: Beginner
User Rating: By 4 Users
Compatibility: C, C++ (general)
Views: 1421
 
     This class I developed makes it very easy to put/delete/modify icons in the system tray. You can also get mouse notifications from the system tray. Once again, NO MFC!
 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :System Tray Class (Very Easy To Use)
//**************************************
#include <Shellapi.h>
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: System Tray Class (Very Easy To Use)
// Description:This class I developed makes it very easy to put/delete/modify icons in the system tray. You can also get mouse notifications from the system tray. Once again, NO MFC!
// By: Mike A. Leonetti (from psc cd)
//**************************************

//Class Code (Copy And Paste it Somewhere)//
#define WM_TRAYNOTIFY 0xA44C
#include <Shellapi.h>
class clsSysTray
	{
		public:
			clsSysTray();
			
			BOOL SetIcon(HICON hNewIcon);
			HICON GetIcon();
			BOOL SetTipText(char *lpstrNewTipText);
			char *GetTipText();
			BOOL AddIcon();
			BOOL RemoveIcon();
			HWND hWnd;
			UINT uID;
		protected:
			NOTIFYICONDATA NotifyIconData;
			bool bInTray;
			
	};
clsSysTray::clsSysTray()
	{
		bInTray = false;
		NotifyIconData.cbSize = sizeof(NotifyIconData);
		NotifyIconData.uID = uID = 2;
		NotifyIconData.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
		NotifyIconData.uCallbackMessage = WM_TRAYNOTIFY;
		NotifyIconData.hIcon = LoadIcon(NULL, IDI_APPLICATION);
		NotifyIconData.szTip[0] = '\0';
		NotifyIconData.hWnd = NULL;
		
	}
HICON clsSysTray::GetIcon()
	{
		return(NotifyIconData.hIcon);
	}
BOOL clsSysTray::SetIcon(HICON hNewIcon)
	{
		NotifyIconData.hIcon = hNewIcon;
		if(bInTray)
			{
				BOOL iRetVal;
				iRetVal = Shell_NotifyIcon(NIM_MODIFY, &NotifyIconData);
				if(iRetVal)
					{
						bInTray = true;
					}
				return(iRetVal);
			}
		else
			return(1);
	}
char *clsSysTray::GetTipText()
	{
		return(NotifyIconData.szTip);
	}
BOOL clsSysTray::SetTipText(char *lpstrNewTipText)
	{
		strcpy(NotifyIconData.szTip, lpstrNewTipText);
		if(bInTray)
			{
				BOOL iRetVal;
				iRetVal = Shell_NotifyIcon(NIM_MODIFY, &NotifyIconData);
				if(iRetVal)
					{
						bInTray = true;
					}
				return(iRetVal);
			}
		else
			return(1);
	}
BOOL clsSysTray::AddIcon()
	{
		BOOL iRetVal;
		
		NotifyIconData.hWnd = hWnd;
		NotifyIconData.uID = hWnd;
		iRetVal = Shell_NotifyIcon(NIM_ADD, &NotifyIconData);
		if(iRetVal)
			{
				bInTray = true;
			}
		return(iRetVal);
	}
BOOL clsSysTray::RemoveIcon()
	{
		BOOL iRetVal;
		iRetVal = Shell_NotifyIcon(NIM_DELETE, &NotifyIconData);
		if(iRetVal)
			{
				bInTray = false;
			}
		return(iRetVal);
	}
//End Class Code//
//Example Of Use///
.
.
.
clsSysTray SystemTrayEx;
SystemTrayEx.hWnd = hWnd;
SystemTrayEx.SetTipText("HI");
SystemTrayEx.AddIcon();
SystemTrayEx.SetIcon(hApp_Icon_Main);
.
.
.
//End Example//
///Example Of Callback (Notification) Messages//
.
.
.
	switch(Msg)
		{	
		
		case WM_TRAYNOTIFY:
 	{
 		switch(LOWORD(lParam))
 			{
 	case WM_LBUTTONDBLCLK:
						case WM_LBUTTONDOWN:
						case WM_LBUTTONUP:
						case WM_RBUTTONDBLCLK:
						case WM_RBUTTONDOWN:
						case WM_RBUTTONUP:		
 			}
		return(0);
		} break;
.
.
.
//End Callback Example//


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.