MainWindow.xaml.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Net.Http;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Forms;
  8. using System.Windows.Interop;
  9. namespace FastGithub.UI
  10. {
  11. /// <summary>
  12. /// MainWindow.xaml 的交互逻辑
  13. /// </summary>
  14. public partial class MainWindow : Window
  15. {
  16. private NotifyIcon notifyIcon;
  17. private const string FAST_GITHUB = "FastGithub";
  18. private const string PROJECT_URI = "https://github.com/dotnetcore/FastGithub";
  19. public MainWindow()
  20. {
  21. InitializeComponent();
  22. var about = new MenuItem("关于(&A)");
  23. about.Click += (s, e) => Process.Start(new ProcessStartInfo { FileName = PROJECT_URI, UseShellExecute = true });
  24. var exit = new MenuItem("退出(&C)");
  25. exit.Click += (s, e) => this.Close();
  26. this.notifyIcon = new NotifyIcon
  27. {
  28. Visible = true,
  29. Text = FAST_GITHUB,
  30. ContextMenu = new ContextMenu(new[] { about, exit }),
  31. Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath)
  32. };
  33. this.notifyIcon.MouseClick += (s, e) =>
  34. {
  35. if (e.Button == MouseButtons.Left)
  36. {
  37. this.Show();
  38. this.Activate();
  39. this.WindowState = WindowState.Normal;
  40. }
  41. };
  42. var fileName = $"{FAST_GITHUB}.exe";
  43. if (File.Exists(fileName) == true)
  44. {
  45. var version = FileVersionInfo.GetVersionInfo(fileName);
  46. this.Title = $"{FAST_GITHUB} v{version.ProductVersion}";
  47. }
  48. this.InitWebBrowsers();
  49. }
  50. private async void InitWebBrowsers()
  51. {
  52. var httpClient = new HttpClient();
  53. for (var i = 0; i < 5; i++)
  54. {
  55. try
  56. {
  57. var response = await httpClient.GetAsync("http://127.0.0.1/flowRates");
  58. response.EnsureSuccessStatusCode();
  59. this.webBrowserFlow.Source = new Uri("http://127.0.0.1/flow");
  60. this.webBrowserCert.Source = new Uri("http://127.0.0.1/cert");
  61. this.webBrowserReward.Source = new Uri("http://127.0.0.1/reward");
  62. break;
  63. }
  64. catch (Exception)
  65. {
  66. await Task.Delay(TimeSpan.FromSeconds(1d));
  67. }
  68. }
  69. httpClient.Dispose();
  70. }
  71. protected override void OnSourceInitialized(EventArgs e)
  72. {
  73. base.OnSourceInitialized(e);
  74. var hwndSource = PresentationSource.FromVisual(this) as HwndSource;
  75. hwndSource.AddHook(WndProc);
  76. IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
  77. {
  78. const int WM_SYSCOMMAND = 0x112;
  79. const int SC_MINIMIZE = 0xf020;
  80. if (msg == WM_SYSCOMMAND)
  81. {
  82. if (wParam.ToInt32() == SC_MINIMIZE)
  83. {
  84. this.Hide();
  85. handled = true;
  86. }
  87. }
  88. return IntPtr.Zero;
  89. }
  90. }
  91. protected override void OnClosed(EventArgs e)
  92. {
  93. this.notifyIcon.Icon = null;
  94. this.notifyIcon.Dispose();
  95. base.OnClosed(e);
  96. }
  97. }
  98. }