Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

CTrayIcon - System Tray Icon Wrapper

, 6 Jun 2007
An article exhibiting a wrapper class for System Taskbar icon.
ctrayicon_demo.zip
CTrayIconDemo.exe
ctrayicon_src.zip
res
CTrayIconDemo.ico
ico00001.ico
ico00002.ico
idr_main.ico
Thumbs.db
CTrayIconDemo.aps
CTrayIconDemo.clw
CTrayIconDemo.dsp
CTrayIconDemo.dsw
CTrayIconDemo.ncb
CTrayIconDemo.opt
CTrayIconDemo.plg
demo.zip
Demo
CTrayIconDemo.exe
src.zip
Src
CTrayIconDemo.clw
CTrayIconDemo.dsp
CTrayIconDemo.dsw
res
CTrayIconDemo.ico
// CTrayIconDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CTrayIconDemo.h"
#include "CTrayIconDemoDlg.h"
#include ".\CTrayIconDemoDlg.h"
#include "MouseMsgHandlerImpl.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCTrayIconDemoDlg dialog

CCTrayIconDemoDlg::CCTrayIconDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCTrayIconDemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCTrayIconDemoDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCTrayIconDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCTrayIconDemoDlg)
	// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
	DDX_Control(pDX, IDC_BTNSTOPANI, m_ctrlStopAni);
	DDX_Control(pDX, IDC_BTNSTARTANI, m_ctrlStartAni);
	DDX_Control(pDX, IDC_BTNSHOWICON, m_ctrlShowIcon);
	DDX_Control(pDX, IDC_BTNHIDEICON, m_ctrlHideIcon);
}

BEGIN_MESSAGE_MAP(CCTrayIconDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CCTrayIconDemoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(IDR_TASKBAR_MENU_ABOUT, OnAbout)
	ON_COMMAND(IDR_TASKBAR_MENU_SHOW, OnShow)
	ON_COMMAND(IDR_TASKBAR_MENU_EXIT, OnCancel)
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BTNHIDEICON, OnBnClickedBtnhideicon)
	ON_BN_CLICKED(IDC_BTNSHOWICON, OnBnClickedBtnshowicon)
	ON_BN_CLICKED(IDC_BTNSTARTANI, OnBnClickedBtnstartani)
	ON_BN_CLICKED(IDC_BTNSTOPANI, OnBnClickedBtnstopani)
	ON_BN_CLICKED(IDC_BTNMINIMIZE, OnBnClickedBtnminimize)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCTrayIconDemoDlg message handlers

BOOL CCTrayIconDemoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// Declare an array of MouseMsgHandlerPtr of size 3, to handle three types of mouse events
	MouseMsgHandlerPtr *pHandler = new MouseMsgHandlerPtr [3];
	
	// Define an object of CRightMouseClickMsgHandler for handling right mouse button click events
	MouseMsgHandlerPtr rbcHandler = new CRightMouseClickMsgHandler();

	// Define an object of CLeftMouseDblClickMsgHandler for handling left mouse button double click events
	MouseMsgHandlerPtr lbcHandler = new CLeftMouseDblClickMsgHandler();

	// Define an object of CMouseHoverMsgHandler for handling mouse hover events
	MouseMsgHandlerPtr mhHandler = new CMouseHoverMsgHandler(m_pTrayIcon);

	pHandler[0] = lbcHandler;
	pHandler[1] = rbcHandler;
	pHandler[2] = mhHandler;

	// Declare an array of IconDataPtr of size 2, to add two sets of icons. 
	// Only one will be active though.
	IconDataPtr *pIconData = new IconDataPtr[2];

	// Define an object of Blue MFC icon
	pIconData[0] = new CIconData(IDR_MAINFRAME, _T("CTrayIcon Blue Demo"), IDR_MAINFRAME_HOVER);	
	
	// Define an object of Green MFC icon
	pIconData[1] = new CIconData(IDR_MAINFRAME_GREEN, _T("CTrayIcon Green Demo"), IDR_MAINFRAME_HOVER);	

	// Define the CTrayIcon object
	m_pTrayIcon = new CTrayIcon(pHandler, 3, pIconData, 2, 0, SECOND);

	// Refer the CTrayIcon object to the CMouseHoverMsgHandler object, for handling mouse events
	((CMouseHoverMsgHandler*)mhHandler)->SetTrayIcon(m_pTrayIcon);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCTrayIconDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCTrayIconDemoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCTrayIconDemoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CCTrayIconDemoDlg::OnAbout() 
{
	CAboutDlg aDlg;
	aDlg.DoModal();
}

void CCTrayIconDemoDlg::OnShow() 
{
	ShowWnd(this);
}

void CCTrayIconDemoDlg::OnCancel() 
{
	delete m_pTrayIcon;
	
	CDialog::OnCancel();
}
void CCTrayIconDemoDlg::OnBnClickedBtnhideicon()
{
	m_pTrayIcon->HideIcon();
	m_ctrlShowIcon.EnableWindow(true);
	m_ctrlHideIcon.EnableWindow(false);
	m_ctrlStartAni.EnableWindow(false);
	m_ctrlStopAni.EnableWindow(false);
}

void CCTrayIconDemoDlg::OnBnClickedBtnshowicon()
{
	m_pTrayIcon->ShowIcon();
	m_ctrlStartAni.EnableWindow(true);
	m_ctrlShowIcon.EnableWindow(false);
	m_ctrlHideIcon.EnableWindow(true);
}

void CCTrayIconDemoDlg::OnBnClickedBtnstartani()
{
	m_pTrayIcon->StartIconAnimation();
	m_ctrlStartAni.EnableWindow(false);
	m_ctrlStopAni.EnableWindow(true);
}

void CCTrayIconDemoDlg::OnBnClickedBtnstopani()
{
	m_pTrayIcon->StopIconAnimation();
	m_ctrlStartAni.EnableWindow(true);
	m_ctrlStopAni.EnableWindow(false);
}

void CCTrayIconDemoDlg::OnBnClickedBtnminimize()
{
	m_pTrayIcon->ShowIcon();
	m_ctrlStartAni.EnableWindow(true);
	HideWnd(this);
}

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

Share

About the Author

No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141220.1 | Last Updated 6 Jun 2007
Article Copyright 2004 by Muneeb Ahmed Awan
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid