Click here to Skip to main content
15,867,594 members
Articles / Programming Languages / C++
Article

Animated System Tray Icon, Taskbar Icon, and Titlebar Icon

Rate me:
Please Sign up or sign in to vote.
4.65/5 (25 votes)
14 Jul 20062 min read 91.6K   4.2K   59   4
An article on animating system tray, taskbar and title bar icons.

Sample ImageSample Image

Taskbar Icon Animation

Sample Image

Titlebar Icon Animation

Sample Image

Introduction

I have came across many solutions to display an icon on the system tray, taskbar, or the title bar, but I could not find code for animating the icon without using MFC. When I started to work with animating system tray icons, I examined the possibility of doing the same on the taskbar and titlebar icons. You can find many “classes” to integrate with your project to do the same. This project is developed without using MFC; it’s a pure Win32 “C” code which has the following features.

Feature

  • Animation of tray Icon
  • Animation of Taskbar icon
  • Animation of Titlebar icon
  • Tray icon notification with menu to control the application window

System Tray Icon

First, a simple Win32 application is created by registering a local class. Create a set of custom icons (.ico files) using the workspace.

void DrawGraph(HDC hdc, RECT Rect);

The DrawGraph routine is used to draw the graph using the specified device context. The graph size is specified using the Rect parameter. Also, the graph’s vertical movement is implemented in this routine.

void AnimateIcon(HINSTANCE hInstance, HWND hWnd, 
                 DWORD dwMsgType,UINT nIndexOfIcon);

The user defined function AnimateIcon contains the Shell_NotifyIcon API with the NOTIFYICONDATA structure to draw an icon on the system tray. While using the Shell_NotifyIcon API, the first parameter would identify the message types like:

  1. NIM_ADD
  2. NIM_MODIFY
  3. NIM_DELETE

NIM_ADD is used to add a new icon to the system tray, which is called at initialization. The NIM_MODIFY message is used to modify the particular system tray icon, and NIM_DELETE is used to remove the icon from the system tray. The third parameter in the AnimateIcon function denotes the message type.

void AnimateIcon(HINSTANCE hInstance, HWND hWnd, 
                 DWORD dwMsgType,UINT nIndexOfIcon)
{
    HICON hIconAtIndex = LoadIcon(hInstance, 
      (LPCTSTR) MAKEINTRESOURCE(IconResourceArray[nIndexOfIcon]));
    NOTIFYICONDATA IconData;
    IconData.cbSize = sizeof(NOTIFYICONDATA);
    IconData.hIcon  = hIconAtIndex;
    IconData.hWnd   = hWnd;
    lstrcpyn(IconData.szTip,"Animated Icons - Demo", 
            (int) strlen("Animated Icons - Demo")+1);
    IconData.uCallbackMessage = MYMSG_NOTIFYICON;

    IconData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;

    Shell_NotifyIcon(dwMsgType, &IconData);
    SendMessage(hWnd, WM_SETICON, NULL, (long) hIconAtIndex);
    if(hIconAtIndex)
        DestroyIcon(hIconAtIndex);
}

MYMSG_NOTIFYICON is the callback notification message. When the cursor does any action on the icon, this callback function will be get executed. When we right click on the tray icon, the MYMSG_NOTIFYICON message is posted.

case MYMSG_NOTIFYICON:
    OnTrayNotification(hInst, hWnd, wParam, lParam);
    break;

In this message handler, the OnTrayNotification function is called to load the menu and display it in the tray icon. The menu contains the following functionalities:

  1. Kill process
  2. Minimize window
  3. Maximize window
  4. Hide window

The functionality of the menu action should be handled in the WndProc callback routine.

Taskbar Icon and Titlebar Icon

To draw a custom icon on the taskbar and the titlebar, send a WM_SETICON message by passing the icon handle through the LPARAM parameter. This will just draw an icon on the bar. Using the DestroyIcon function, we can remove the icon from the bar. To animate these icons, a timer function called TimerAnimationIcon is created. This will select the next icon from the icon list structure and call the AnimateIcon function with the NIM_MODIFY message flag. Now, the new icon is displayed by overwriting the earlier one.

void TimerAnimationIcon(HINSTANCE hInst, HWND hWnd)
{
    AnimateIcon(hInst, hWnd, NIM_MODIFY, nCounter);
    m_nCounter = nCounter;
    nCounter++;
    nCounter = nCounter%(NUM_ICON_FOR_ANIMATION);
}

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 Luxoft
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralHow to add Menu on Title/Caption bar Pin
gawadepd17-Apr-11 22:10
gawadepd17-Apr-11 22:10 
How to add Menu on Title/Caption bar
Hi,

Please help me to add Menu on Title/Caption bar. I want's to add the menus along with system buttons, application logos.
If you are having any clue please revert back to me.

Thanks in Advance,
Pravin

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.