using System.Windows.Forms;
using System.Windows.Threading;
public partial class Window : Window
{
private NotifyIcon notifyIcon;
DispatcherTimer icoTimer = new DispatcherTimer();
string icoUrl = @"../../Red.ico";
string icoUrl2 = @"../../Red2.ico";
public long i = ;
public Window()
{
InitializeComponent();
//不在任务栏显示
this.ShowInTaskbar = false;
this.notifyIcon = new NotifyIcon();
this.notifyIcon.BalloonTipText = "系统监控中... ...";
this.notifyIcon.ShowBalloonTip();
this.notifyIcon.Text = "系统监控中... ...";
//this.notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);
this.notifyIcon.Icon = new System.Drawing.Icon(@"../../Red.ico");
this.notifyIcon.Visible = true;
//打开菜单项
System.Windows.Forms.MenuItem open = new System.Windows.Forms.MenuItem("Open");
open.Click += new EventHandler(Show);
//退出菜单项
System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("Exit");
exit.Click += new EventHandler(Close);
//关联托盘控件
System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { open, exit };
notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);
this.notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler((o, e) =>
{
if (e.Button == MouseButtons.Left) this.Show(o, e);
//this.Visibility = System.Windows.Visibility.Visible;
//this.ShowInTaskbar = true;
//this.Activate();
NavigationWindow win = new MainWindow();
this.notifyIcon.Visible = false;
win.ShowDialog();
});
//闪烁图标
icoTimer.Interval = TimeSpan.FromSeconds(0.3);
icoTimer.Tick += new EventHandler(IcoTimer_Tick);
icoTimer.Start();
}
public void IcoTimer_Tick(object sender, EventArgs e)
{
i=i+;
if (i % != )
{
this.notifyIcon.Icon = new System.Drawing.Icon(icoUrl);
}
else
{
this.notifyIcon.Icon = new System.Drawing.Icon(icoUrl2);
}
}
private void Show(object sender, EventArgs e)
{
this.Visibility = System.Windows.Visibility.Visible;
this.ShowInTaskbar = true;
this.Activate();
}
private void Hide(object sender, EventArgs e)
{
this.ShowInTaskbar = false;
this.Visibility = System.Windows.Visibility.Hidden;
}
private void Close(object sender, EventArgs e)
{
System.Windows.Application.Current.Shutdown();
}
WPF 系统托盘 图标闪烁
本文介绍了一个使用C# Windows Forms开发的应用程序,它通过NotifyIcon实现系统监控的提示,并利用DispatcherTimer实现图标闪烁效果。开发者展示了如何设置通知的显示、菜单选项和与托盘区交互,以及如何定时切换图标资源。
摘要由CSDN通过智能技术生成