WPF控件--利用Winform库中的NotifyIcon实现托盘小程序

WPF控件--NotifyIcon

 

运行界面如下所示:

                 

       图1                                             图2

 

       代码很少,如下所示:       

  1 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->
  2 using System;
  3 using System.Windows;
  4 using System.Windows.Forms;
  5 using System.Drawing;
  6 
  7 namespace Royen
  8 {
  9     public partial class SysTray : Window
 10     {
 11         private NotifyIcon notifyIcon=null;
 12 
 13         public SysTray()
 14         {
 15             InitializeComponent();
 16 
 17             InitialTray();
 18         }
 19 
 20         private void InitialTray()
 21         {
 22             //隐藏主窗体
 23             this.Visibility = Visibility.Hidden;
 24                 
 25             //设置托盘的各个属性
 26             notifyIcon = new NotifyIcon();
 27             notifyIcon.BalloonTipText = "systray runnning...";         
 28             notifyIcon.Text = "systray";
 29             notifyIcon.Icon = new System.Drawing.Icon("http://www.cnblogs.com/res/spring.ico");
 30             notifyIcon.Visible = true;
 31             notifyIcon.ShowBalloonTip(2000);            
 32             notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick);
 33             
 34             //设置菜单项
 35             MenuItem setting1 = new MenuItem("setting1");
 36             MenuItem setting2 = new MenuItem("setting2");
 37             MenuItem setting = new MenuItem("setting", new MenuItem[]{setting1,setting2});
 38 
 39             //帮助选项
 40             MenuItem help = new MenuItem("help");
 41 
 42             //关于选项
 43             MenuItem about = new MenuItem("about");
 44 
 45             //退出菜单项
 46             MenuItem exit = new MenuItem("exit");
 47             exit.Click += new EventHandler(exit_Click);
 48 
 49             //关联托盘控件
 50             MenuItem[] childen = new MenuItem[] {setting,help,about,exit};
 51             notifyIcon.ContextMenu = new ContextMenu(childen);
 52 
 53             //窗体状态改变时候触发
 54             this.StateChanged += new EventHandler(SysTray_StateChanged);
 55         }
 56       
 57         /// <summary>
 58         /// 鼠标单击
 59         /// </summary>
 60         /// <param name="sender"></param>
 61         /// <param name="e"></param>
 62         private void notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
 63         {
 64             //如果鼠标左键单击
 65             if (e.Button == MouseButtons.Left)
 66             {
 67                 if (this.Visibility == Visibility.Visible)
 68                 {
 69                     this.Visibility = Visibility.Hidden;
 70                 }
 71                 else
 72                 {
 73                     this.Visibility = Visibility.Visible;
 74                     this.Activate();
 75                 }
 76             }
 77         }
 78 
 79         /// <summary>
 80         /// 窗体状态改变时候触发
 81         /// </summary>
 82         /// <param name="sender"></param>
 83         /// <param name="e"></param>
 84         private void SysTray_StateChanged(object sender, EventArgs e)
 85         {
 86             if (this.WindowState == WindowState.Minimized)
 87             {
 88                 this.Visibility = Visibility.Hidden;
 89             }
 90         }          
 91 
 92 
 93         /// <summary>
 94         /// 退出选项
 95         /// </summary>
 96         /// <param name="sender"></param>
 97         /// <param name="e"></param>
 98         private void exit_Click(object sender, EventArgs e)
 99         {
100             if (System.Windows.MessageBox.Show("sure to exit?",
101                                                "application",
102                                                 MessageBoxButton.YesNo,
103                                                 MessageBoxImage.Question,
104                                                 MessageBoxResult.No) == MessageBoxResult.Yes)
105             {
106                 System.Windows.Application.Current.Shutdown();
107             }
108         }
109     }
110 }
View Code

工程源码下载:/Files/royenhome/Royen.rar 。真诚的希望和大家学习交流~

支持正版,版权来自 http://www.cnblogs.com/royenhome/archive/2010/02/02/1662243.html  园主:royen

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值