WPF控件--NotifyIcon
运行界面如下所示:
图1 图2
代码很少,如下所示:
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> using System; using System.Windows; using System.Windows.Forms; using System.Drawing; namespace Royen { public partial class SysTray : Window { private NotifyIcon notifyIcon=null; public SysTray() { InitializeComponent(); InitialTray(); } private void InitialTray() { //隐藏主窗体 this.Visibility = Visibility.Hidden; //设置托盘的各个属性 notifyIcon = new NotifyIcon(); notifyIcon.BalloonTipText = "systray runnning..."; notifyIcon.Text = "systray"; notifyIcon.Icon = new System.Drawing.Icon("http://www.cnblogs.com/res/spring.ico"); notifyIcon.Visible = true; notifyIcon.ShowBalloonTip(); notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick); //设置菜单项 MenuItem setting1 = new MenuItem("setting1"); MenuItem setting2 = new MenuItem("setting2"); MenuItem setting = new MenuItem("setting", new MenuItem[]{setting1,setting2}); //帮助选项 MenuItem help = new MenuItem("help"); //关于选项 MenuItem about = new MenuItem("about"); //退出菜单项 MenuItem exit = new MenuItem("exit"); exit.Click += new EventHandler(exit_Click); //关联托盘控件 MenuItem[] childen = new MenuItem[] {setting,help,about,exit}; notifyIcon.ContextMenu = new ContextMenu(childen); //窗体状态改变时候触发 this.StateChanged += new EventHandler(SysTray_StateChanged); } /// <summary> /// 鼠标单击 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e) { //如果鼠标左键单击 if (e.Button == MouseButtons.Left) { if (this.Visibility == Visibility.Visible) { this.Visibility = Visibility.Hidden; } else { this.Visibility = Visibility.Visible; this.Activate(); } } } /// <summary> /// 窗体状态改变时候触发 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SysTray_StateChanged(object sender, EventArgs e) { if (this.WindowState == WindowState.Minimized) { this.Visibility = Visibility.Hidden; } } /// <summary> /// 退出选项 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void exit_Click(object sender, EventArgs e) { if (System.Windows.MessageBox.Show("sure to exit?", "application", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes) { System.Windows.Application.Current.Shutdown(); } } } }
工程源码下载:/Files/royenhome/Royen.rar 。真诚的希望和大家学习交流~
支持正版,版权来自 http://www.cnblogs.com/royenhome/archive/2010/02/02/1662243.html 园主:royen