2009-02-18 74 views
15

我知道如何在Windows通知区域(系统托盘)中放置图标。托盘图标动画

让图标变成动画的最佳方法是什么?你可以使用动画GIF,还是你必须依靠计时器?

我正在使用C#和WPF,但也接受了WinForms。

回答

22

Abhinaba Basu's blog post Animation and Text in System tray using C#解释。

它归结为:

  • 使得图标的每一个代表一个动画帧的阵列。
  • 在定时器事件中切换托盘中的图标
  • 创建位图条。每帧是16×16像素
  • 使用SysTray.cs

例如

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使用以下代码来创建的动画帧

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

要动画帧StartAnimation启动定时器并且在该定时器的图标改变为动画整个序列。

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++; 
    } 
    .... 
} 

使用系统托盘

创建和电线您的菜单

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

获取要在托盘静态显示的图标。

具有所有所需要的信息

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

创建具有动画帧图像条创建系统托盘对象。为6帧条上的图像将具有6×16的宽度和高度作为16个像素

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

要停止动画调用

m_sysTray.StopAnimation(); 
+4

请一定要检查该文章的评论:“可耻的是我:(里有很多代码泄漏”(http://blogs.msdn.com/b/abhinaba/archive/2005/09/12 /动画和文本在系统托盘-使用-C。 ASPX#504147) – 2012-01-05 00:30:56

2

我认为最好的办法是拥有多个小图标,您可以根据速度和时间继续将系统托盘对象更改为新图片。