Animate System Tray in C#

Description:

I am sure those who have worked with Visual C++ would have experienced the problem of displaying an icon in system tray and manipulating with context menus for that. Previously I use to display System tray icons with Shell_NotifyIcon by filling a structure and manipulating with a context menu for that icon is another problem. We need to create a menu dynamically and call TrackPopMenu and call DestroyMenu().

This application gives you a picture of how to use .NET's TrayIcon class. With the introduction of this class in .NET, no need to call Shell_NotifyIcon Win32 API and TrackPopMenu() and DestroyMenu() APIs. Designing of your icon, creating context menu, adding event handlers are all done during design time itself. The complete source code is available in AnimateTray.zip file.

Source Code:

/* Timer handling function */
public void AnimateIcon(object sender, System.EventArgs e)
{
if ( m_Icon1 != null && m_Icon2 != null )
{
if ( m_bIconFlag == true )
{
m_trayIcon.Icon = m_Icon2;
m_bIconFlag =
false;
}
else
{
m_trayIcon.Icon = m_Icon1;
m_bIconFlag =
true;
}
}
/* Start menu handler */
protected void OnClickStop (object sender, System.EventArgs e)
{
m_timer.Start();
menuItem4.Enabled =
true;
menuItem1.Enabled =
false;
}
/* Stop menu handler */
protected void OnClickStop (object sender, System.EventArgs e)
{
m_timer.Stop();
menuItem4.Enabled =
false;
menuItem1.Enabled =
true;
}

Clicking the right mouse button on the system tray icon, a menu is populated. Selecting Start option from the menu will create a timer. This timer object is associated with AnimateIcon() function. m_Icon1 and m_Icon2 are which are associated with Icon1.ico and Icon2.ico files resp .The two icon files Icon1.ico and Icon2.ico should exist in the same directory.

Up Next
    Ebook Download
    View all
    Learn
    View all