2
0

MainWindow.xaml.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Windows;
  5. using System.Windows.Input;
  6. using System.Windows.Interop;
  7. namespace FastGithub.UI
  8. {
  9. /// <summary>
  10. /// MainWindow.xaml 的交互逻辑
  11. /// </summary>
  12. public partial class MainWindow : Window
  13. {
  14. private readonly System.Windows.Forms.NotifyIcon notifyIcon;
  15. private const string FAST_GITHUB = "FastGithub";
  16. private const string PROJECT_URI = "https://github.com/dotnetcore/FastGithub";
  17. public MainWindow()
  18. {
  19. InitializeComponent();
  20. var about = new System.Windows.Forms.MenuItem("关于(&A)");
  21. about.Click += (s, e) => Process.Start(PROJECT_URI);
  22. var exit = new System.Windows.Forms.MenuItem("退出(&C)");
  23. exit.Click += (s, e) => this.Close();
  24. this.notifyIcon = new System.Windows.Forms.NotifyIcon
  25. {
  26. Visible = true,
  27. Text = FAST_GITHUB,
  28. ContextMenu = new System.Windows.Forms.ContextMenu(new[] { about, exit }),
  29. Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath)
  30. };
  31. this.notifyIcon.MouseClick += (s, e) =>
  32. {
  33. if (e.Button == System.Windows.Forms.MouseButtons.Left)
  34. {
  35. this.Show();
  36. this.Activate();
  37. this.WindowState = WindowState.Normal;
  38. }
  39. };
  40. var fileName = $"{FAST_GITHUB}.exe";
  41. if (File.Exists(fileName) == true)
  42. {
  43. var version = FileVersionInfo.GetVersionInfo(fileName);
  44. this.Title = $"{FAST_GITHUB} v{version.ProductVersion}";
  45. }
  46. this.webBrowserIssue.AddHandler(KeyDownEvent, new RoutedEventHandler(WebBrowser_KeyDown), true);
  47. var resource = Application.GetResourceStream(new Uri("Resource/issue.html", UriKind.Relative));
  48. this.webBrowserIssue.NavigateToStream(resource.Stream);
  49. }
  50. /// <summary>
  51. /// 拦截F5
  52. /// </summary>
  53. /// <param name="sender"></param>
  54. /// <param name="e"></param>
  55. private void WebBrowser_KeyDown(object sender, RoutedEventArgs e)
  56. {
  57. var @event = (KeyEventArgs)e;
  58. if (@event.Key == Key.F5)
  59. {
  60. var resource = Application.GetResourceStream(new Uri("Resource/issue.html", UriKind.Relative));
  61. this.webBrowserIssue.NavigateToStream(resource.Stream);
  62. }
  63. }
  64. /// <summary>
  65. /// 拦截最小化事件
  66. /// </summary>
  67. /// <param name="e"></param>
  68. protected override void OnSourceInitialized(EventArgs e)
  69. {
  70. base.OnSourceInitialized(e);
  71. var hwndSource = (HwndSource)PresentationSource.FromVisual(this);
  72. hwndSource.AddHook(WndProc);
  73. IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
  74. {
  75. const int WM_SYSCOMMAND = 0x112;
  76. const int SC_MINIMIZE = 0xf020;
  77. const int SC_CLOSE = 0xf060;
  78. if (msg == WM_SYSCOMMAND)
  79. {
  80. if (wParam.ToInt32() == SC_MINIMIZE || wParam.ToInt32() == SC_CLOSE)
  81. {
  82. this.Hide();
  83. handled = true;
  84. }
  85. }
  86. return IntPtr.Zero;
  87. }
  88. }
  89. /// <summary>
  90. /// 关闭时
  91. /// </summary>
  92. /// <param name="e"></param>
  93. protected override void OnClosed(EventArgs e)
  94. {
  95. this.notifyIcon.Icon = null;
  96. this.notifyIcon.Dispose();
  97. base.OnClosed(e);
  98. }
  99. }
  100. }