|
@@ -0,0 +1,180 @@
|
|
|
|
|
+using System;
|
|
|
|
|
+using System.Net.NetworkInformation;
|
|
|
|
|
+using System.Threading.Tasks;
|
|
|
|
|
+using System.Windows;
|
|
|
|
|
+using System.Windows.Input;
|
|
|
|
|
+using System.Windows.Media;
|
|
|
|
|
+
|
|
|
|
|
+namespace PingHandler
|
|
|
|
|
+{
|
|
|
|
|
+ public partial class MainWindow : Window
|
|
|
|
|
+ {
|
|
|
|
|
+ public MainWindow()
|
|
|
|
|
+ {
|
|
|
|
|
+ InitializeComponent();
|
|
|
|
|
+ StartMonitoring();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private async void StartMonitoring()
|
|
|
|
|
+ {
|
|
|
|
|
+ while (true)
|
|
|
|
|
+ {
|
|
|
|
|
+ var latency = await GetNetworkLatency();
|
|
|
|
|
+ UpdateUI(latency);
|
|
|
|
|
+ await Task.Delay(1000); // 每秒钟更新一次
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private async Task<int> GetNetworkLatency()
|
|
|
|
|
+ {
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
|
|
+ var ping = new Ping();
|
|
|
|
|
+ var reply = await ping.SendPingAsync("www.baidu.com", 1000);
|
|
|
|
|
+ if (reply.Status == IPStatus.Success)
|
|
|
|
|
+ {
|
|
|
|
|
+ return (int)reply.RoundtripTime;
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ catch
|
|
|
|
|
+ {
|
|
|
|
|
+ return -1; // 网络断线
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void UpdateUI(int latency)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (latency == -1)
|
|
|
|
|
+ {
|
|
|
|
|
+ LatencyText.Text = "Disconnected";
|
|
|
|
|
+ LatencyText.Foreground = Brushes.Red;
|
|
|
|
|
+ LatencyProgressBar.Value = 0;
|
|
|
|
|
+ LatencyProgressBar.Foreground = Brushes.Red;
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ LatencyText.Text = $"{latency} ms";
|
|
|
|
|
+
|
|
|
|
|
+ if (latency < 50)
|
|
|
|
|
+ {
|
|
|
|
|
+ LatencyText.Foreground = Brushes.Green;
|
|
|
|
|
+ LatencyProgressBar.Foreground = Brushes.Green;
|
|
|
|
|
+ LatencyProgressBar.Value = latency;
|
|
|
|
|
+ }
|
|
|
|
|
+ else if (latency < 100)
|
|
|
|
|
+ {
|
|
|
|
|
+ LatencyText.Foreground = Brushes.Yellow;
|
|
|
|
|
+ LatencyProgressBar.Foreground = Brushes.Yellow;
|
|
|
|
|
+ LatencyProgressBar.Value = latency;
|
|
|
|
|
+ }
|
|
|
|
|
+ else if (latency < 200)
|
|
|
|
|
+ {
|
|
|
|
|
+ LatencyText.Foreground = Brushes.DarkOrange;
|
|
|
|
|
+ LatencyProgressBar.Foreground = Brushes.DarkOrange;
|
|
|
|
|
+ LatencyProgressBar.Value = latency;
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ LatencyText.Foreground = Brushes.Red;
|
|
|
|
|
+ LatencyProgressBar.Foreground = Brushes.Red;
|
|
|
|
|
+ LatencyProgressBar.Value = 200;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void Window_LocationChanged(object sender, EventArgs e)
|
|
|
|
|
+ {
|
|
|
|
|
+ CheckIfNearScreenEdge();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected override void OnLocationChanged(EventArgs e)
|
|
|
|
|
+ {
|
|
|
|
|
+ base.OnLocationChanged(e);
|
|
|
|
|
+ EnsureWindowIsOnScreen();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
|
|
|
|
|
+ {
|
|
|
|
|
+ base.OnRenderSizeChanged(sizeInfo);
|
|
|
|
|
+ EnsureWindowIsOnScreen();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void EnsureWindowIsOnScreen()
|
|
|
|
|
+ {
|
|
|
|
|
+ var desktopWorkingArea = SystemParameters.WorkArea;
|
|
|
|
|
+ if (Left < desktopWorkingArea.Left) Left = desktopWorkingArea.Left;
|
|
|
|
|
+ if (Top < desktopWorkingArea.Top) Top = desktopWorkingArea.Top;
|
|
|
|
|
+ if (Left + Width > desktopWorkingArea.Right) Left = desktopWorkingArea.Right - Width;
|
|
|
|
|
+ if (Top + Height > desktopWorkingArea.Bottom) Top = desktopWorkingArea.Bottom - Height;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void Window_MouseDown(object sender, MouseButtonEventArgs e)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (e.LeftButton == MouseButtonState.Pressed)
|
|
|
|
|
+ {
|
|
|
|
|
+ DragMove();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ private void CheckIfNearScreenEdge()
|
|
|
|
|
+ {
|
|
|
|
|
+ var desktopWorkingArea = SystemParameters.WorkArea;
|
|
|
|
|
+ const int edgeThreshold = 10; // 距离边缘的阈值
|
|
|
|
|
+
|
|
|
|
|
+ if (Left <= desktopWorkingArea.Left + edgeThreshold)
|
|
|
|
|
+ {
|
|
|
|
|
+ Width = 20;
|
|
|
|
|
+ Height = 200;
|
|
|
|
|
+ LatencyText.Visibility = Visibility.Collapsed;
|
|
|
|
|
+ MainBorder.Background = Brushes.Transparent;
|
|
|
|
|
+ LatencyProgressBar.Style = (Style)Resources["VerticalProgressBar"];
|
|
|
|
|
+ LatencyProgressBar.HorizontalAlignment = HorizontalAlignment.Stretch;
|
|
|
|
|
+ LatencyProgressBar.VerticalAlignment = VerticalAlignment.Stretch;
|
|
|
|
|
+ }
|
|
|
|
|
+ else if (Left + Width >= desktopWorkingArea.Right - edgeThreshold)
|
|
|
|
|
+ {
|
|
|
|
|
+ Width = 20;
|
|
|
|
|
+ Height = 200;
|
|
|
|
|
+ LatencyText.Visibility = Visibility.Collapsed;
|
|
|
|
|
+ MainBorder.Background = Brushes.Transparent;
|
|
|
|
|
+ LatencyProgressBar.Style = (Style)Resources["VerticalProgressBar"];
|
|
|
|
|
+ LatencyProgressBar.HorizontalAlignment = HorizontalAlignment.Stretch;
|
|
|
|
|
+ LatencyProgressBar.VerticalAlignment = VerticalAlignment.Stretch;
|
|
|
|
|
+ }
|
|
|
|
|
+ else if (Top <= desktopWorkingArea.Top + edgeThreshold)
|
|
|
|
|
+ {
|
|
|
|
|
+ Width = 200;
|
|
|
|
|
+ Height = 20;
|
|
|
|
|
+ LatencyText.Visibility = Visibility.Collapsed;
|
|
|
|
|
+ MainBorder.Background = Brushes.Transparent;
|
|
|
|
|
+ LatencyProgressBar.Style = (Style)Resources["ColoredProgressBar"];
|
|
|
|
|
+ LatencyProgressBar.HorizontalAlignment = HorizontalAlignment.Stretch;
|
|
|
|
|
+ LatencyProgressBar.VerticalAlignment = VerticalAlignment.Stretch;
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ Width = 85;
|
|
|
|
|
+ Height = 45;
|
|
|
|
|
+ LatencyText.Visibility = Visibility.Visible;
|
|
|
|
|
+ MainBorder.Background = new SolidColorBrush(Color.FromArgb(128, 0, 0, 0));
|
|
|
|
|
+ LatencyProgressBar.Style = (Style)Resources["ColoredProgressBar"];
|
|
|
|
|
+ LatencyProgressBar.HorizontalAlignment = HorizontalAlignment.Stretch;
|
|
|
|
|
+ LatencyProgressBar.VerticalAlignment = VerticalAlignment.Bottom;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void Window_MouseEnter(object sender, MouseEventArgs e)
|
|
|
|
|
+ {
|
|
|
|
|
+ // Width = 200; // 扩展显示详细信息
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void Window_MouseLeave(object sender, MouseEventArgs e)
|
|
|
|
|
+ {
|
|
|
|
|
+ // Width = 85; // 缩小为进度条
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|