Click here to Skip to main content
15,895,142 members
Articles / Programming Languages / C++

Easy Animated Tray Icon

Rate me:
Please Sign up or sign in to vote.
4.64/5 (12 votes)
4 Feb 20023 min read 215.8K   5.8K   69  
This is a class (SS_TrayIcon) that allows for the easy addition of a tray icon into any project (whether you use MFC or not).
// ----------------------------------------------------------------------- //
//
//  FILENAME:	SS_Wnd.cpp
//  AUTHOR:		Steve Schaneville
//  CREATED:	22 Jan 2002, 11:26
//
//  PURPOSE:	
//
//  Copyright (c) 2002
//
// ----------------------------------------------------------------------- //

// ------------------[       Pre-Include Defines       ]------------------ //
// ------------------[          Include Files          ]------------------ //
#include "SS_Wnd.h"
//#include "resource.h"
#include <list>
using namespace std;

// ------------------[      Macros/Constants/Types     ]------------------ //

typedef struct SSWndHandleMapEntry
{
    HWND    hWnd;
    SS_Wnd* pWnd;
} SSWND_HANDLE_MAP_ENTRY;

typedef list<SSWndHandleMapEntry> HandleMap;
typedef list<SSWndHandleMapEntry>::iterator HandleMapIterator;

// ------------------[         Global Variables        ]------------------ //

// the instance handle
HINSTANCE g_hInstance = NULL;

// Static variable to keep track of whether or not we've already registered 
// the SS_Wnd class.
BOOL SS_Wnd::m_bClassIsRegistered = FALSE;

// Mapping table to help us send the callback messages to the proper class instance.
HandleMap g_HandleMap;

// ------------------[         Global Functions        ]------------------ //

// look up the pWnd based on the hWnd
SS_Wnd* FindWindowInstance(HWND hWnd)
{
    SS_Wnd* pWnd = NULL;
    HandleMapIterator it = g_HandleMap.begin();
    while( it != g_HandleMap.end() )
    {
        if( it->hWnd == hWnd )
            return it->pWnd;
        
        it++;
    }

    return NULL;
}

// The global callback message handling function for SS_Wnd.
LRESULT CALLBACK SSWndWindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    WNDCLASS wndClass;
    ::GetClassInfo(g_hInstance, SSWND_REGISTERED_NAME, &wndClass);
    if( !_tcscmp(wndClass.lpszClassName, SSWND_REGISTERED_NAME) )
    {
        SS_Wnd* pWnd = FindWindowInstance(hWnd);
        if( pWnd )
            return pWnd->WindowProc(message, wParam, lParam);        
    }

    return 1;
}

// The global callback timer handling function.
VOID CALLBACK TimerProc(HWND hWnd, UINT uMsg, UINT idEvent, DWORD dwTime)
{
    SS_Wnd* pWnd = FindWindowInstance(hWnd);
    if( pWnd )
        pWnd->OnTimer(uMsg, idEvent, dwTime);
}

// ------------------[    Class Function Definitions   ]------------------ //

// Standard Constructor
SS_Wnd::SS_Wnd(HINSTANCE hInstance)
{
    InitObject();
    Instance(hInstance);
    RegisterClass();
}

// Standard Destructor
SS_Wnd::~SS_Wnd()
{
}

// Copy Constructor
SS_Wnd::SS_Wnd( SS_Wnd& rhs )
{
    InitObject();
    *this = rhs;
}

// Assignment Operator
SS_Wnd& SS_Wnd::operator = ( SS_Wnd& rhs )
{
    InitObject();

    return *this;
}

// Object Initialization
VOID SS_Wnd::InitObject()
{
    m_hInstance = NULL;
    m_hWnd = NULL;
}

VOID SS_Wnd::RegisterClass()
{
    if( !m_bClassIsRegistered )
    {
        WNDCLASSEX wce;
        
        wce.cbSize = sizeof(wce);
        wce.style = NULL;
        wce.lpfnWndProc = SSWndWindowProc;
        wce.cbClsExtra = 0;
        wce.cbWndExtra = 0;
        wce.hInstance = Instance();
        wce.hIcon = NULL;
        wce.hIconSm = NULL;
        wce.hCursor = NULL; // LoadCursor(Instance(), MAKEINTRESOURCE(IDC_POINTER));
        wce.hbrBackground = NULL;
        wce.lpszMenuName = NULL;
        wce.lpszClassName = SSWND_REGISTERED_NAME;
        
        ::RegisterClassEx(&wce);

        m_bClassIsRegistered = TRUE;
        g_hInstance = m_hInstance;
    }
}

VOID SS_Wnd::Create()
{
    // create the window
    HWND hWnd = CreateWindowEx( NULL,
                                SSWND_REGISTERED_NAME,
                                _T(""),
                                NULL,
                                0,
                                0,
                                0,
                                0,
                                NULL,
                                NULL,
                                Instance(),
                                NULL );

    // store the handle in the classes member function
    WindowHandle(hWnd);

    // save the handle into the handle map for callback messages
    SSWndHandleMapEntry hme;
    hme.hWnd = hWnd;
    hme.pWnd = this;
    g_HandleMap.push_front(hme);
}

LRESULT SS_Wnd::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
    switch( message )
    {
        case WM_TIMER:
            OnTimer(message, (UINT_PTR)wParam, (DWORD)lParam);
    	break;

        default:
        {
        }
    }

    return 1;
}

VOID SS_Wnd::OnTimer(UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
}

UINT_PTR SS_Wnd::SetTimer(UINT nIDEvent, UINT uElapse, TIMERPROC lpTimerProc)
{
    if( NULL == lpTimerProc )
        lpTimerProc = TimerProc;

    return ::SetTimer(m_hWnd, nIDEvent, uElapse, lpTimerProc);
}

BOOL SS_Wnd::KillTimer(UINT nIDEvent)
{
    return ::KillTimer(m_hWnd, nIDEvent);
}

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


Written By
Architect Amedisys
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions