MainWindow.xaml.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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.webCert.AddHandler(KeyDownEvent, new RoutedEventHandler(WebBrowser_KeyDown), true);
  47. var resource = Application.GetResourceStream(new Uri("Resource/cert.html", UriKind.Relative));
  48. this.webCert.NavigateToStream(resource.Stream);
  49. }
  50. private void WebBrowser_KeyDown(object sender, RoutedEventArgs e)
  51. {
  52. var @event = (KeyEventArgs)e;
  53. if (@event.Key == Key.F5)
  54. {
  55. var resource = Application.GetResourceStream(new Uri("Resource/cert.html", UriKind.Relative));
  56. this.webCert.NavigateToStream(resource.Stream);
  57. }
  58. }
  59. protected override void OnSourceInitialized(EventArgs e)
  60. {
  61. base.OnSourceInitialized(e);
  62. var hwndSource = PresentationSource.FromVisual(this) as HwndSource;
  63. hwndSource.AddHook(WndProc);
  64. IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
  65. {
  66. const int WM_SYSCOMMAND = 0x112;
  67. const int SC_MINIMIZE = 0xf020;
  68. if (msg == WM_SYSCOMMAND)
  69. {
  70. if (wParam.ToInt32() == SC_MINIMIZE)
  71. {
  72. this.Hide();
  73. handled = true;
  74. }
  75. }
  76. return IntPtr.Zero;
  77. }
  78. }
  79. protected override void OnClosed(EventArgs e)
  80. {
  81. this.notifyIcon.Icon = null;
  82. this.notifyIcon.Dispose();
  83. base.OnClosed(e);
  84. }
  85. }
  86. }