MainWindow.xaml.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using System.Net.NetworkInformation;
  3. using System.Threading.Tasks;
  4. using System.Windows;
  5. using System.Windows.Input;
  6. using System.Windows.Media;
  7. namespace PingHandler
  8. {
  9. public partial class MainWindow : Window
  10. {
  11. public MainWindow()
  12. {
  13. InitializeComponent();
  14. StartMonitoring();
  15. }
  16. private async void StartMonitoring()
  17. {
  18. while (true)
  19. {
  20. var latency = await GetNetworkLatency();
  21. UpdateUI(latency);
  22. await Task.Delay(1000); // 每秒钟更新一次
  23. }
  24. }
  25. private async Task<int> GetNetworkLatency()
  26. {
  27. try
  28. {
  29. var ping = new Ping();
  30. var reply = await ping.SendPingAsync("www.baidu.com", 1000);
  31. if (reply.Status == IPStatus.Success)
  32. {
  33. return (int)reply.RoundtripTime;
  34. }
  35. else
  36. {
  37. return -1;
  38. }
  39. }
  40. catch
  41. {
  42. return -1; // 网络断线
  43. }
  44. }
  45. private void UpdateUI(int latency)
  46. {
  47. if (latency == -1)
  48. {
  49. LatencyText.Text = "Disconnected";
  50. LatencyText.Foreground = Brushes.Red;
  51. LatencyProgressBar.Value = 0;
  52. LatencyProgressBar.Foreground = Brushes.Red;
  53. return;
  54. }
  55. LatencyText.Text = $"{latency} ms";
  56. if (latency < 50)
  57. {
  58. LatencyText.Foreground = Brushes.Green;
  59. LatencyProgressBar.Foreground = Brushes.Green;
  60. LatencyProgressBar.Value = latency;
  61. }
  62. else if (latency < 100)
  63. {
  64. LatencyText.Foreground = Brushes.Yellow;
  65. LatencyProgressBar.Foreground = Brushes.Yellow;
  66. LatencyProgressBar.Value = latency;
  67. }
  68. else if (latency < 200)
  69. {
  70. LatencyText.Foreground = Brushes.DarkOrange;
  71. LatencyProgressBar.Foreground = Brushes.DarkOrange;
  72. LatencyProgressBar.Value = latency;
  73. }
  74. else
  75. {
  76. LatencyText.Foreground = Brushes.Red;
  77. LatencyProgressBar.Foreground = Brushes.Red;
  78. LatencyProgressBar.Value = 200;
  79. }
  80. }
  81. private void Window_LocationChanged(object sender, EventArgs e)
  82. {
  83. CheckIfNearScreenEdge();
  84. }
  85. protected override void OnLocationChanged(EventArgs e)
  86. {
  87. base.OnLocationChanged(e);
  88. EnsureWindowIsOnScreen();
  89. }
  90. protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
  91. {
  92. base.OnRenderSizeChanged(sizeInfo);
  93. EnsureWindowIsOnScreen();
  94. }
  95. private void EnsureWindowIsOnScreen()
  96. {
  97. var desktopWorkingArea = SystemParameters.WorkArea;
  98. if (Left < desktopWorkingArea.Left) Left = desktopWorkingArea.Left;
  99. if (Top < desktopWorkingArea.Top) Top = desktopWorkingArea.Top;
  100. if (Left + Width > desktopWorkingArea.Right) Left = desktopWorkingArea.Right - Width;
  101. if (Top + Height > desktopWorkingArea.Bottom) Top = desktopWorkingArea.Bottom - Height;
  102. }
  103. private void Window_MouseDown(object sender, MouseButtonEventArgs e)
  104. {
  105. if (e.LeftButton == MouseButtonState.Pressed)
  106. {
  107. DragMove();
  108. }
  109. }
  110. private void CheckIfNearScreenEdge()
  111. {
  112. var desktopWorkingArea = SystemParameters.WorkArea;
  113. const int edgeThreshold = 10; // 距离边缘的阈值
  114. if (Left <= desktopWorkingArea.Left + edgeThreshold)
  115. {
  116. Width = 20;
  117. Height = 200;
  118. LatencyText.Visibility = Visibility.Collapsed;
  119. MainBorder.Background = Brushes.Transparent;
  120. LatencyProgressBar.Style = (Style)Resources["VerticalProgressBar"];
  121. LatencyProgressBar.HorizontalAlignment = HorizontalAlignment.Stretch;
  122. LatencyProgressBar.VerticalAlignment = VerticalAlignment.Stretch;
  123. }
  124. else if (Left + Width >= desktopWorkingArea.Right - edgeThreshold)
  125. {
  126. Width = 20;
  127. Height = 200;
  128. LatencyText.Visibility = Visibility.Collapsed;
  129. MainBorder.Background = Brushes.Transparent;
  130. LatencyProgressBar.Style = (Style)Resources["VerticalProgressBar"];
  131. LatencyProgressBar.HorizontalAlignment = HorizontalAlignment.Stretch;
  132. LatencyProgressBar.VerticalAlignment = VerticalAlignment.Stretch;
  133. }
  134. else if (Top <= desktopWorkingArea.Top + edgeThreshold)
  135. {
  136. Width = 200;
  137. Height = 20;
  138. LatencyText.Visibility = Visibility.Collapsed;
  139. MainBorder.Background = Brushes.Transparent;
  140. LatencyProgressBar.Style = (Style)Resources["ColoredProgressBar"];
  141. LatencyProgressBar.HorizontalAlignment = HorizontalAlignment.Stretch;
  142. LatencyProgressBar.VerticalAlignment = VerticalAlignment.Stretch;
  143. }
  144. else
  145. {
  146. Width = 85;
  147. Height = 45;
  148. LatencyText.Visibility = Visibility.Visible;
  149. MainBorder.Background = new SolidColorBrush(Color.FromArgb(128, 0, 0, 0));
  150. LatencyProgressBar.Style = (Style)Resources["ColoredProgressBar"];
  151. LatencyProgressBar.HorizontalAlignment = HorizontalAlignment.Stretch;
  152. LatencyProgressBar.VerticalAlignment = VerticalAlignment.Bottom;
  153. }
  154. }
  155. private void Window_MouseEnter(object sender, MouseEventArgs e)
  156. {
  157. // Width = 200; // 扩展显示详细信息
  158. }
  159. private void Window_MouseLeave(object sender, MouseEventArgs e)
  160. {
  161. // Width = 85; // 缩小为进度条
  162. }
  163. }
  164. }