The premise is that the tray display function of the program has been realized, and then a normal icon and a transparent icon are used to cycle through the timer to achieve a flashing effect.
1. Define fields
DispatcherTimer timer = new DispatcherTimer();//定时器
bool change = false;//切换图标
System.Drawing.Icon icon1 = new System.Drawing.Icon(System.Windows.Application.GetResourceStream(new Uri("/Photo/WPF.ico", UriKind.Relative)).Stream);//程序图标
System.Drawing.Icon icon2 = new System.Drawing.Icon(System.Windows.Application.GetResourceStream(new Uri("/Photo/WPF2.ico", UriKind.Relative)).Stream);//程序图标
System.Drawing.Icon icon3 = (Properties.Resources.WPF3);//程序图标,这里图片做为一种资源放在了资源文件里,也可以像以上的写法定义。
2. The initial timer in the event
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = TimeSpan.FromMilliseconds(500);
3. Events that are executed regularly
private void timer_Tick(object sender, EventArgs e)
{
//判断是否最小化,这里的notifyIcon是一个具体的new出来的实例
if (ws == WindowState.Minimized)
{
if (!change)
{
this.notifyIcon.Icon = icon1;
change = true;
}
else
{
this.notifyIcon.Icon = icon3;
change = false;
}
}
}