WPF 系统托盘 图标闪烁

本文介绍了一个使用C# Windows Forms开发的应用程序,它通过NotifyIcon实现系统监控的提示,并利用DispatcherTimer实现图标闪烁效果。开发者展示了如何设置通知的显示、菜单选项和与托盘区交互,以及如何定时切换图标资源。
摘要由CSDN通过智能技术生成
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();
             }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值