Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

Animated System Tray Icon, Taskbar Icon, and Titlebar Icon

4.65/5 (25 votes)
14 Jul 20062 min read 2   4.2K  
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