【问题标题】:Tray Icon animation托盘图标动画
【发布时间】:2010-10-08 06:34:18
【问题描述】:

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

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

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

【问题讨论】:

    标签: c# wpf system-tray notifyicon trayicon


    【解决方案1】:

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

    归结为:

    • 制作一组图标,每个图标代表一个动画帧。
    • 根据定时器事件切换托盘中的图标
    • 创建位图条。每帧为 16x16 像素
    • 使用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++;
        }
        ....
    }
    

    使用系统托盘

    创建并连接您的菜单

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

    【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多