From f1693bce64b98de58695b8b5774881c3e44c3c3e Mon Sep 17 00:00:00 2001 From: crazypeace Date: Sun, 2 Jul 2023 17:23:45 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=A4=9A=E5=B1=8F=E5=B9=95?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E4=B8=8B=20=E8=8E=B7=E5=8F=96=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E8=BF=90=E8=A1=8C=E6=89=80=E5=9C=A8=E5=B1=8F=E5=B9=95?= =?UTF-8?q?=E7=9A=84DPI=E7=BC=A9=E6=94=BE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- v2rayN/v2rayN/App.xaml.cs | 1 + .../v2rayN/Converters/MaterialDesignFonts.cs | 4 +- v2rayN/v2rayN/Handler/HotkeyHandler.cs | 2 + v2rayN/v2rayN/Handler/MainFormHandler.cs | 5 +-- v2rayN/v2rayN/Tool/UI.cs | 1 + v2rayN/v2rayN/Tool/Utils.cs | 4 +- .../v2rayN/ViewModels/AddServer2ViewModel.cs | 2 +- .../v2rayN/ViewModels/MainWindowViewModel.cs | 4 +- .../ViewModels/RoutingRuleSettingViewModel.cs | 2 +- v2rayN/v2rayN/Views/AddServer2Window.xaml.cs | 35 +++++++++++++-- v2rayN/v2rayN/Views/AddServerWindow.xaml.cs | 35 +++++++++++++-- v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs | 42 +++++++++++++++--- .../Views/GlobalHotkeySettingWindow.xaml.cs | 42 +++++++++++++++--- v2rayN/v2rayN/Views/MainWindow.xaml.cs | 44 +++++++++++++++++-- .../v2rayN/Views/OptionSettingWindow.xaml.cs | 44 ++++++++++++++++--- v2rayN/v2rayN/Views/QrcodeView.xaml.cs | 1 + .../Views/RoutingRuleDetailsWindow.xaml.cs | 35 +++++++++++++-- .../Views/RoutingRuleSettingWindow.xaml.cs | 40 ++++++++++++++--- .../v2rayN/Views/RoutingSettingWindow.xaml.cs | 40 +++++++++++++++-- v2rayN/v2rayN/Views/SubEditWindow.xaml.cs | 35 +++++++++++++-- v2rayN/v2rayN/Views/SubSettingWindow.xaml.cs | 39 ++++++++++++++-- v2rayN/v2rayN/v2rayN.csproj | 1 + 22 files changed, 398 insertions(+), 60 deletions(-) diff --git a/v2rayN/v2rayN/App.xaml.cs b/v2rayN/v2rayN/App.xaml.cs index 37ca3478..8ed4ba32 100644 --- a/v2rayN/v2rayN/App.xaml.cs +++ b/v2rayN/v2rayN/App.xaml.cs @@ -3,6 +3,7 @@ using System.Windows.Threading; using v2rayN.Handler; using v2rayN.Mode; using v2rayN.Tool; +using Application = System.Windows.Application; namespace v2rayN { diff --git a/v2rayN/v2rayN/Converters/MaterialDesignFonts.cs b/v2rayN/v2rayN/Converters/MaterialDesignFonts.cs index 0b6908d1..bf58c4cd 100644 --- a/v2rayN/v2rayN/Converters/MaterialDesignFonts.cs +++ b/v2rayN/v2rayN/Converters/MaterialDesignFonts.cs @@ -1,5 +1,5 @@ -using System.Windows.Media; -using v2rayN.Handler; +using v2rayN.Handler; +using FontFamily = System.Windows.Media.FontFamily; namespace v2rayN.Converters { diff --git a/v2rayN/v2rayN/Handler/HotkeyHandler.cs b/v2rayN/v2rayN/Handler/HotkeyHandler.cs index 2848aaec..5a915452 100644 --- a/v2rayN/v2rayN/Handler/HotkeyHandler.cs +++ b/v2rayN/v2rayN/Handler/HotkeyHandler.cs @@ -6,6 +6,8 @@ using System.Windows.Input; using System.Windows.Interop; using v2rayN.Mode; using v2rayN.Resx; +using Application = System.Windows.Application; +using KeyEventArgs = System.Windows.Input.KeyEventArgs; namespace v2rayN.Handler { diff --git a/v2rayN/v2rayN/Handler/MainFormHandler.cs b/v2rayN/v2rayN/Handler/MainFormHandler.cs index e3e9ae8f..556432d6 100644 --- a/v2rayN/v2rayN/Handler/MainFormHandler.cs +++ b/v2rayN/v2rayN/Handler/MainFormHandler.cs @@ -1,9 +1,8 @@ -using Microsoft.Win32; -using System.Drawing; -using System.IO; +using System.IO; using System.Windows.Media.Imaging; using v2rayN.Mode; using v2rayN.Resx; +using SaveFileDialog = Microsoft.Win32.SaveFileDialog; namespace v2rayN.Handler { diff --git a/v2rayN/v2rayN/Tool/UI.cs b/v2rayN/v2rayN/Tool/UI.cs index dccbeee1..a8cf7a1d 100644 --- a/v2rayN/v2rayN/Tool/UI.cs +++ b/v2rayN/v2rayN/Tool/UI.cs @@ -1,4 +1,5 @@ using System.Windows; +using MessageBox = System.Windows.MessageBox; namespace v2rayN { diff --git a/v2rayN/v2rayN/Tool/Utils.cs b/v2rayN/v2rayN/Tool/Utils.cs index d65bebae..c83f231d 100644 --- a/v2rayN/v2rayN/Tool/Utils.cs +++ b/v2rayN/v2rayN/Tool/Utils.cs @@ -4,7 +4,6 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using NLog; using System.Diagnostics; -using System.Drawing; using System.IO; using System.IO.Compression; using System.Net; @@ -28,6 +27,9 @@ using ZXing; using ZXing.Common; using ZXing.QrCode; using ZXing.Windows.Compatibility; +using Clipboard = System.Windows.Clipboard; +using DataFormats = System.Windows.DataFormats; +using IDataObject = System.Windows.IDataObject; namespace v2rayN { diff --git a/v2rayN/v2rayN/ViewModels/AddServer2ViewModel.cs b/v2rayN/v2rayN/ViewModels/AddServer2ViewModel.cs index 53934b1f..dcd5cc93 100644 --- a/v2rayN/v2rayN/ViewModels/AddServer2ViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/AddServer2ViewModel.cs @@ -1,4 +1,3 @@ -using Microsoft.Win32; using ReactiveUI; using ReactiveUI.Fody.Helpers; using ReactiveUI.Validation.Helpers; @@ -10,6 +9,7 @@ using v2rayN.Base; using v2rayN.Handler; using v2rayN.Mode; using v2rayN.Resx; +using OpenFileDialog = Microsoft.Win32.OpenFileDialog; namespace v2rayN.ViewModels { diff --git a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs index 84204192..031e485f 100644 --- a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs @@ -3,12 +3,10 @@ using DynamicData.Binding; using MaterialDesignColors; using MaterialDesignColors.ColorManipulation; using MaterialDesignThemes.Wpf; -using Microsoft.Win32; using ReactiveUI; using ReactiveUI.Fody.Helpers; using Splat; using System.Diagnostics; -using System.Drawing; using System.IO; using System.Reactive; using System.Reactive.Linq; @@ -21,6 +19,8 @@ using v2rayN.Mode; using v2rayN.Resx; using v2rayN.Tool; using v2rayN.Views; +using Application = System.Windows.Application; +using OpenFileDialog = Microsoft.Win32.OpenFileDialog; namespace v2rayN.ViewModels { diff --git a/v2rayN/v2rayN/ViewModels/RoutingRuleSettingViewModel.cs b/v2rayN/v2rayN/ViewModels/RoutingRuleSettingViewModel.cs index 8c7c4273..992f9aeb 100644 --- a/v2rayN/v2rayN/ViewModels/RoutingRuleSettingViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/RoutingRuleSettingViewModel.cs @@ -1,5 +1,4 @@ using DynamicData.Binding; -using Microsoft.Win32; using ReactiveUI; using ReactiveUI.Fody.Helpers; using Splat; @@ -11,6 +10,7 @@ using v2rayN.Mode; using v2rayN.Resx; using v2rayN.Views; using Application = System.Windows.Application; +using OpenFileDialog = Microsoft.Win32.OpenFileDialog; namespace v2rayN.ViewModels { diff --git a/v2rayN/v2rayN/Views/AddServer2Window.xaml.cs b/v2rayN/v2rayN/Views/AddServer2Window.xaml.cs index 71946c9d..c1d57e00 100644 --- a/v2rayN/v2rayN/Views/AddServer2Window.xaml.cs +++ b/v2rayN/v2rayN/Views/AddServer2Window.xaml.cs @@ -3,6 +3,7 @@ using System.Reactive.Disposables; using System.Windows; using v2rayN.Mode; using v2rayN.ViewModels; +using Application = System.Windows.Application; namespace v2rayN.Views { @@ -11,10 +12,6 @@ namespace v2rayN.Views public AddServer2Window(ProfileItem profileItem) { InitializeComponent(); - - this.MaxWidth = SystemParameters.WorkArea.Width; - this.MaxHeight = SystemParameters.WorkArea.Height; - this.Owner = Application.Current.MainWindow; this.Loaded += Window_Loaded; ViewModel = new AddServer2ViewModel(profileItem, this); @@ -44,6 +41,36 @@ namespace v2rayN.Views private void Window_Loaded(object sender, RoutedEventArgs e) { txtRemarks.Focus(); + + // 获取当前屏幕的尺寸 + var screen = System.Windows.Forms.Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle); + var screenWidth = screen.WorkingArea.Width; + var screenHeight = screen.WorkingArea.Height; + var screenTop = screen.WorkingArea.Top; + + // 获取屏幕的 DPI 缩放因素 + double dpiFactor = 1; + PresentationSource source = PresentationSource.FromVisual(this); + if (source != null) + { + dpiFactor = source.CompositionTarget.TransformToDevice.M11; + } + + // 设置窗口尺寸不超过当前屏幕的尺寸 + if (this.Width > screenWidth / dpiFactor) + { + this.Width = screenWidth / dpiFactor; + } + if (this.Height > screenHeight / dpiFactor) + { + this.Height = screenHeight / dpiFactor; + } + + // 设置窗口不要显示在屏幕外面 + if (this.Top < screenTop / dpiFactor) + { + this.Top = screenTop / dpiFactor; + } } private void btnCancel_Click(object sender, System.Windows.RoutedEventArgs e) diff --git a/v2rayN/v2rayN/Views/AddServerWindow.xaml.cs b/v2rayN/v2rayN/Views/AddServerWindow.xaml.cs index e91076df..72bce547 100644 --- a/v2rayN/v2rayN/Views/AddServerWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/AddServerWindow.xaml.cs @@ -7,6 +7,7 @@ using v2rayN.Handler; using v2rayN.Mode; using v2rayN.Resx; using v2rayN.ViewModels; +using Application = System.Windows.Application; namespace v2rayN.Views { @@ -15,10 +16,6 @@ namespace v2rayN.Views public AddServerWindow(ProfileItem profileItem) { InitializeComponent(); - - this.MaxWidth = SystemParameters.WorkArea.Width; - this.MaxHeight = SystemParameters.WorkArea.Height; - this.Owner = Application.Current.MainWindow; this.Loaded += Window_Loaded; cmbNetwork.SelectionChanged += CmbNetwork_SelectionChanged; @@ -176,6 +173,36 @@ namespace v2rayN.Views private void Window_Loaded(object sender, RoutedEventArgs e) { txtRemarks.Focus(); + + // 获取当前屏幕的尺寸 + var screen = System.Windows.Forms.Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle); + var screenWidth = screen.WorkingArea.Width; + var screenHeight = screen.WorkingArea.Height; + var screenTop = screen.WorkingArea.Top; + + // 获取屏幕的 DPI 缩放因素 + double dpiFactor = 1; + PresentationSource source = PresentationSource.FromVisual(this); + if (source != null) + { + dpiFactor = source.CompositionTarget.TransformToDevice.M11; + } + + // 设置窗口尺寸不超过当前屏幕的尺寸 + if (this.Width > screenWidth / dpiFactor) + { + this.Width = screenWidth / dpiFactor; + } + if (this.Height > screenHeight / dpiFactor) + { + this.Height = screenHeight / dpiFactor; + } + + // 设置窗口不要显示在屏幕外面 + if (this.Top < screenTop / dpiFactor) + { + this.Top = screenTop / dpiFactor; + } } private void CmbNetwork_SelectionChanged(object sender, SelectionChangedEventArgs e) diff --git a/v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs index f23e32cb..6937f37d 100644 --- a/v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs @@ -4,6 +4,7 @@ using System.Windows; using v2rayN.Handler; using v2rayN.Mode; using v2rayN.ViewModels; +using Application = System.Windows.Application; namespace v2rayN.Views { @@ -14,13 +15,11 @@ namespace v2rayN.Views public DNSSettingWindow() { InitializeComponent(); - - this.MaxWidth = SystemParameters.WorkArea.Width; - this.MaxHeight = SystemParameters.WorkArea.Height; - this.Owner = Application.Current.MainWindow; - _config = LazyConfig.Instance.GetConfig(); + this.Loaded += Window_Loaded; + + _config = LazyConfig.Instance.GetConfig(); ViewModel = new DNSSettingViewModel(this); Global.domainStrategy4Freedoms.ForEach(it => @@ -41,6 +40,39 @@ namespace v2rayN.Views }); } + private void Window_Loaded(object sender, RoutedEventArgs e) + { + // 获取当前屏幕的尺寸 + var screen = System.Windows.Forms.Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle); + var screenWidth = screen.WorkingArea.Width; + var screenHeight = screen.WorkingArea.Height; + var screenTop = screen.WorkingArea.Top; + + // 获取屏幕的 DPI 缩放因素 + double dpiFactor = 1; + PresentationSource source = PresentationSource.FromVisual(this); + if (source != null) + { + dpiFactor = source.CompositionTarget.TransformToDevice.M11; + } + + // 设置窗口尺寸不超过当前屏幕的尺寸 + if (this.Width > screenWidth / dpiFactor) + { + this.Width = screenWidth / dpiFactor; + } + if (this.Height > screenHeight / dpiFactor) + { + this.Height = screenHeight / dpiFactor; + } + + // 设置窗口不要显示在屏幕外面 + if (this.Top < screenTop / dpiFactor) + { + this.Top = screenTop / dpiFactor; + } + } + private void linkDnsObjectDoc_Click(object sender, RoutedEventArgs e) { Utils.ProcessStart("https://www.v2fly.org/config/dns.html#dnsobject"); diff --git a/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml.cs b/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml.cs index f4c41538..f5eaa78c 100644 --- a/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml.cs @@ -1,10 +1,12 @@ using System.Text; using System.Windows; -using System.Windows.Controls; using System.Windows.Input; using v2rayN.Handler; using v2rayN.Mode; using v2rayN.Resx; +using Application = System.Windows.Application; +using KeyEventArgs = System.Windows.Input.KeyEventArgs; +using TextBox = System.Windows.Controls.TextBox; namespace v2rayN.Views { @@ -16,11 +18,8 @@ namespace v2rayN.Views public GlobalHotkeySettingWindow() { InitializeComponent(); - - this.MaxWidth = SystemParameters.WorkArea.Width; - this.MaxHeight = SystemParameters.WorkArea.Height; - this.Owner = Application.Current.MainWindow; + this.Loaded += Window_Loaded; _config = LazyConfig.Instance.GetConfig(); _config.globalHotkeys ??= new List(); @@ -36,6 +35,39 @@ namespace v2rayN.Views InitData(); } + private void Window_Loaded(object sender, RoutedEventArgs e) + { + // 获取当前屏幕的尺寸 + var screen = System.Windows.Forms.Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle); + var screenWidth = screen.WorkingArea.Width; + var screenHeight = screen.WorkingArea.Height; + var screenTop = screen.WorkingArea.Top; + + // 获取屏幕的 DPI 缩放因素 + double dpiFactor = 1; + PresentationSource source = PresentationSource.FromVisual(this); + if (source != null) + { + dpiFactor = source.CompositionTarget.TransformToDevice.M11; + } + + // 设置窗口尺寸不超过当前屏幕的尺寸 + if (this.Width > screenWidth / dpiFactor) + { + this.Width = screenWidth / dpiFactor; + } + if (this.Height > screenHeight / dpiFactor) + { + this.Height = screenHeight / dpiFactor; + } + + // 设置窗口不要显示在屏幕外面 + if (this.Top < screenTop / dpiFactor) + { + this.Top = screenTop / dpiFactor; + } + } + private void InitData() { _TextBoxKeyEventItem = new() diff --git a/v2rayN/v2rayN/Views/MainWindow.xaml.cs b/v2rayN/v2rayN/Views/MainWindow.xaml.cs index fad24fd2..e1a9bbe6 100644 --- a/v2rayN/v2rayN/Views/MainWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/MainWindow.xaml.cs @@ -14,6 +14,12 @@ using v2rayN.Handler; using v2rayN.Mode; using v2rayN.Resx; using v2rayN.ViewModels; +using Application = System.Windows.Application; +using DataObject = System.Windows.DataObject; +using DragDropEffects = System.Windows.DragDropEffects; +using DragEventArgs = System.Windows.DragEventArgs; +using KeyEventArgs = System.Windows.Input.KeyEventArgs; +using MouseEventArgs = System.Windows.Input.MouseEventArgs; using Point = System.Windows.Point; namespace v2rayN.Views @@ -25,10 +31,7 @@ namespace v2rayN.Views public MainWindow() { InitializeComponent(); - - this.MaxWidth = SystemParameters.WorkArea.Width; - this.MaxHeight = SystemParameters.WorkArea.Height; - + this.Loaded += Window_Loaded; _config = LazyConfig.Instance.GetConfig(); App.Current.SessionEnding += Current_SessionEnding; @@ -233,6 +236,39 @@ namespace v2rayN.Views }); } + private void Window_Loaded(object sender, RoutedEventArgs e) + { + // 获取当前屏幕的尺寸 + var screen = System.Windows.Forms.Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle); + var screenWidth = screen.WorkingArea.Width; + var screenHeight = screen.WorkingArea.Height; + var screenTop = screen.WorkingArea.Top; + + // 获取屏幕的 DPI 缩放因素 + double dpiFactor = 1; + PresentationSource source = PresentationSource.FromVisual(this); + if (source != null) + { + dpiFactor = source.CompositionTarget.TransformToDevice.M11; + } + + // 设置窗口尺寸不超过当前屏幕的尺寸 + if (this.Width > screenWidth / dpiFactor) + { + this.Width = screenWidth / dpiFactor; + } + if (this.Height > screenHeight / dpiFactor) + { + this.Height = screenHeight / dpiFactor; + } + + // 设置窗口不要显示在屏幕外面 + if (this.Top < screenTop / dpiFactor) + { + this.Top = screenTop / dpiFactor; + } + } + #region Event private void UpdateViewHandler(EViewAction action) diff --git a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs index d29d06b7..5ae3ee78 100644 --- a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs @@ -1,5 +1,4 @@ -using Microsoft.Win32; -using ReactiveUI; +using ReactiveUI; using System.Globalization; using System.IO; using System.Reactive.Disposables; @@ -8,6 +7,9 @@ using System.Windows.Media; using v2rayN.Handler; using v2rayN.Mode; using v2rayN.ViewModels; +using Application = System.Windows.Application; +using FontFamily = System.Windows.Media.FontFamily; +using OpenFileDialog = Microsoft.Win32.OpenFileDialog; namespace v2rayN.Views { @@ -18,11 +20,8 @@ namespace v2rayN.Views public OptionSettingWindow() { InitializeComponent(); - - this.MaxWidth = SystemParameters.WorkArea.Width; - this.MaxHeight = SystemParameters.WorkArea.Height; - this.Owner = Application.Current.MainWindow; + this.Loaded += Window_Loaded; _config = LazyConfig.Instance.GetConfig(); ViewModel = new OptionSettingViewModel(this); @@ -184,6 +183,39 @@ namespace v2rayN.Views }); } + private void Window_Loaded(object sender, RoutedEventArgs e) + { + // 获取当前屏幕的尺寸 + var screen = System.Windows.Forms.Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle); + var screenWidth = screen.WorkingArea.Width; + var screenHeight = screen.WorkingArea.Height; + var screenTop = screen.WorkingArea.Top; + + // 获取屏幕的 DPI 缩放因素 + double dpiFactor = 1; + PresentationSource source = PresentationSource.FromVisual(this); + if (source != null) + { + dpiFactor = source.CompositionTarget.TransformToDevice.M11; + } + + // 设置窗口尺寸不超过当前屏幕的尺寸 + if (this.Width > screenWidth / dpiFactor) + { + this.Width = screenWidth / dpiFactor; + } + if (this.Height > screenHeight / dpiFactor) + { + this.Height = screenHeight / dpiFactor; + } + + // 设置窗口不要显示在屏幕外面 + if (this.Top < screenTop / dpiFactor) + { + this.Top = screenTop / dpiFactor; + } + } + private void btnCancel_Click(object sender, RoutedEventArgs e) { this.Close(); diff --git a/v2rayN/v2rayN/Views/QrcodeView.xaml.cs b/v2rayN/v2rayN/Views/QrcodeView.xaml.cs index b3e7e964..44f494e0 100644 --- a/v2rayN/v2rayN/Views/QrcodeView.xaml.cs +++ b/v2rayN/v2rayN/Views/QrcodeView.xaml.cs @@ -1,4 +1,5 @@ using System.Windows.Controls; +using UserControl = System.Windows.Controls.UserControl; namespace v2rayN.Views { diff --git a/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml.cs b/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml.cs index 400bf64c..6a89f206 100644 --- a/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml.cs @@ -4,6 +4,7 @@ using System.Windows; using v2rayN.Base; using v2rayN.Mode; using v2rayN.ViewModels; +using Application = System.Windows.Application; namespace v2rayN.Views { @@ -12,10 +13,6 @@ namespace v2rayN.Views public RoutingRuleDetailsWindow(RulesItem rulesItem) { InitializeComponent(); - - this.MaxWidth = SystemParameters.WorkArea.Width; - this.MaxHeight = SystemParameters.WorkArea.Height; - this.Owner = Application.Current.MainWindow; this.Loaded += Window_Loaded; clbProtocol.SelectionChanged += ClbProtocol_SelectionChanged; @@ -63,6 +60,36 @@ namespace v2rayN.Views private void Window_Loaded(object sender, RoutedEventArgs e) { cmbOutboundTag.Focus(); + + // 获取当前屏幕的尺寸 + var screen = System.Windows.Forms.Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle); + var screenWidth = screen.WorkingArea.Width; + var screenHeight = screen.WorkingArea.Height; + var screenTop = screen.WorkingArea.Top; + + // 获取屏幕的 DPI 缩放因素 + double dpiFactor = 1; + PresentationSource source = PresentationSource.FromVisual(this); + if (source != null) + { + dpiFactor = source.CompositionTarget.TransformToDevice.M11; + } + + // 设置窗口尺寸不超过当前屏幕的尺寸 + if (this.Width > screenWidth / dpiFactor) + { + this.Width = screenWidth / dpiFactor; + } + if (this.Height > screenHeight / dpiFactor) + { + this.Height = screenHeight / dpiFactor; + } + + // 设置窗口不要显示在屏幕外面 + if (this.Top < screenTop / dpiFactor) + { + this.Top = screenTop / dpiFactor; + } } private void ClbProtocol_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) diff --git a/v2rayN/v2rayN/Views/RoutingRuleSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/RoutingRuleSettingWindow.xaml.cs index 5a0c8d88..c8c40abf 100644 --- a/v2rayN/v2rayN/Views/RoutingRuleSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/RoutingRuleSettingWindow.xaml.cs @@ -1,10 +1,12 @@ -using Microsoft.Win32; -using ReactiveUI; +using ReactiveUI; using System.Reactive.Disposables; using System.Windows; using System.Windows.Input; using v2rayN.Mode; using v2rayN.ViewModels; +using Application = System.Windows.Application; +using KeyEventArgs = System.Windows.Input.KeyEventArgs; +using OpenFileDialog = Microsoft.Win32.OpenFileDialog; namespace v2rayN.Views { @@ -13,10 +15,6 @@ namespace v2rayN.Views public RoutingRuleSettingWindow(RoutingItem routingItem) { InitializeComponent(); - - this.MaxWidth = SystemParameters.WorkArea.Width; - this.MaxHeight = SystemParameters.WorkArea.Height; - this.Owner = Application.Current.MainWindow; this.Loaded += Window_Loaded; this.PreviewKeyDown += RoutingRuleSettingWindow_PreviewKeyDown; @@ -67,6 +65,36 @@ namespace v2rayN.Views private void Window_Loaded(object sender, RoutedEventArgs e) { txtRemarks.Focus(); + + // 获取当前屏幕的尺寸 + var screen = System.Windows.Forms.Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle); + var screenWidth = screen.WorkingArea.Width; + var screenHeight = screen.WorkingArea.Height; + var screenTop = screen.WorkingArea.Top; + + // 获取屏幕的 DPI 缩放因素 + double dpiFactor = 1; + PresentationSource source = PresentationSource.FromVisual(this); + if (source != null) + { + dpiFactor = source.CompositionTarget.TransformToDevice.M11; + } + + // 设置窗口尺寸不超过当前屏幕的尺寸 + if (this.Width > screenWidth / dpiFactor) + { + this.Width = screenWidth / dpiFactor; + } + if (this.Height > screenHeight / dpiFactor) + { + this.Height = screenHeight / dpiFactor; + } + + // 设置窗口不要显示在屏幕外面 + if (this.Top < screenTop / dpiFactor) + { + this.Top = screenTop / dpiFactor; + } } private void RoutingRuleSettingWindow_PreviewKeyDown(object sender, KeyEventArgs e) diff --git a/v2rayN/v2rayN/Views/RoutingSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/RoutingSettingWindow.xaml.cs index 8d4d712c..43f1b8f5 100644 --- a/v2rayN/v2rayN/Views/RoutingSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/RoutingSettingWindow.xaml.cs @@ -4,6 +4,8 @@ using System.Windows; using System.Windows.Input; using v2rayN.Mode; using v2rayN.ViewModels; +using Application = System.Windows.Application; +using KeyEventArgs = System.Windows.Input.KeyEventArgs; namespace v2rayN.Views { @@ -12,11 +14,8 @@ namespace v2rayN.Views public RoutingSettingWindow() { InitializeComponent(); - - this.MaxWidth = SystemParameters.WorkArea.Width; - this.MaxHeight = SystemParameters.WorkArea.Height; - this.Owner = Application.Current.MainWindow; + this.Loaded += Window_Loaded; this.Closing += RoutingSettingWindow_Closing; this.PreviewKeyDown += RoutingSettingWindow_PreviewKeyDown; lstRoutings.SelectionChanged += lstRoutings_SelectionChanged; @@ -71,6 +70,39 @@ namespace v2rayN.Views }); } + private void Window_Loaded(object sender, RoutedEventArgs e) + { + // 获取当前屏幕的尺寸 + var screen = System.Windows.Forms.Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle); + var screenWidth = screen.WorkingArea.Width; + var screenHeight = screen.WorkingArea.Height; + var screenTop = screen.WorkingArea.Top; + + // 获取屏幕的 DPI 缩放因素 + double dpiFactor = 1; + PresentationSource source = PresentationSource.FromVisual(this); + if (source != null) + { + dpiFactor = source.CompositionTarget.TransformToDevice.M11; + } + + // 设置窗口尺寸不超过当前屏幕的尺寸 + if (this.Width > screenWidth / dpiFactor) + { + this.Width = screenWidth / dpiFactor; + } + if (this.Height > screenHeight / dpiFactor) + { + this.Height = screenHeight / dpiFactor; + } + + // 设置窗口不要显示在屏幕外面 + if (this.Top < screenTop / dpiFactor) + { + this.Top = screenTop / dpiFactor; + } + } + private void RoutingSettingWindow_Closing(object? sender, System.ComponentModel.CancelEventArgs e) { if (ViewModel?.IsModified == true) diff --git a/v2rayN/v2rayN/Views/SubEditWindow.xaml.cs b/v2rayN/v2rayN/Views/SubEditWindow.xaml.cs index 4021d176..9423708a 100644 --- a/v2rayN/v2rayN/Views/SubEditWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/SubEditWindow.xaml.cs @@ -3,6 +3,7 @@ using System.Reactive.Disposables; using System.Windows; using v2rayN.Mode; using v2rayN.ViewModels; +using Application = System.Windows.Application; namespace v2rayN.Views { @@ -11,10 +12,6 @@ namespace v2rayN.Views public SubEditWindow(SubItem subItem) { InitializeComponent(); - - this.MaxWidth = SystemParameters.WorkArea.Width; - this.MaxHeight = SystemParameters.WorkArea.Height; - this.Owner = Application.Current.MainWindow; this.Loaded += Window_Loaded; @@ -44,6 +41,36 @@ namespace v2rayN.Views private void Window_Loaded(object sender, RoutedEventArgs e) { txtRemarks.Focus(); + + // 获取当前屏幕的尺寸 + var screen = System.Windows.Forms.Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle); + var screenWidth = screen.WorkingArea.Width; + var screenHeight = screen.WorkingArea.Height; + var screenTop = screen.WorkingArea.Top; + + // 获取屏幕的 DPI 缩放因素 + double dpiFactor = 1; + PresentationSource source = PresentationSource.FromVisual(this); + if (source != null) + { + dpiFactor = source.CompositionTarget.TransformToDevice.M11; + } + + // 设置窗口尺寸不超过当前屏幕的尺寸 + if (this.Width > screenWidth / dpiFactor) + { + this.Width = screenWidth / dpiFactor; + } + if (this.Height > screenHeight / dpiFactor) + { + this.Height = screenHeight / dpiFactor; + } + + // 设置窗口不要显示在屏幕外面 + if (this.Top < screenTop / dpiFactor) + { + this.Top = screenTop / dpiFactor; + } } private void btnCancel_Click(object sender, RoutedEventArgs e) diff --git a/v2rayN/v2rayN/Views/SubSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/SubSettingWindow.xaml.cs index 57d4b86a..2be73fcb 100644 --- a/v2rayN/v2rayN/Views/SubSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/SubSettingWindow.xaml.cs @@ -5,6 +5,7 @@ using System.Windows; using System.Windows.Input; using v2rayN.Mode; using v2rayN.ViewModels; +using Application = System.Windows.Application; namespace v2rayN.Views { @@ -13,11 +14,8 @@ namespace v2rayN.Views public SubSettingWindow() { InitializeComponent(); - - this.MaxWidth = SystemParameters.WorkArea.Width; - this.MaxHeight = SystemParameters.WorkArea.Height; - this.Owner = Application.Current.MainWindow; + this.Loaded += Window_Loaded; ViewModel = new SubSettingViewModel(this); this.Closing += SubSettingWindow_Closing; @@ -36,6 +34,39 @@ namespace v2rayN.Views }); } + private void Window_Loaded(object sender, RoutedEventArgs e) + { + // 获取当前屏幕的尺寸 + var screen = System.Windows.Forms.Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle); + var screenWidth = screen.WorkingArea.Width; + var screenHeight = screen.WorkingArea.Height; + var screenTop = screen.WorkingArea.Top; + + // 获取屏幕的 DPI 缩放因素 + double dpiFactor = 1; + PresentationSource source = PresentationSource.FromVisual(this); + if (source != null) + { + dpiFactor = source.CompositionTarget.TransformToDevice.M11; + } + + // 设置窗口尺寸不超过当前屏幕的尺寸 + if (this.Width > screenWidth / dpiFactor) + { + this.Width = screenWidth / dpiFactor; + } + if (this.Height > screenHeight / dpiFactor) + { + this.Height = screenHeight / dpiFactor; + } + + // 设置窗口不要显示在屏幕外面 + if (this.Top < screenTop / dpiFactor) + { + this.Top = screenTop / dpiFactor; + } + } + private void SubSettingWindow_Closing(object? sender, CancelEventArgs e) { if (ViewModel?.IsModified == true) diff --git a/v2rayN/v2rayN/v2rayN.csproj b/v2rayN/v2rayN/v2rayN.csproj index 83a1e1de..eef09331 100644 --- a/v2rayN/v2rayN/v2rayN.csproj +++ b/v2rayN/v2rayN/v2rayN.csproj @@ -11,6 +11,7 @@ v2rayN.ico Copyright © 2017-2023 (GPLv3) 6.27 + True