ShowWindowNotifyIcon.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Media;
  5. using Hardcodet.Wpf.TaskbarNotification;
  6. namespace MECF.Framework.Common.NotifyTrayIcons
  7. {
  8. public class ShowWindowNotifyIcon: TaskbarIcon
  9. {
  10. public event Action<bool> ShowMainWindow;
  11. public event Action ExitWindow;
  12. public ShowWindowNotifyIcon(string windowName, ImageSource icon)
  13. {
  14. ContextMenu menu = new ContextMenu();
  15. MenuItem item = new MenuItem();
  16. item.Header = "Show " + windowName;
  17. item.Click += ClickShow;
  18. menu.Items.Add(item);
  19. item = new MenuItem();
  20. item.Header = "Hide " + windowName;
  21. item.Click += ClickHide;
  22. menu.Items.Add(item);
  23. item = new MenuItem();
  24. item.Header = "-";
  25. menu.Items.Add(item);
  26. item = new MenuItem();
  27. item.Header = "Exit ";
  28. item.Click += ClickExit;
  29. menu.Items.Add(item);
  30. this.ContextMenu = menu;
  31. this.TrayMouseDoubleClick += ShowWindowNotifyIcon_TrayMouseDoubleClick;
  32. ToolTipText = "Double-click for window, right-click for menu";
  33. IconSource = icon;
  34. }
  35. private void ShowWindowNotifyIcon_TrayMouseDoubleClick(object sender, RoutedEventArgs e)
  36. {
  37. if (ShowMainWindow != null)
  38. ShowMainWindow(true);
  39. }
  40. public void ShowBallon(string title, string text)
  41. {
  42. ShowBalloonTip(title, text, BalloonIcon.Info);
  43. }
  44. private void ClickHide(object sender, RoutedEventArgs e)
  45. {
  46. if (ShowMainWindow != null)
  47. ShowMainWindow(false);
  48. }
  49. private void ClickShow(object sender, System.Windows.RoutedEventArgs e)
  50. {
  51. if (ShowMainWindow != null)
  52. {
  53. ShowMainWindow(true);
  54. }
  55. }
  56. private void ClickExit(object sender, System.Windows.RoutedEventArgs e)
  57. {
  58. if (ExitWindow != null)
  59. {
  60. Application.Current.Dispatcher.Invoke(() => { ExitWindow(); });
  61. }
  62. }
  63. }
  64. }