Tray Icon animation

Click Ok picture Click Ok · Feb 18, 2009 · Viewed 17.7k times · Source

I know how to place a icon in the Windows notification area (system tray).

What is the best method to have an icon animate? Can you use an animated gif, or do you have to rely on a timer?

I'm using C# and WPF, but WinForms accepted too.

Answer

Scott Hanselman picture Scott Hanselman · Feb 25, 2009

Abhinaba Basu's blog post Animation and Text in System tray using C# explains.

It comes down to:

  • making an array of icons each of which represent an animation frame.
  • switching the icons in the tray on timer events
  • create a bitmap strip. Each frame is 16x16 pixels
  • use SysTray.cs

e.g.

enter image description here

private void button1_Click(object sender, System.EventArgs e)
{
    m_sysTray.StopAnimation();
    Bitmap bmp = new Bitmap("tick.bmp");
    // the color from the left bottom pixel will be made transparent
    bmp.MakeTransparent();
    m_sysTray.SetAnimationClip(bmp);
    m_sysTray.StartAnimation(150, 5);
}

SetAnimationClip uses the following code to create the animation frame

public void SetAnimationClip (Bitmap bitmapStrip)
{
    m_animationIcons = new Icon[bitmapStrip.Width / 16];
    for (int i = 0; i < m_animationIcons.Length; i++)
    {
        Rectangle rect = new Rectangle(i*16, 0, 16, 16);
        Bitmap bmp = bitmapStrip.Clone(rect, bitmapStrip.PixelFormat);
        m_animationIcons[i] = Icon.FromHandle(bmp.GetHicon());
    }
}

To animate the frame StartAnimation starts a timer and in the timer the icons are changed to animate the whole sequence.

public void StartAnimation(int interval, int loopCount)
{
    if(m_animationIcons == null)
        throw new ApplicationException("Animation clip not set with    
                                        SetAnimationClip");

    m_loopCount = loopCount;
    m_timer.Interval = interval;
    m_timer.Start();
}

private void m_timer_Tick(object sender, EventArgs e)
{
    if(m_currIndex < m_animationIcons.Length)
    {
        m_notifyIcon.Icon = m_animationIcons[m_currIndex];
        m_currIndex++;
    }
    ....
}

Using SysTray

Create and wire up your menu

ContextMenu m_menu = new ContextMenu();                                   
m_menu.MenuItems.Add(0, new MenuItem("Show",new
                     System.EventHandler(Show_Click)));

Get an icon you want to show statically in the tray.

Create a SysTray object with all the required information

m_sysTray = new SysTray("Right click for context menu",
            new Icon(GetType(),"TrayIcon.ico"), m_menu);

Create image strips with the animation frames. For 6 frame strip the image will have a width of 6*16 and height as 16 pixels

Bitmap bmp = new Bitmap("tick.bmp");
// the color from the left bottom pixel will be made transparent
bmp.MakeTransparent();
m_sysTray.SetAnimationClip(bmp);

Start animation indicating how many times you need to loop the animation and the frame delay

m_sysTray.StartAnimation(150, 5);

To stop animation call

m_sysTray.StopAnimation();