Click here to Skip to main content
13,404,408 members (41,235 online)
Click here to Skip to main content

Stats

98.5K views
3.4K downloads
46 bookmarked
Posted 14 Dec 2003

Tray Icon Class with Icon Animation Abilities

, 14 Dec 2003
A class which makes tray icons management and animation really easy
// akTrayIconDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "akTrayIconDemo.h"
#include ".\akTrayIconDemoDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CakTrayIconDemoDlg dialog



CakTrayIconDemoDlg::CakTrayIconDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CakTrayIconDemoDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CakTrayIconDemoDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_ED_TIP, m_Tip);
    DDX_Control(pDX, IDC_ED_INFO, m_Info);
    DDX_Control(pDX, IDC_ED_TIMEOUT, m_Timeout);
    DDX_Control(pDX, IDC_ED_INFO_TITLE, m_InfoTitle);
    DDX_Control(pDX, IDC_CB_INFO_TYPE, m_InfoType);
    DDX_Control(pDX, IDC_ED_ANIM_TIP, m_AnimTip);
}

BEGIN_MESSAGE_MAP(CakTrayIconDemoDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
    ON_EN_SETFOCUS(IDC_ED_TIP, OnEnSetfocusEdTip)
    ON_EN_SETFOCUS(IDC_ED_INFO, OnEnSetfocusEdInfo)
    ON_EN_SETFOCUS(IDC_ED_TIMEOUT, OnEnSetfocusEdTimeout)
    ON_REGISTERED_MESSAGE(CakTrayIcon::WM_TRAYICONNOTIFY, OnTrayIconNotify)
    ON_BN_CLICKED(IDC_BTN_CHANGE, OnBnClickedBtnChange)
    ON_BN_CLICKED(IDC_BTN_SHOW, OnBnClickedBtnShow)
    ON_BN_CLICKED(IDC_BTN_ANIMATE, OnBnClickedBtnAnimate)
    ON_EN_SETFOCUS(IDC_ED_INFO_TITLE, OnEnSetfocusEdInfoTitle)
    ON_CBN_SETFOCUS(IDC_CB_INFO_TYPE, OnCbnSetfocusCbInfoType)
    ON_EN_SETFOCUS(IDC_ED_ANIM_TIP, OnEnSetfocusEdAnimTip)
END_MESSAGE_MAP()


// CakTrayIconDemoDlg message handlers

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

	// 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

    m_Tip.SetWindowText(_T("Enter some tip here"));
    m_Info.SetWindowText(_T("Here's the info for popup"));
    m_InfoTitle.SetWindowText(_T("Popup title"));
    m_Timeout.SetWindowText(_T("15"));
    m_InfoType.SetCurSel(0);
    m_AnimTip.SetWindowText(_T("Some lenghly operation's performing..."));

	m_TrayIcon.Create(this, 1);
    m_TrayIcon.SetIconAndTip(LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_SMILE)), 
        _T("This is a tray icon"));
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CakTrayIconDemoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CakTrayIconDemoDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CakTrayIconDemoDlg::OnEnSetfocusEdTip()
{
    SetDefID(IDC_BTN_CHANGE);
}

void CakTrayIconDemoDlg::OnEnSetfocusEdInfo()
{
    SetDefID(IDC_BTN_SHOW);
}

void CakTrayIconDemoDlg::OnEnSetfocusEdTimeout()
{
    SetDefID(IDC_BTN_SHOW);
}

void CakTrayIconDemoDlg::OnEnSetfocusEdInfoTitle()
{
    SetDefID(IDC_BTN_SHOW);
}

void CakTrayIconDemoDlg::OnCbnSetfocusCbInfoType()
{
    SetDefID(IDC_BTN_SHOW);
}

void CakTrayIconDemoDlg::OnEnSetfocusEdAnimTip()
{
    SetDefID(IDC_BTN_ANIMATE);
}

LRESULT CakTrayIconDemoDlg::OnTrayIconNotify(WPARAM wParam, LPARAM lParam)
{
    if (1 == wParam)
    {
        //  Handle notifications only from icon with id = 1
        if (WM_LBUTTONDBLCLK == lParam)
        {
            AfxMessageBox("Double clicked the icon!");
        }
        if (WM_RBUTTONUP == lParam)
        {
            CPoint pt;
            GetCursorPos(&pt);
            CMenu mnu;
            mnu.LoadMenu(IDR_TRAYMENU);
            SetForegroundWindow();
            mnu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON, pt.x, pt.y, this);
            PostMessage(WM_NULL);
        }
    }
    return 0;
}

void CakTrayIconDemoDlg::OnBnClickedBtnChange()
{
    CString tip;
    m_Tip.GetWindowText(tip);
    m_TrayIcon.SetTip(tip);
}

void CakTrayIconDemoDlg::OnBnClickedBtnShow()
{
    CString title, info, timeout;
    m_Info.GetWindowText(info);
    m_InfoTitle.GetWindowText(title);
    m_Timeout.GetWindowText(timeout);
    DWORD to = atoi(timeout) * 1000;
    const DWORD Flags[] = { NIIF_NONE, NIIF_ERROR, NIIF_INFO };
    m_TrayIcon.PopupBalloon(info, title, Flags[m_InfoType.GetCurSel()], to ? to : 10000);
}

void CakTrayIconDemoDlg::OnBnClickedBtnAnimate()
{
    //  Add another tray icon for animation purposes
    CakTrayIcon ti;
    ti.Create(this, 2);

    CString tip;
    m_AnimTip.GetWindowText(tip);

    CakTrayIconAnimator tia(&ti, IDI_HOURGLASS, tip);
    tia.Animate();
    AfxMessageBox(_T("Some lengthly operation in progress.\nClick OK when you tired to look at the hourglass spinning around..."), MB_OK);
}

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

Alex Kolesnichenko
Software Developer (Senior)
United States United States
Started professional career in software development back in 2000, in Ukraine. Founder and owner of a boutique software company called ByteGems.com Software. Worked for 6 years at w2bi, Inc in New Jersey USA, currently work in a large multinational company based in Redmond, WA.

My buzzwords at the moment: .NET, C#, ASP.NET, MVC, LINQ, TypeScript, JavaScript, AngularJS, HTML, JSON, services.

Still buzzing: C++, Win32, ATL, MFC, SQL, WinForms, WebForms, EF, Sockets, TCP/IP, Remoting.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1802120.2 | Last Updated 15 Dec 2003
Article Copyright 2003 by Alex Kolesnichenko
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid