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

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

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

有帮助吗?

解决方案

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

归结为:

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

例如

enter image description here

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

其他提示

我想做到这一点,最好的办法就是有多个小图标,你可以继续在系统托盘对象更改为基于速度和时间的新图片。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top