托盘图标动画 [英] Tray Icon animation

查看:218
本文介绍了托盘图标动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know how to place a icon in the Windows notification area (system tray).

什么是有一个图标动画的最佳方法?您可以使用gif动画,或者你必须依靠定时器?

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?

我使用C#和WPF,但WinForms的接受了。

I'm using C# and WPF, but WinForms accepted too.

推荐答案

Abhinaba Basu的博客帖子的动画和文字系统托盘中使用C#解释说。

它归结为:


  • 制作的图标每一个代表一个动画帧的数组。

  • 切换图标上的定时器事件

  • 托盘创建位图带。每一帧是16×16像素

  • 使用 SysTray.cs

  • 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

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

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

要动画帧 StartAnimation 启动计时器,并在计时器的图标更改为动画的整个序列。

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



使用系统托盘

创建来包装你的菜单

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



创建具有动画帧图像条。对于6架带图像将有6 * 16的宽度和高度为16像素

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

要停止动画通话

m_sysTray.StopAnimation();

这篇关于托盘图标动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆