托盘图标动画

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

有图标动画的最佳方法是什么? 你可以使用动画gif,还是你必须依赖计时器?

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

Abhinaba Basu的博客文章使用C#解释了系统托盘中的动画和文本

它归结为:

  • 制作一组图标,每个图标代表一个动画帧。
  • 在计时器事件中切换托盘中的图标
  • 创建一个位图条。 每帧为16×16像素
  • 使用SysTray.cs

例如

在此处输入图像描述

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

使用SysTray

创建并连接菜单

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

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

使用所有必需信息创建SysTray对象

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

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