首页 > 代码库 > C#语言基础知识(8):C# 托盘图标闪烁

C#语言基础知识(8):C# 托盘图标闪烁

在用户正在登录QQ或者使用Firemail邮件系统自动收取邮件的时候,托盘图标会闪动提示用户正在运行的任务。闪动图标可以使用定时切换托盘图标的方式实现,托盘图标可以从ImageList控件中获取。在ImageList控件里面添加三个icon,第一个icon表示窗体启动以后的托盘图标。第二个和第三个图标分别表示当特定的任务发生的时候,定时切换的图标。

  1 using System;  2 using System.Collections.Generic;  3 using System.ComponentModel;  4 using System.Data;  5 using System.Drawing;  6 using System.Text;  7 using System.Windows.Forms;  8 using System.Threading;  9  10 namespace JajaWeixin.MyClient 11 { 12     public partial class Flashing : Form 13     { 14         //切换图片的标识 15         private bool iconFlag = false;//加载图标切换状态是停止状态 16  17         //系统是否运行 18         private bool isRun = false;//加载状态运行是停止 19  20         //设置icon显示的图片 21         public Flashing() 22         { 23             InitializeComponent(); 24             this.setIconImg(0);//加载窗体时托盘显示的图标是下标为0的那张图片 25         } 26          27         //客户端查询订单每隔5秒 28         private void timer_dingdanchaxun_Tick(object sender, EventArgs e) 29         { 30  31             string jiudianId = "201406261626"; 32             string objGuid = Convert.ToString(System.Guid.NewGuid()); 33             string result = jiudianId + objGuid; 34             string desString = DESEncrypt.Encrypt(result); 35             mylocalhost.MyWebService serviceone = new mylocalhost.MyWebService(); 36             string str = serviceone.Messagetoremind(desString); 37             //MessageBox.Show(serviceone.Messagetoremind(desString)); 38             //return; 39             if (str != "") 40             { 41                 this.isRun = true;//开始闪烁 42             } 43             else 44             { 45                 this.isRun = false; 46             } 47         } 48  49         //设置托盘的图标可以从Image对象转换为Icon对象 50         private void setIconImg(int index) 51         { 52             Image img=this.imgIcon.Images[index]; 53             Bitmap b = new Bitmap(img); 54             Icon icon = Icon.FromHandle(b.GetHicon()); 55             this.niMain.Icon = icon; 56         } 57  58         //显示主窗体 59         private void tsmiMain_Click(object sender, EventArgs e) 60         { 61             //显示主窗体 62             this.Visible = true; 63             this.WindowState = System.Windows.Forms.FormWindowState.Normal; 64             //隐藏托盘图标 65             this.niMain.Visible = false;//显示主窗体的时候,托盘图标隐藏 66             this.Show();//主窗体显示 67         } 68  69         //退出 70         private void tsmiExit_Click(object sender, EventArgs e) 71         { 72             //设置托盘的提示信息 73             this.niMain.BalloonTipText = "成功退出!"; 74             this.niMain.BalloonTipTitle = "退出"; 75             this.niMain.ShowBalloonTip(1000 * 3);//在任务栏中持续显示气球提示的指定时间 76             //延迟退出 77             Thread.Sleep(1000 * 2); 78             //释放托盘图标资源 79             this.niMain.Dispose(); 80             //终止线程 81             Application.ExitThread(); 82         } 83  84         //最小化时隐藏窗体,显示托盘图标 85         private void Flashing_SizeChanged(object sender, EventArgs e) 86         { 87             if (this.WindowState == FormWindowState.Minimized) 88             { 89                 this.Hide(); 90                 this.niMain.Visible = true; 91             } 92         } 93  94         //窗体的最小化按钮和关闭按钮实现隐藏窗体的功能,窗体关闭时,会执行FormClosing 事件, 95         //释放与此窗体关联的所有资源。因此需要取消关闭事件,实现窗体的隐藏和托盘的显示功能。 96         private void Flashing_FormClosing(object sender, FormClosingEventArgs e) 97         { 98             e.Cancel = true; 99             this.Hide();100             //this.ShowInTaskbar = false;//取消窗体在任务栏的显示101             this.niMain.Visible = true;102         }103 104         //加载窗体是隐藏窗体105         private void Flashing_Load(object sender, EventArgs e)106         {107             this.Hide();108         }109 110         //定时器切换图标显示111         private void tmrIcon_Tick(object sender, EventArgs e)112         {113             if (!this.isRun)114             {115                 return;116             }117             if (iconFlag)118             {119                 this.setIconImg(1);120                 iconFlag = !iconFlag;121             }122             else123             {124                 this.setIconImg(2);125                 iconFlag = !iconFlag;126             }127         }128 129         //点击运行菜单130         private void tsmiRun_Click(object sender, EventArgs e)131         {132             this.tsmiRun.Enabled = false;//运行按钮不可用133             this.tsmiStop.Enabled = true;//停止按钮可用134             //设置运行状态135             //this.isRun = true;//开始闪烁136             this.timer_dingdanchaxun.Enabled = true;137         }138 139         //点击停止菜单140         private void tsmiStop_Click(object sender, EventArgs e)141         {142             this.tsmiRun.Enabled = true;143             this.tsmiStop.Enabled = false;144             //设置为停止状态145             this.isRun = false;146             this.timer_dingdanchaxun.Enabled = false;147             //恢复图标显示148             this.setIconImg(0);149         }150 151     }152 }

 效果如图:

右击托盘图标