WPFSystemTray.cs
1 public class WPFSystemTray 2 { 3 ///4 /// 设置系统托盘 5 /// 6 /// 最小化参数 7 /// 8 ///9 public static NotifyIcon SetSystemTray(SystemTrayParameter pars, List menuList) 10 { 11 NotifyIcon notifyIcon = new NotifyIcon(); 12 notifyIcon.Visible = true; 13 if (!string.IsNullOrWhiteSpace(pars.Icon)) 14 { 15 notifyIcon.Icon = new System.Drawing.Icon(pars.Icon);//程序图标 16 } 17 if (!string.IsNullOrWhiteSpace(pars.MinText)) 18 { 19 notifyIcon.Text = pars.MinText;//最小化到托盘时,鼠标悬浮时显示的文字 20 } 21 if (!string.IsNullOrWhiteSpace(pars.TipText)) 22 { 23 notifyIcon.BalloonTipText = pars.TipText; //设置系统托盘启动时显示的文本 24 notifyIcon.ShowBalloonTip(pars.Time == 0 ? 100 : pars.Time);//显示时长 25 } 26 notifyIcon.MouseDoubleClick += pars.dbClick; //双击事件 27 notifyIcon.ContextMenuStrip = GetMenuStrip(menuList); 28 return notifyIcon; 29 } 30 /// 31 /// 设置系统托盘的菜单属性 32 /// 33 /// 34 ///35 static ContextMenuStrip GetMenuStrip(List menus) 36 { 37 ContextMenuStrip menu = new ContextMenuStrip(); 38 ToolStripMenuItem[] menuArray = new ToolStripMenuItem[menus.Count]; 39 int i = 0; 40 foreach (SystemTrayMenu item in menus) 41 { 42 ToolStripMenuItem menuItem = new ToolStripMenuItem(); 43 menuItem.Text = item.Txt; 44 menuItem.Click += item.Click; 45 if (!string.IsNullOrWhiteSpace(item.Icon) && System.IO.File.Exists(item.Icon)) { menuItem.Image = System.Drawing.Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + item.Icon); } 46 menuArray[i++] = menuItem; 47 } 48 menu.Items.AddRange(menuArray); 49 return menu; 50 } 51 } 52 53 /// 54 /// 系统托盘参数 55 /// 56 public class SystemTrayParameter 57 { 58 public SystemTrayParameter(string Icon, string MinText, string TipText, int Time, MouseEventHandler dbClick) 59 { 60 this.Icon = Icon; 61 this.MinText = MinText; 62 this.TipText = TipText; 63 this.Time = Time; 64 this.dbClick = dbClick; 65 } 66 ///67 /// 托盘显示图标 68 /// 69 public string Icon { get; set; } 70 ///71 /// 最小化悬浮时文本 72 /// 73 public string MinText { get; set; } 74 ///75 /// 最小化启动时文本 76 /// 77 public string TipText { get; set; } 78 ///79 /// 最小化启动时文本显示时长 80 /// 81 public int Time { get; set; } 82 ///83 /// 最小化双击事件 84 /// 85 public MouseEventHandler dbClick { get; set; } 86 } 87 ///88 /// 右键菜单 89 /// 90 public class SystemTrayMenu 91 { 92 ///93 /// 菜单文本 94 /// 95 public string Txt { get; set; } 96 ///97 /// 菜单图标 98 /// 99 public string Icon { get; set; }100 ///101 /// 菜单单击事件102 /// 103 public EventHandler Click { get; set; }104 }
MainWindow.cs
1 public MainWindow() 2 { 3 InitializeComponent(); 4 //系统托盘 5 SystemTrayParameter pars = new SystemTrayParameter("Logo45.ico", "守护中", "", 0, notifyIcon_MouseDoubleClick); 6 this.notifyIcon = WPFSystemTray.SetSystemTray(pars, GetList()); 7 WinCommon.WinBaseSet(this); 8 } 9 10 #region 系统托盘11 form.NotifyIcon notifyIcon;12 //最小化到托盘13 private void Button_Click(object sender, RoutedEventArgs e)14 {15 this.Hide();16 this.notifyIcon.Visible = true;17 }18 //托盘右键菜单集合19 private ListGetList()20 {21 List ls = new List ();22 ls.Add(new SystemTrayMenu() { Txt = "打开主面板", Icon = "", Click = mainWin_Click });23 ls.Add(new SystemTrayMenu() { Txt = "退出", Icon = "/img/exit.png", Click = exit_Click });24 return ls;25 }26 //双击事件27 void notifyIcon_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)28 {29 this.Show();30 this.notifyIcon.Visible = false;31 }32 #region 托盘右键菜单33 //打开主面板34 void mainWin_Click(object sender, EventArgs e)35 {36 this.Show();37 //this.notifyIcon.Visible = false;38 }39 //退出40 void exit_Click(object sender, EventArgs e)41 {42 this.Close();43 System.Windows.Application.Current.Shutdown();44 }45 #endregion46 #endregion