随笔 - 3458, 文章 - 0, 评论 - 739, 阅读 - 1188万
  管理

C#托盘图标动画效果 - 开源研究系列文章

Posted on   lzhdim  阅读(10677)  评论(0编辑  收藏  举报

       这次在编写一个CPU使用率的小应用,上次发布了获取CPU使用率的代码,这次研究的是托盘图标的动画效果的问题。

       托盘图标的动画效果,其实是多个图标,然后在时间内进行切换显示,形成的动画效果。这里笔者推荐两种方法:

  1、 Timer计时器方法;

  就是设置一个Timer计时器,然后更改计时器的定时时间,进行切换不同的图标。

复制代码
/// <summary>
/// 图标动画显示计时器
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TimerAnimation_Tick(object sender, EventArgs e)
{
    if (icons.Length <= current) current = 0;
    this.NICPURater.Icon = icons[current];
    current = (current + 1) % icons.Length;
}

/// <summary>
/// CPU使用率获取显示计时器
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TimerCPUUseage_Tick(object sender, EventArgs e)
{
    interval = Math.Min(100, cpuUsage.NextValue());
    this.NICPURater.Text = $"CPU: {interval:f1}%";
    interval = 200.0f / (float)Math.Max(1.0f, Math.Min(20.0f, interval / 5.0f));

    this.TimerAnimation.Stop();
    this.TimerAnimation.Interval = (int)interval;
    this.TimerAnimation.Start();
}
复制代码

  2、 无限循环法;

  设置一个无限循环,控制Thread,Sleep的时间进行切换。

复制代码
private void Anmination()
{
    while (!_IsDone)
    {
        if (icons.Length <= current) current = 0;
        this.NICPURater.Icon = icons[current];
        current = (current + 1) % icons.Length;

        Thread.Sleep((int)interval);
    }
}
复制代码

       这里介绍了两种托盘图标动画切换的方式,具体请读者自己去实践应用。

相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2025年3月3日 星期一 【蛇】己卯月辛未日 乙巳年 二月初四 全国爱耳日
您的IP:18.222.255.180,操作系统:未知操作系统,浏览器:未知浏览器
Copyright (C) 2000-2025 Lzhdim Software All Rights Reserved
点击右上角即可分享
微信分享提示