WPF控件--NotifyIcon
运行界面如下所示:
图1 图2
代码很少,如下所示:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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 }
工程源码下载:/Files/royenhome/Royen.rar 。真诚的希望和大家学习交流~
支持正版,版权来自 http://www.cnblogs.com/royenhome/archive/2010/02/02/1662243.html 园主:royen