Animated Tray Icon Class | CodeGuru

Animated Tray Icon Class

There was one reason why I sat down to write this class. And as is usually the case, necessity is the mother of invention. Well, it started off like this. I had a time consuming task going on and during this time I wanted to have an animated tray icon to appear in the tray […]

Written By
CodeGuru Staff
CodeGuru Staff
Mar 9, 2000
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

There was one reason why I sat down to write this class. And as is usually
the case, necessity is the mother of invention.
Well, it started off like this. I had a time consuming task going on and
during this time I wanted to have an animated tray icon to appear in the tray
to indicate the ongoing task. When this task got over, I needed a way to
remove this. The examples I’d seen earlier couldn’t by themselves address to
my problem as, the task which I was performing was on a thread and since it
didn’t have any window, there was no proper way to set a timer or whatever to
animate the icon in the tray. So, I was looking at some way to accomplish the
same by making the animation to happen on a different thread than the one in
which it is being invoked.

Basically, my anitrayicon class’ constructor starts off by creating a thread
and then this Thread Procedure creates an invisible window.
The handle to this window is set into anitrayicon object’s m_hWnd. All
further messages to the tray icon are basically messages sent to this window.
Thus, the main function of the WndProc for the Trayicon window is to wait for
messages and call various Shell_Notifyicon() to accomplish the desired action
and to animate the icon on a WM_TIMER message.

Here is how one uses it :

Using the AnimatedTrayIcon class

Have an object on the stack / heap.

void myFunc()
{
 AnimatedTrayIcon oTrayIcon;
 HICON* hIconArray = new HICON[5];
 hIconArray[0] = LoadIcon(............);//handle to icon 1
 hIconArray[1] = LoadIcon(............);//handle to icon 2
 hIconArray[2] = LoadIcon(............);//handle to icon 3
 hIconArray[3] = LoadIcon(............);//handle to icon 4
 hIconArray[4] = LoadIcon(............);//handle to icon 5
 oTrayIcon.SetFrameIconInfo(hIconArray,5);
 oTrayIcon.SetFrameRate(250);//in ms
 oTrayIcon.Start();
 ..
 ..
 ..
 ..

 //u can stop the tray icon here if u want by calling 
 oTrayIcon.Stop();
 ..
 ..
 //and then restart it at a later stage by calling 
 ..
 oTrayIcon.Continue();

 //long process.....

 //one needn't necessarily call Remove() if that is the only tray icon one 
 //needs to have the destructor automatically removes the icon 
}

Downloads

Download source – 3 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.