C# 托盘图标动画
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/559853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tray Icon animation
提问by Click Ok
I know how to place a icon in the Windows notification area (system tray).
我知道如何在 Windows 通知区域(系统托盘)中放置图标。
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?
使图标动画的最佳方法是什么?您可以使用动画 gif,还是必须依赖计时器?
I'm using C# and WPF, but WinForms accepted too.
我正在使用 C# 和 WPF,但 WinForms 也被接受了。
采纳答案by Scott Hanselman
Abhinaba Basu's blog post Animation and Text in System tray using C#explains.
Abhinaba Basu 的博客文章使用 C#解释了系统托盘中的动画和文本。
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
- 制作一组图标,每个图标代表一个动画帧。
- 在计时器事件上切换托盘中的图标
- 创建位图条。每帧为 16x16 像素
- 使用SysTray.cs
e.g.
例如
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
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());
}
}
To animate the frame StartAnimation
starts a timer and in the timer the icons are changed to animate the whole sequence.
动画帧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++;
}
....
}
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
使用所有必需的信息创建一个 SysTray 对象
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
使用动画帧创建图像条。对于 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);
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();
回答by Suroot
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.
我认为最好的方法是拥有多个小图标,您可以根据速度和时间继续将系统托盘对象更改为新图片。