Click here to Skip to main content
15,878,809 members
Articles / Desktop Programming / MFC

System Tray Icons

Rate me:
Please Sign up or sign in to vote.
4.66/5 (26 votes)
1 Apr 2012CPOL8 min read 211K   7K   136  
An article on creating, manipulating and showing popup balloons on system tray icons.
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "SysTray.h"
#include "MainFrm.h"
#include "SysTrayIconTipChange.h"

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

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

IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code !
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
    ON_COMMAND(ID_SHOWICONS_KEYS, OnShowiconsKeys)
    ON_COMMAND(ID_SHOWICONS_INFO, OnShowiconsInfo)
    ON_COMMAND(ID_SHOWICONS_QUESTION, OnShowiconsQuestion)
    ON_COMMAND(ID_SHOWICONS_USERS, OnShowiconsUsers)
    ON_COMMAND(ID_HIDEICONS_KEYS, OnHideiconsKeys)
    ON_COMMAND(ID_HIDEICONS_INFO, OnHideiconsInfo)
    ON_COMMAND(ID_HIDEICONS_QUESTION, OnHideiconsQuestion)
    ON_COMMAND(ID_HIDEICONS_USERS, OnHideiconsUsers)
    ON_COMMAND(ID_DELETEICONS_KEYS, OnDeleteiconsKeys)
    ON_COMMAND(ID_DELETEICONS_INFO, OnDeleteiconsInfo)
    ON_COMMAND(ID_DELETEICONS_QUESTION, OnDeleteiconsQuestion)
    ON_COMMAND(ID_DELETEICONS_USERS, OnDeleteiconsUsers)
    ON_COMMAND(ID_SHOWBALLOONS_KEYS, OnShowballoonsKeys)
    ON_COMMAND(ID_SHOWBALLOONS_INFO, OnShowballoonsInfo)
    ON_COMMAND(ID_SHOWBALLOONS_QUESTION, OnShowballoonsQuestion)
    ON_COMMAND(ID_SHOWBALLOONS_USERS, OnShowballoonsUsers)
    ON_COMMAND(ID_CHANGETEXT_FORKEYSICON, OnChangetextForkeysicon)
    ON_COMMAND(ID_CHANGETEXT_INFO, OnChangetextInfo)
    ON_COMMAND(ID_CHANGETEXT_QUESTION, OnChangetextQuestion)
    ON_COMMAND(ID_CHANGETEXT_USER, OnChangetextUser)
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

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

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

    // Create the tray icons
    HICON hIconKeys = ::AfxGetApp()->LoadIcon(IDI_ICON_KEYS);
    m_stiIcon.CreateIcon(hIconKeys, 1, _T("Keys"));
    
    HICON hIconQuestion = ::AfxGetApp()->LoadIcon(IDI_ICON_QUESTION);
    m_stiIcon.CreateIcon(hIconQuestion, 2, _T("Question"));
    
    HICON hIconUsers = ::AfxGetApp()->LoadIcon(IDI_ICON_USERS);
    m_stiIcon.CreateIcon(hIconUsers, 3, _T("Users"));
    
    HICON hIconInfo = ::AfxGetApp()->LoadIcon(IDI_ICON_INFO);
    m_stiIcon.CreateIcon(hIconInfo, 4, _T("Info"));
        
    return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CMDIFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

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

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

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

#endif //_DEBUG

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


void CMainFrame::OnShowiconsKeys()
{
    m_stiIcon.ShowIcon(1);
}

void CMainFrame::OnShowiconsInfo()
{
    m_stiIcon.ShowIcon(4);    
}

void CMainFrame::OnShowiconsQuestion()
{
    m_stiIcon.ShowIcon(2);
}

void CMainFrame::OnShowiconsUsers()
{
    m_stiIcon.ShowIcon(3);    
}

void CMainFrame::OnHideiconsKeys()
{
    m_stiIcon.HideIcon(1);
}

void CMainFrame::OnHideiconsInfo()
{
    m_stiIcon.HideIcon(4);
}

void CMainFrame::OnHideiconsQuestion()
{
    m_stiIcon.HideIcon(2);
}

void CMainFrame::OnHideiconsUsers()
{
    m_stiIcon.HideIcon(3);
}

void CMainFrame::OnDeleteiconsKeys()
{
    m_stiIcon.DeleteIcon(1);
}

void CMainFrame::OnDeleteiconsInfo()
{
    m_stiIcon.DeleteIcon(4);
}

void CMainFrame::OnDeleteiconsQuestion()
{
    m_stiIcon.DeleteIcon(2);
}

void CMainFrame::OnDeleteiconsUsers()
{
    m_stiIcon.DeleteIcon(3);
}


void CMainFrame::OnShowballoonsKeys()
{
    m_stiIcon.ShowBalloon(1, _T("Keys"), _T("have you lost the keys?"), NIIF_ERROR, 15);
}

void CMainFrame::OnShowballoonsInfo()
{
    m_stiIcon.ShowBalloon(4, _T("Info"), _T("What info do you want today?"), NIIF_INFO);
}

void CMainFrame::OnShowballoonsQuestion()
{
    m_stiIcon.ShowBalloon(2, _T("Warning!!"), _T("You have been warned"), NIIF_WARNING);
}

void CMainFrame::OnShowballoonsUsers()
{
    m_stiIcon.ShowBalloon(3, _T("Users"), _T("Nothing important about them !!"));
}

void CMainFrame::OnChangetextForkeysicon()
{
    CSysTrayIconTipChange dlgForTipChange;
    
    if (dlgForTipChange.DoModal() == IDOK)
    {
        m_stiIcon.ChangeIconTip(1, dlgForTipChange.m_strNewIconTip);
    }
}

void CMainFrame::OnChangetextInfo()
{
    CSysTrayIconTipChange dlgForTipChange;
    
    if (dlgForTipChange.DoModal() == IDOK)
    {
        m_stiIcon.ChangeIconTip(4, dlgForTipChange.m_strNewIconTip);
    }
}

void CMainFrame::OnChangetextQuestion()
{
    CSysTrayIconTipChange dlgForTipChange;
    
    if (dlgForTipChange.DoModal() == IDOK)
    {
        m_stiIcon.ChangeIconTip(2, dlgForTipChange.m_strNewIconTip);
    }
}

void CMainFrame::OnChangetextUser()
{
    CSysTrayIconTipChange dlgForTipChange;
    
    if (dlgForTipChange.DoModal() == IDOK)
    {
        m_stiIcon.ChangeIconTip(3, dlgForTipChange.m_strNewIconTip);
    }
}

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)


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

Comments and Discussions