| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- 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; // 缩小为进度条
- }
- }
-
- }
|