nuget添加
Hardcodet.NotifyIcon.Wpf.NetCore
后台代码
1 public static class WindowsTaskbarIcon 2 { 3 static TaskbarIcon WindowsNotifyIcon { get; set; } 4 5 public static void Open() 6 { 7 if (WindowsNotifyIcon is null) 8 { 9 InitNotifyIcon(); 10 } 11 } 12 ///初始化托盘控件 13 static void InitNotifyIcon() 14 { 15 WindowsNotifyIcon = new TaskbarIcon(); 16 WindowsNotifyIcon.Icon = new Drawing.Icon("StaticResource/favicon.ico"); 17 ContextMenu context = new ContextMenu(); 18 MenuItem show = new MenuItem(); 19 show.Header = "显示"; 20 show.FontSize = 18; 21 show.Click += delegate (object sender, RoutedEventArgs e) 22 { 23 Application.Current.MainWindow.Show(); 24 }; 25 context.Items.Add(show); 26 27 MenuItem hide = new MenuItem(); 28 hide.Header = "隐藏"; 29 hide.FontSize = 18; 30 hide.Click += delegate (object sender, RoutedEventArgs e) 31 { 32 Application.Current.MainWindow.Hide(); 33 }; 34 context.Items.Add(hide); 35 36 MenuItem exit = new MenuItem(); 37 exit.Header = "退出"; 38 exit.FontSize = 18; 39 exit.Click += delegate (object sender, RoutedEventArgs e) 40 { 41 Environment.Exit(0); 42 }; 43 context.Items.Add(exit); 44 WindowsNotifyIcon.ContextMenu = context; 45 } 46 }
使用
效果