Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
660 views
in Technique[技术] by (71.8m points)

c# - Tray Icon animation

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...