简体   繁体   English

托盘图标动画

[英]Tray Icon animation

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也接受了。

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 每帧为16x16像素
  • use SysTray.cs 使用SysTray.cs

eg 例如

在此输入图像描述

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM