gujiheimao 1 anno fa
commit
28518de91e
8 ha cambiato i file con 323 aggiunte e 0 eliminazioni
  1. 5 0
      .gitignore
  2. 9 0
      App.xaml
  3. 17 0
      App.xaml.cs
  4. 10 0
      AssemblyInfo.cs
  5. 76 0
      MainWindow.xaml
  6. 180 0
      MainWindow.xaml.cs
  7. 10 0
      PingHandler.csproj
  8. 16 0
      PingHandler.sln

+ 5 - 0
.gitignore

@@ -0,0 +1,5 @@
+bin/
+obj/
+/packages/
+riderModule.iml
+/_ReSharper.Caches/

+ 9 - 0
App.xaml

@@ -0,0 +1,9 @@
+<Application x:Class="PingHandler.App"
+             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+             xmlns:local="clr-namespace:PingHandler"
+             StartupUri="MainWindow.xaml">
+    <Application.Resources>
+         
+    </Application.Resources>
+</Application>

+ 17 - 0
App.xaml.cs

@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+using System.Configuration;
+using System.Data;
+using System.Linq;
+using System.Threading.Tasks;
+using System.Windows;
+
+namespace PingHandler
+{
+    /// <summary>
+    /// Interaction logic for App.xaml
+    /// </summary>
+    public partial class App : Application
+    {
+    }
+}

+ 10 - 0
AssemblyInfo.cs

@@ -0,0 +1,10 @@
+using System.Windows;
+
+[assembly: ThemeInfo(
+    ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
+    //(used if a resource is not found in the page,
+    // or application resource dictionaries)
+    ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
+    //(used if a resource is not found in the page,
+    // app, or any theme specific resource dictionaries)
+)]

+ 76 - 0
MainWindow.xaml

@@ -0,0 +1,76 @@
+<Window x:Class="PingHandler.MainWindow"
+        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+        Title="Network Latency Monitor" 
+        Width="85" 
+        Height="45" 
+        Topmost="True"
+        WindowStyle="None" 
+        AllowsTransparency="True" 
+        Background="Transparent"
+        ResizeMode="NoResize"
+        MouseDown="Window_MouseDown"
+        LocationChanged="Window_LocationChanged"
+        MouseEnter="Window_MouseEnter"
+        MouseLeave="Window_MouseLeave">
+    <Window.Resources>
+        <Style x:Key="ColoredProgressBar" TargetType="ProgressBar">
+            <Setter Property="Template">
+                <Setter.Value>
+                    <ControlTemplate TargetType="ProgressBar">
+                        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1" CornerRadius="5">
+                            <Grid x:Name="PART_Track" Background="Transparent">
+                                <Rectangle x:Name="PART_Indicator" HorizontalAlignment="Left" Fill="{TemplateBinding Foreground}" />
+                                </Grid>
+                        </Border>
+                    </ControlTemplate>
+                </Setter.Value>
+            </Setter>
+            <Setter Property="BorderBrush" Value="Black"/>
+            <Setter Property="Foreground" Value="Green"/>
+            <Setter Property="Height" Value="20"/>
+        </Style>
+        <Style x:Key="VerticalProgressBar" TargetType="ProgressBar">
+            <Setter Property="Template">
+                <Setter.Value>
+                    <ControlTemplate TargetType="ProgressBar">
+                        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1" CornerRadius="5">
+                            <Grid x:Name="PART_Track" Background="Transparent" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
+                                <Rectangle x:Name="PART_Indicator" VerticalAlignment="Bottom" Fill="{TemplateBinding Foreground}" Width="20" HorizontalAlignment="Stretch" />
+                            </Grid>
+                        </Border>
+                    </ControlTemplate>
+                </Setter.Value>
+            </Setter>
+            <Setter Property="BorderBrush" Value="Black"/>
+            <Setter Property="Foreground" Value="Green"/>
+            <Setter Property="Width" Value="20"/>
+        </Style>
+    </Window.Resources>
+    <Grid>
+        <Border  x:Name="MainBorder" Background="#80000000" CornerRadius="5">
+            <Grid>
+                <Border BorderBrush="Black" BorderThickness="1" CornerRadius="5" Margin="5">
+                <TextBlock x:Name="LatencyText" 
+                           VerticalAlignment="Center" 
+                           HorizontalAlignment="Center" 
+                               FontWeight="Bold"
+                               Padding="5"/>
+                </Border>
+                <ProgressBar x:Name="LatencyProgressBar" 
+                             VerticalAlignment="Bottom" 
+                             Margin="5"
+                             Minimum="0" 
+                             Maximum="200"
+                             Height="4"
+                             Style="{StaticResource ColoredProgressBar}"/>
+                <Border x:Name="DragArea"
+                        Background="Transparent"
+                        HorizontalAlignment="Stretch"
+                        VerticalAlignment="Top"
+                        Height="20"
+                        MouseDown="Window_MouseDown"/>
+            </Grid>
+        </Border>
+    </Grid>
+</Window>

+ 180 - 0
MainWindow.xaml.cs

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

+ 10 - 0
PingHandler.csproj

@@ -0,0 +1,10 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+    <PropertyGroup>
+        <OutputType>WinExe</OutputType>
+        <TargetFramework>net6.0-windows</TargetFramework>
+        <Nullable>enable</Nullable>
+        <UseWPF>true</UseWPF>
+    </PropertyGroup>
+
+</Project>

+ 16 - 0
PingHandler.sln

@@ -0,0 +1,16 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PingHandler", "PingHandler.csproj", "{8EAC00B8-ABD7-4C16-BE79-21C3633D539D}"
+EndProject
+Global
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|Any CPU = Debug|Any CPU
+		Release|Any CPU = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{8EAC00B8-ABD7-4C16-BE79-21C3633D539D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{8EAC00B8-ABD7-4C16-BE79-21C3633D539D}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{8EAC00B8-ABD7-4C16-BE79-21C3633D539D}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{8EAC00B8-ABD7-4C16-BE79-21C3633D539D}.Release|Any CPU.Build.0 = Release|Any CPU
+	EndGlobalSection
+EndGlobal