MainWindow.xaml.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 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 = PresentationSource.FromVisual(this) as HwndSource;
  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. if (msg == WM_SYSCOMMAND && wParam.ToInt32() == SC_MINIMIZE)
  78. {
  79. this.Hide();
  80. handled = true;
  81. }
  82. return IntPtr.Zero;
  83. }
  84. }
  85. /// <summary>
  86. /// 关闭时
  87. /// </summary>
  88. /// <param name="e"></param>
  89. protected override void OnClosed(EventArgs e)
  90. {
  91. this.notifyIcon.Icon = null;
  92. this.notifyIcon.Dispose();
  93. base.OnClosed(e);
  94. }
  95. }
  96. }