Tray Icon animation
Asked Answered
M

2

20

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.

Mulligrubs answered 18/2, 2009 at 4:31 Comment(0)
P
28

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();
Purificator answered 25/2, 2009 at 22:20 Comment(1)
Make sure to check the comments on that article: "Shame on me :( There are many leaks in the code." (blogs.msdn.com/b/abhinaba/archive/2005/09/12/…)Academic
I
3

I think the best way to do this is to have multiple small icons which you can continue to change the systray object to the new picture based on the speed and the time.

Incorporeal answered 18/2, 2009 at 4:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.