在用户正在登录QQ或者使用Firemail邮件系统自动收取邮件的时候,托盘图标会闪动提示用户正在运行的任务。闪动图标可以使用定时切换托盘图标的方式实现,托盘图标可以从ImageList控件中获取。在ImageList控件里面添加三个icon,第一个icon表示窗体启动以后的托盘图标。第二个和第三个图标分别表示当特定的任务发生的时候,定时切换的图标。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading; namespace JajaWeixin.MyClient
{
public partial class Flashing : Form
{
//切换图片的标识
private bool iconFlag = false;//加载图标切换状态是停止状态 //系统是否运行
private bool isRun = false;//加载状态运行是停止 //设置icon显示的图片
public Flashing()
{
InitializeComponent();
this.setIconImg();//加载窗体时托盘显示的图标是下标为0的那张图片
} //客户端查询订单每隔5秒
private void timer_dingdanchaxun_Tick(object sender, EventArgs e)
{ string jiudianId = "";
string objGuid = Convert.ToString(System.Guid.NewGuid());
string result = jiudianId + objGuid;
string desString = DESEncrypt.Encrypt(result);
mylocalhost.MyWebService serviceone = new mylocalhost.MyWebService();
string str = serviceone.Messagetoremind(desString);
//MessageBox.Show(serviceone.Messagetoremind(desString));
//return;
if (str != "")
{
this.isRun = true;//开始闪烁
}
else
{
this.isRun = false;
}
} //设置托盘的图标可以从Image对象转换为Icon对象
private void setIconImg(int index)
{
Image img=this.imgIcon.Images[index];
Bitmap b = new Bitmap(img);
Icon icon = Icon.FromHandle(b.GetHicon());
this.niMain.Icon = icon;
} //显示主窗体
private void tsmiMain_Click(object sender, EventArgs e)
{
//显示主窗体
this.Visible = true;
this.WindowState = System.Windows.Forms.FormWindowState.Normal;
//隐藏托盘图标
this.niMain.Visible = false;//显示主窗体的时候,托盘图标隐藏
this.Show();//主窗体显示
} //退出
private void tsmiExit_Click(object sender, EventArgs e)
{
//设置托盘的提示信息
this.niMain.BalloonTipText = "成功退出!";
this.niMain.BalloonTipTitle = "退出";
this.niMain.ShowBalloonTip( * );//在任务栏中持续显示气球提示的指定时间
//延迟退出
Thread.Sleep( * );
//释放托盘图标资源
this.niMain.Dispose();
//终止线程
Application.ExitThread();
} //最小化时隐藏窗体,显示托盘图标
private void Flashing_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Hide();
this.niMain.Visible = true;
}
} //窗体的最小化按钮和关闭按钮实现隐藏窗体的功能,窗体关闭时,会执行FormClosing 事件,
//释放与此窗体关联的所有资源。因此需要取消关闭事件,实现窗体的隐藏和托盘的显示功能。
private void Flashing_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.Hide();
//this.ShowInTaskbar = false;//取消窗体在任务栏的显示
this.niMain.Visible = true;
} //加载窗体是隐藏窗体
private void Flashing_Load(object sender, EventArgs e)
{
this.Hide();
} //定时器切换图标显示
private void tmrIcon_Tick(object sender, EventArgs e)
{
if (!this.isRun)
{
return;
}
if (iconFlag)
{
this.setIconImg();
iconFlag = !iconFlag;
}
else
{
this.setIconImg();
iconFlag = !iconFlag;
}
} //点击运行菜单
private void tsmiRun_Click(object sender, EventArgs e)
{
this.tsmiRun.Enabled = false;//运行按钮不可用
this.tsmiStop.Enabled = true;//停止按钮可用
//设置运行状态
//this.isRun = true;//开始闪烁
this.timer_dingdanchaxun.Enabled = true;
} //点击停止菜单
private void tsmiStop_Click(object sender, EventArgs e)
{
this.tsmiRun.Enabled = true;
this.tsmiStop.Enabled = false;
//设置为停止状态
this.isRun = false;
this.timer_dingdanchaxun.Enabled = false;
//恢复图标显示
this.setIconImg();
} }
}
效果如图: