WPF 系统托盘 图标闪烁
WPF消息通知
系统托盘,图标闪烁
1 using System.Windows.Forms; 2 3 using System.Windows.Threading; 4 5 public partial class Window : Window 6 { 7 private NotifyIcon notifyIcon; 8 DispatcherTimer icoTimer = new DispatcherTimer(); 9 string icoUrl = @"../../Red.ico"; 10 string icoUrl2 = @"../../Red2.ico"; 11 public long i = 0; 12 public Window() 13 { 14 InitializeComponent(); 15 //不在任务栏显示 16 this.ShowInTaskbar = false; 17 this.notifyIcon = new NotifyIcon(); 18 this.notifyIcon.BalloonTipText = "系统监控中... ..."; 19 this.notifyIcon.ShowBalloonTip(2000); 20 this.notifyIcon.Text = "系统监控中... ..."; 21 //this.notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath); 22 this.notifyIcon.Icon = new System.Drawing.Icon(@"../../Red.ico"); 23 this.notifyIcon.Visible = true; 24 //打开菜单项 25 System.Windows.Forms.MenuItem open = new System.Windows.Forms.MenuItem("Open"); 26 open.Click += new EventHandler(Show); 27 //退出菜单项 28 System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("Exit"); 29 exit.Click += new EventHandler(Close); 30 //关联托盘控件 31 System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { open, exit }; 32 notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen); 33 34 this.notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler((o, e) => 35 { 36 if (e.Button == MouseButtons.Left) this.Show(o, e); 37 //this.Visibility = System.Windows.Visibility.Visible; 38 //this.ShowInTaskbar = true; 39 //this.Activate(); 40 NavigationWindow win = new MainWindow(); 41 this.notifyIcon.Visible = false; 42 win.ShowDialog(); 43 }); 44 45 46 //闪烁图标 47 icoTimer.Interval = TimeSpan.FromSeconds(0.3); 48 icoTimer.Tick += new EventHandler(IcoTimer_Tick); 49 icoTimer.Start(); 50 } 51 public void IcoTimer_Tick(object sender, EventArgs e) 52 { 53 i=i+1; 54 if (i % 2 != 0) 55 { 56 this.notifyIcon.Icon = new System.Drawing.Icon(icoUrl); 57 } 58 else 59 { 60 this.notifyIcon.Icon = new System.Drawing.Icon(icoUrl2); 61 } 62 } 63 private void Show(object sender, EventArgs e) 64 { 65 this.Visibility = System.Windows.Visibility.Visible; 66 this.ShowInTaskbar = true; 67 this.Activate(); 68 } 69 70 private void Hide(object sender, EventArgs e) 71 { 72 this.ShowInTaskbar = false; 73 this.Visibility = System.Windows.Visibility.Hidden; 74 } 75 76 private void Close(object sender, EventArgs e) 77 { 78 System.Windows.Application.Current.Shutdown(); 79 }
转自:http://www.cnblogs.com/ke10/archive/2012/11/16/NotifyIcon.html
记得对System.Windows.Forms添加引用。