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

Adding Icons to the System Tray

, 2 Aug 2003
A class for adding icons to the system tray
hatrsystemtray.zip
systemtraysdk_demo.zip
SystemTraySDK_demo
Release
TaskbarDemo.exe
res
icon1.ico
icon2.ico
icon3.ico
icon4.ico
TaskbarDemo.ICO
TaskbarDemo.dsp
TaskbarDemo.dsw
systemtray_demo.zip
systemtray_demo
Release
SystemTrayDemo.exe
RES
Cmdbar.bmp
icon1.ico
icon2.ico
icon3.ico
icon4.ico
SystemTrayDemo.ico
SystemTrayDemoDoc.ico
Toolbar.bmp
SystemTrayDemo.dsp
SystemTrayDemo.dsw
SystemTrayDemoCE.dsp
SystemTrayDemoCE.dsw
systemtray_src.zip
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "SystemTrayDemo.h"

#include "MainFrm.h"

#ifndef _WIN32_WCE
#include "BalloonDlg.h"
#endif

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

#define	WM_ICON_NOTIFY			WM_APP+10

#ifndef SYSTEMTRAY_USEW2K
#define NIIF_WARNING 0
#endif

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
    ON_COMMAND(ID_POPUP_ANIMATE, OnAnimate)
	ON_COMMAND(IDC_SHOW_ICON, OnShowIcon)
	ON_COMMAND(IDC_HIDE_ICON, OnHideIcon)
	ON_COMMAND(IDC_MOVE_TO_RIGHT, OnMoveToRight)
	ON_COMMAND(IDC_SHOW_BALLOON, OnShowBalloon)
	ON_UPDATE_COMMAND_UI(IDC_HIDE_ICON, OnUpdateHideIcon)
	ON_UPDATE_COMMAND_UI(IDC_SHOW_BALLOON, OnUpdateShowBalloon)
	ON_UPDATE_COMMAND_UI(IDC_MOVE_TO_RIGHT, OnUpdateMoveToRight)
	ON_UPDATE_COMMAND_UI(IDC_SHOW_ICON, OnUpdateShowIcon)
	ON_UPDATE_COMMAND_UI(IDC_SET_FOCUS, OnUpdateSetFocus)
	ON_COMMAND(IDC_SET_FOCUS, OnSetFocusToIcon)
	ON_WM_CLOSE()
	ON_COMMAND(ID_DEMO_MAXIMISE, OnDemoMaximise)
	ON_UPDATE_COMMAND_UI(ID_DEMO_MAXIMISE, OnUpdateDemoMaximise)
	ON_COMMAND(ID_DEMO_MINIMISE, OnDemoMinimise)
	ON_UPDATE_COMMAND_UI(ID_DEMO_MINIMISE, OnUpdateDemoMinimise)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
}

CMainFrame::~CMainFrame()
{
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

#ifndef _WIN32_WCE
    HICON hIcon = ::LoadIcon(NULL, IDI_ASTERISK);  // Icon to use
#else
    HICON hIcon = ::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDR_MAINFRAME));  // Icon to use
#endif

	// Create the tray icon
#ifdef USING_NONMFC_TRAY  // This is set in project settings
	if (!m_TrayIcon.Create(AfxGetInstanceHandle(),
#else
	if (!m_TrayIcon.Create(
#endif
                        NULL,                            // Let icon deal with its own messages
                        WM_ICON_NOTIFY,                  // Icon notify message to use
                        _T("This is a Tray Icon - Right click on me!"),  // tooltip
                        hIcon,
                        IDR_POPUP_MENU,                  // ID of tray icon
                        FALSE,
                        _T("Here's a cool new Win2K balloon!"), // balloon tip
                        _T("Look at me!"),               // balloon title
                        NIIF_WARNING,                    // balloon icon
                        20 ))                            // balloon timeout
    {
		return -1;
    }

	// MFC version of CSystemTray automatically sends messages to the main app window
#ifdef USING_NONMFC_TRAY  // This is set in project settings
	m_TrayIcon.SetTargetWnd(GetSafeHwnd());		// Send all menu messages here.
#endif

    m_TrayIcon.SetMenuDefaultItem(3, TRUE);
	
	return 0;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame/CSystemTray menu message handlers

void CMainFrame::OnAnimate()
{
    m_TrayIcon.SetIconList(IDI_ICON1, IDI_ICON4);
    m_TrayIcon.Animate(50,2);
}

void CMainFrame::OnShowIcon() 
{
    m_TrayIcon.ShowIcon();
}

void CMainFrame::OnHideIcon() 
{
    m_TrayIcon.HideIcon();
}

void CMainFrame::OnMoveToRight() 
{
    m_TrayIcon.MoveToRight();
}

void CMainFrame::OnShowBalloon() 
{
#ifdef SYSTEMTRAY_USEW2K
    CBalloonDlg dlg;

    if ( IDOK == dlg.DoModal() )
    {
        m_TrayIcon.ShowBalloon ( dlg.m_sBalloonText, dlg.m_sBalloonTitle,
                                 dlg.m_dwIcon, dlg.m_uTimeout );
    }
#else
    MessageBox(_T("Balloon tips only supported in Windows 2000"), _T("Sorry..."),
               MB_ICONEXCLAMATION);
#endif
}

void CMainFrame::OnSetFocusToIcon() 
{
#ifdef SYSTEMTRAY_USEW2K
    m_TrayIcon.SetFocus();
#else
    MessageBox(_T("\"SetFocus\" only supported in Windows 2000"), _T("Sorry..."),
               MB_ICONEXCLAMATION);
#endif
}

void CMainFrame::OnUpdateShowIcon(CCmdUI* pCmdUI) 
{
    pCmdUI->Enable ( !m_TrayIcon.Visible() );
}

void CMainFrame::OnUpdateHideIcon(CCmdUI* pCmdUI) 
{
    pCmdUI->Enable ( m_TrayIcon.Visible() );
}

void CMainFrame::OnUpdateMoveToRight(CCmdUI* pCmdUI) 
{
    pCmdUI->Enable ( m_TrayIcon.Visible() );
}

void CMainFrame::OnUpdateShowBalloon(CCmdUI* pCmdUI) 
{
#ifdef SYSTEMTRAY_USEW2K
    pCmdUI->Enable ( m_TrayIcon.Visible() );
#else
    pCmdUI->Enable(FALSE);
#endif
}

void CMainFrame::OnUpdateSetFocus(CCmdUI* pCmdUI) 
{
#ifdef SYSTEMTRAY_USEW2K
    pCmdUI->Enable ( m_TrayIcon.Visible() );
#else
    pCmdUI->Enable(FALSE);
#endif
}

void CMainFrame::OnClose() 
{
    m_TrayIcon.RemoveIcon();	
	CFrameWnd::OnClose();
}

void CMainFrame::OnDemoMaximise() 
{
#ifndef _WIN32_WCE
#ifdef USING_NONMFC_TRAY  // This is set in project settings
    CSystemTray::MaximiseFromTray(GetSafeHwnd());
#else
    CSystemTray::MaximiseFromTray(this);
#endif
    m_TrayIcon.SetMenuDefaultItem(ID_DEMO_MINIMISE, FALSE);
#endif
}

void CMainFrame::OnDemoMinimise() 
{
#ifndef _WIN32_WCE
#ifdef USING_NONMFC_TRAY  // This is set in project settings
    CSystemTray::MinimiseToTray(GetSafeHwnd());
#else
    CSystemTray::MinimiseToTray(this);
#endif
    m_TrayIcon.SetMenuDefaultItem(ID_DEMO_MAXIMISE, FALSE);
#endif
}

void CMainFrame::OnUpdateDemoMaximise(CCmdUI* pCmdUI) 
{
#ifdef _WIN32_WCE
    pCmdUI->Enable(FALSE);
#else
    pCmdUI->Enable(!IsIconic() && !IsWindowVisible());
#endif
}

void CMainFrame::OnUpdateDemoMinimise(CCmdUI* pCmdUI) 
{
#ifdef _WIN32_WCE
    pCmdUI->Enable(FALSE);
#else
    pCmdUI->Enable(!IsIconic() && IsWindowVisible());
#endif
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Chris Maunder
Founder CodeProject
Canada Canada
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.
 
His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.
 
He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.
 
Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.
Follow on   Twitter   Google+   LinkedIn

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 3 Aug 2003
Article Copyright 1999 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid