Click here to Skip to main content
12,557,750 members (56,789 online)
Click here to Skip to main content


46 bookmarked

Tray Icon Class with Icon Animation Abilities

, 14 Dec 2003
A class which makes tray icons management and animation really easy
#pragma once

#include <afxtempl.h>

//  CakTrayIcon
class CakTrayIcon

    static const UINT WM_TRAYICONNOTIFY;

    CakTrayIcon(CWnd *pWnd, UINT Id, HICON Icon, LPCTSTR Tip);

    void Create(CWnd *pWnd, UINT Id);

    void SetIcon(HICON Icon);
    void SetTip(LPCTSTR Tip);
    void SetIconAndTip(HICON Icon, LPCTSTR Tip);

    void GetIconAndTip(HICON *pIcon, CString *pTip);
    UINT GetId();

    void PopupBalloon(LPCTSTR Info, LPCTSTR InfoTitle, DWORD Flags, UINT Timeout = 10000);

    HWND m_Master;
    UINT m_Id;
    HICON m_Icon;
    CString m_Tip;

    void InitializeTrayIconStruct(NOTIFYICONDATA *pStruct, UINT Flags = 0, 


//  CakIconLoader
class CakIconLoader

    CakIconLoader(UINT ResourceId, HMODULE hModule = 0);
    CakIconLoader(LPCTSTR ResourceId, HMODULE hModule = 0);

    UINT GetIconsCount();
    HICON GetIcon(UINT Index);

    CArray<CArray<BYTE> > m_Frames;
    HMODULE m_hModule;

    void Initialize(HRSRC hResource);
    void LoadFrame(UINT Id, CArray<BYTE> *pFrame);

    HICON m_CurrentIcon;
    void DeleteCurrentIcon();

#pragma pack(push)
#pragma pack(2)
        BYTE bWidth;                                    // Width, in pixels, of the image
        BYTE bHeight;                                   // Height, in pixels, of the image
        BYTE bColorCount;                               // Number of colors in image (0 if >=8bpp)
        BYTE bReserved;                                 // Reserved ( must be 0)
        WORD wPlanes;                                   // Color Planes
        WORD wBitCount;                                 // Bits per pixel
        DWORD dwBytesInRes;                             // How many bytes in this resource?
        WORD nID;                                       // Where in the file is this image?

    struct ICONDIR
        WORD idReserved;                                // Reserved (must be 0)
        WORD idType;                                    // Resource Type (1 for icons)
        WORD idCount;                                   // How many images?
        ICONDIRENTRY idEntries[1];                      // An entry for each image (idCount of 'em)
#pragma pack(pop)

    struct ICONIMAGE
        UINT Width, Height, Colors;                     // Width, Height and bpp
        LPBYTE lpBits;                                  // ptr to DIB bits
        DWORD dwNumBytes;                               // how many bytes?
        LPBITMAPINFO lpbi;                              // ptr to header
        LPBYTE lpXOR;                                   // ptr to XOR image bits
        LPBYTE lpAND;                                   // ptr to AND image bits

    LPSTR FindDIBBits(LPSTR lpbi);
    WORD DIBNumColors(LPSTR lpbi);
    WORD PaletteSize(LPSTR lpbi);
    bool AdjustIconImagePointers(ICONIMAGE *lpImage);


//  CakTrayIconAnimator
class CakTrayIconAnimator

    CakTrayIconAnimator(CakTrayIcon *pMaster, UINT ResourceId, LPCTSTR Tip = 0);
    CakTrayIconAnimator(CakTrayIcon *pMaster, LPCTSTR ResourceId, LPCTSTR Tip = 0);

    void Animate(UINT FrameDelay = 300);

    CakTrayIcon *m_pMaster;
    HICON m_OldIcon;
    CString m_OldTip;

    CString m_Tip;

    CakIconLoader m_Loader;

    void Initialize(LPCTSTR Tip);

    static UINT AnimateProc(LPVOID lpVoid);

    struct AnimateParams
        CakTrayIcon *pMaster;
        CakIconLoader *pLoader;
        UINT FrameDelay;
        CString StopEventName;

    AnimateParams m_AnimateParams;

    HANDLE m_hAnimateThread;
    CString m_StopEventName;
    UINT m_FrameDelay;


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.


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


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

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