using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ECMonitor.Code.SysemNotifyIcon { public class WPFSystemTray { /// /// 设置系统托盘 /// /// 最小化参数 /// /// public static NotifyIcon SetSystemTray(SystemTrayParameter pars, List menuList) { NotifyIcon notifyIcon = new NotifyIcon(); notifyIcon.Visible = true; if (!string.IsNullOrWhiteSpace(pars.Icon)) { notifyIcon.Icon = new System.Drawing.Icon(pars.Icon);//程序图标 } if (!string.IsNullOrWhiteSpace(pars.MinText)) { notifyIcon.Text = pars.MinText;//最小化到托盘时,鼠标悬浮时显示的文字 } if (!string.IsNullOrWhiteSpace(pars.TipText)) { notifyIcon.BalloonTipText = pars.TipText; //设置系统托盘启动时显示的文本 notifyIcon.ShowBalloonTip(pars.Time == 0 ? 100 : pars.Time);//显示时长 } notifyIcon.MouseDoubleClick += pars.dbClick; //双击事件 notifyIcon.ContextMenuStrip = GetMenuStrip(menuList); return notifyIcon; } /// /// 设置系统托盘的菜单属性 /// /// /// static ContextMenuStrip GetMenuStrip(List menus) { ContextMenuStrip menu = new ContextMenuStrip(); ToolStripMenuItem[] menuArray = new ToolStripMenuItem[menus.Count]; int i = 0; foreach (SystemTrayMenu item in menus) { ToolStripMenuItem menuItem = new ToolStripMenuItem(); menuItem.Text = item.Txt; menuItem.Click += item.Click; if (!string.IsNullOrWhiteSpace(item.Icon) && System.IO.File.Exists(item.Icon)) { menuItem.Image = System.Drawing.Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + item.Icon); } menuArray[i++] = menuItem; } menu.Items.AddRange(menuArray); return menu; } } /// /// 系统托盘参数 /// public class SystemTrayParameter { public SystemTrayParameter(string Icon, string MinText, string TipText, int Time, MouseEventHandler dbClick) { this.Icon = Icon; this.MinText = MinText; this.TipText = TipText; this.Time = Time; this.dbClick = dbClick; } /// /// 托盘显示图标 /// public string Icon { get; set; } /// /// 最小化悬浮时文本 /// public string MinText { get; set; } /// /// 最小化启动时文本 /// public string TipText { get; set; } /// /// 最小化启动时文本显示时长 /// public int Time { get; set; } /// /// 最小化双击事件 /// public MouseEventHandler dbClick { get; set; } } /// /// 右键菜单 /// public class SystemTrayMenu { /// /// 菜单文本 /// public string Txt { get; set; } /// /// 菜单图标 /// public string Icon { get; set; } /// /// 菜单单击事件 /// public EventHandler Click { get; set; } } }