Click here to Skip to main content
15,891,845 members
Articles / Programming Languages / C++

CTrayIcon - System Tray Icon Wrapper

Rate me:
Please Sign up or sign in to vote.
3.17/5 (14 votes)
6 Jun 20078 min read 176.1K   5K   47  
An article exhibiting a wrapper class for System Taskbar icon.
// IconData.h : header file

#pragma once

class CIconData;

// Synomym for CIconData pointer
typedef CIconData* IconDataPtr;

// CIconData

class CIconData: public CObject
{
public:

	// Quad parameter constructor
	CIconData(	unsigned int uDefaultIconID, 
				LPSTR lpToolTip,
				unsigned int uHoverIconID = 0, 
				HICON hIcon = NULL)
	{
		SetDefaultIconID(uDefaultIconID);	
		SetHoverIconID(uHoverIconID);
		SetToolTip(lpToolTip);		
		SetIconHandler(hIcon);	
		LoadIcon(GetDefaultIconID());
	}

	// Copy constructor
	CIconData(const CIconData &pIconData)
	{
		CIconData(pIconData.m_uDefaultIconID, pIconData.m_lpToolTip, pIconData.m_uHoverIconID, pIconData.m_hIcon);
	}


	// Gets the selected icon's identifer
	unsigned int GetIconID() const
	{
		return m_uIconID;
	}

	// Gets the default icon identfier
	unsigned int GetDefaultIconID() const
	{
		return m_uDefaultIconID;
	}

	// Sets the default icon identifier
	void SetDefaultIconID(unsigned int uIconID)
	{
		m_uDefaultIconID = uIconID;		
	}

	// Gets the hover icon identifer
	unsigned int GetHoverIconID() const
	{
		return m_uHoverIconID;
	}

	// Sets the hover icon identifier
	void SetHoverIconID(unsigned int uIconID)
	{
		m_uHoverIconID = uIconID;
	}

	// Gets the tooltip
	LPSTR GetToolTip() const
	{
		return m_lpToolTip;
	}

	// Sets the tooltip
	void SetToolTip(LPSTR lpToolTip)
	{
		m_lpToolTip = lpToolTip;
		/*m_lpToolTip = new char[strlen(lpToolTip)];
		strcpy(m_lpToolTip, lpToolTip);*/
	}

	// Gets the icon handler
	HICON GetIconHandler() const
	{
		return m_hIcon;
	}

	// Selects and loads the speciifed icon into memory
	bool LoadIcon(unsigned int uIconID)
	{
		if(uIconID == m_uDefaultIconID || uIconID == m_uHoverIconID)
		{
			SetIconID(uIconID);
			SetIconHandler(AfxGetApp()->LoadIcon(GetIconID()));
			return true;
		}

		return false;
	}

	// Desctructor
	~CIconData()
	{
		//delete[] m_lpToolTip;
		m_lpToolTip = 0;
	}

private:

	CIconData(){}

	void SetIconID(unsigned int uIconID)
	{
		if(uIconID != 0)
			m_uIconID = uIconID;		
	}

	void SetIconHandler(HICON hIcon)
	{
		if(hIcon != NULL)
		{
			if(m_hIcon != NULL)
				DestroyIcon(m_hIcon);
			m_hIcon = hIcon;
		}
	}

	unsigned int m_uIconID;

	unsigned int m_uDefaultIconID;

	unsigned int m_uHoverIconID;	

	HICON m_hIcon;

	LPSTR m_lpToolTip;
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions