WPF-系统托盘
WPFSystemTray.cs
1 public class WPFSystemTray 2 { 3 /// <summary> 4 /// 设置系统托盘 5 /// </summary> 6 /// <param name="pars">最小化参数</param> 7 /// <param name="dbClick"></param> 8 /// <returns></returns> 9 public static NotifyIcon SetSystemTray(SystemTrayParameter pars, List<SystemTrayMenu> 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 /// <summary> 31 /// 设置系统托盘的菜单属性 32 /// </summary> 33 /// <param name="menus"></param> 34 /// <returns></returns> 35 static ContextMenuStrip GetMenuStrip(List<SystemTrayMenu> 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 /// <summary> 54 /// 系统托盘参数 55 /// </summary> 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 /// <summary> 67 /// 托盘显示图标 68 /// </summary> 69 public string Icon { get; set; } 70 /// <summary> 71 /// 最小化悬浮时文本 72 /// </summary> 73 public string MinText { get; set; } 74 /// <summary> 75 /// 最小化启动时文本 76 /// </summary> 77 public string TipText { get; set; } 78 /// <summary> 79 /// 最小化启动时文本显示时长 80 /// </summary> 81 public int Time { get; set; } 82 /// <summary> 83 /// 最小化双击事件 84 /// </summary> 85 public MouseEventHandler dbClick { get; set; } 86 } 87 /// <summary> 88 /// 右键菜单 89 /// </summary> 90 public class SystemTrayMenu 91 { 92 /// <summary> 93 /// 菜单文本 94 /// </summary> 95 public string Txt { get; set; } 96 /// <summary> 97 /// 菜单图标 98 /// </summary> 99 public string Icon { get; set; } 100 /// <summary> 101 /// 菜单单击事件 102 /// </summary> 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 List<SystemTrayMenu> GetList() 20 { 21 List<SystemTrayMenu> ls = new List<SystemTrayMenu>(); 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 #endregion 46 #endregion
如果现在不努力,以后会活的更累吧。