解决多屏幕环境下 获取程序运行所在屏幕的DPI缩放的问题

This commit is contained in:
crazypeace 2023-07-02 20:34:24 +08:00
parent f1693bce64
commit b84806338c
17 changed files with 309 additions and 135 deletions

View file

@ -1,4 +1,5 @@
using v2rayN.Handler; using System.Windows.Media;
using v2rayN.Handler;
using FontFamily = System.Windows.Media.FontFamily; using FontFamily = System.Windows.Media.FontFamily;
namespace v2rayN.Converters namespace v2rayN.Converters

View file

@ -1,4 +1,6 @@
using System.IO; using Microsoft.Win32;
using System.Drawing;
using System.IO;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using v2rayN.Mode; using v2rayN.Mode;
using v2rayN.Resx; using v2rayN.Resx;

View file

@ -4,6 +4,7 @@ using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using NLog; using NLog;
using System.Diagnostics; using System.Diagnostics;
using System.Drawing;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Net; using System.Net;

View file

@ -1,3 +1,4 @@
using Microsoft.Win32;
using ReactiveUI; using ReactiveUI;
using ReactiveUI.Fody.Helpers; using ReactiveUI.Fody.Helpers;
using ReactiveUI.Validation.Helpers; using ReactiveUI.Validation.Helpers;

View file

@ -3,10 +3,12 @@ using DynamicData.Binding;
using MaterialDesignColors; using MaterialDesignColors;
using MaterialDesignColors.ColorManipulation; using MaterialDesignColors.ColorManipulation;
using MaterialDesignThemes.Wpf; using MaterialDesignThemes.Wpf;
using Microsoft.Win32;
using ReactiveUI; using ReactiveUI;
using ReactiveUI.Fody.Helpers; using ReactiveUI.Fody.Helpers;
using Splat; using Splat;
using System.Diagnostics; using System.Diagnostics;
using System.Drawing;
using System.IO; using System.IO;
using System.Reactive; using System.Reactive;
using System.Reactive.Linq; using System.Reactive.Linq;

View file

@ -2,6 +2,7 @@
using ReactiveUI; using ReactiveUI;
using ReactiveUI.Fody.Helpers; using ReactiveUI.Fody.Helpers;
using Splat; using Splat;
using Microsoft.Win32;
using System.Reactive; using System.Reactive;
using System.Windows; using System.Windows;
using v2rayN.Base; using v2rayN.Base;

View file

@ -42,12 +42,6 @@ namespace v2rayN.Views
{ {
txtRemarks.Focus(); 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 缩放因素 // 获取屏幕的 DPI 缩放因素
double dpiFactor = 1; double dpiFactor = 1;
PresentationSource source = PresentationSource.FromVisual(this); PresentationSource source = PresentationSource.FromVisual(this);
@ -56,20 +50,41 @@ namespace v2rayN.Views
dpiFactor = source.CompositionTarget.TransformToDevice.M11; dpiFactor = source.CompositionTarget.TransformToDevice.M11;
} }
// 获取当前屏幕的尺寸
var screen = System.Windows.Forms.Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle);
var screenWidth = screen.WorkingArea.Width / dpiFactor;
var screenHeight = screen.WorkingArea.Height / dpiFactor;
var screenTop = screen.WorkingArea.Top / dpiFactor;
var screenLeft = screen.WorkingArea.Left / dpiFactor;
var screenBottom = screen.WorkingArea.Bottom / dpiFactor;
var screenRight = screen.WorkingArea.Right / dpiFactor;
// 设置窗口尺寸不超过当前屏幕的尺寸 // 设置窗口尺寸不超过当前屏幕的尺寸
if (this.Width > screenWidth / dpiFactor) if (this.Width > screenWidth)
{ {
this.Width = screenWidth / dpiFactor; this.Width = screenWidth;
} }
if (this.Height > screenHeight / dpiFactor) if (this.Height > screenHeight)
{ {
this.Height = screenHeight / dpiFactor; this.Height = screenHeight;
} }
// 设置窗口不要显示在屏幕外面 // 设置窗口不要显示在屏幕外面
if (this.Top < screenTop / dpiFactor) if (this.Top < screenTop)
{ {
this.Top = screenTop / dpiFactor; this.Top = screenTop;
}
if (this.Left < screenLeft)
{
this.Left = screenLeft;
}
if (this.Top + this.Height > screenBottom)
{
this.Top = screenBottom - this.Height;
}
if (this.Left + this.Width > screenRight)
{
this.Left = screenRight - this.Width;
} }
} }

View file

@ -174,12 +174,6 @@ namespace v2rayN.Views
{ {
txtRemarks.Focus(); 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 缩放因素 // 获取屏幕的 DPI 缩放因素
double dpiFactor = 1; double dpiFactor = 1;
PresentationSource source = PresentationSource.FromVisual(this); PresentationSource source = PresentationSource.FromVisual(this);
@ -188,20 +182,41 @@ namespace v2rayN.Views
dpiFactor = source.CompositionTarget.TransformToDevice.M11; dpiFactor = source.CompositionTarget.TransformToDevice.M11;
} }
// 获取当前屏幕的尺寸
var screen = System.Windows.Forms.Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle);
var screenWidth = screen.WorkingArea.Width / dpiFactor;
var screenHeight = screen.WorkingArea.Height / dpiFactor;
var screenTop = screen.WorkingArea.Top / dpiFactor;
var screenLeft = screen.WorkingArea.Left / dpiFactor;
var screenBottom = screen.WorkingArea.Bottom / dpiFactor;
var screenRight = screen.WorkingArea.Right / dpiFactor;
// 设置窗口尺寸不超过当前屏幕的尺寸 // 设置窗口尺寸不超过当前屏幕的尺寸
if (this.Width > screenWidth / dpiFactor) if (this.Width > screenWidth)
{ {
this.Width = screenWidth / dpiFactor; this.Width = screenWidth;
} }
if (this.Height > screenHeight / dpiFactor) if (this.Height > screenHeight)
{ {
this.Height = screenHeight / dpiFactor; this.Height = screenHeight;
} }
// 设置窗口不要显示在屏幕外面 // 设置窗口不要显示在屏幕外面
if (this.Top < screenTop / dpiFactor) if (this.Top < screenTop)
{ {
this.Top = screenTop / dpiFactor; this.Top = screenTop;
}
if (this.Left < screenLeft)
{
this.Left = screenLeft;
}
if (this.Top + this.Height > screenBottom)
{
this.Top = screenBottom - this.Height;
}
if (this.Left + this.Width > screenRight)
{
this.Left = screenRight - this.Width;
} }
} }

View file

@ -42,12 +42,6 @@ namespace v2rayN.Views
private void Window_Loaded(object sender, RoutedEventArgs e) 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 缩放因素 // 获取屏幕的 DPI 缩放因素
double dpiFactor = 1; double dpiFactor = 1;
PresentationSource source = PresentationSource.FromVisual(this); PresentationSource source = PresentationSource.FromVisual(this);
@ -56,20 +50,41 @@ namespace v2rayN.Views
dpiFactor = source.CompositionTarget.TransformToDevice.M11; dpiFactor = source.CompositionTarget.TransformToDevice.M11;
} }
// 获取当前屏幕的尺寸
var screen = System.Windows.Forms.Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle);
var screenWidth = screen.WorkingArea.Width / dpiFactor;
var screenHeight = screen.WorkingArea.Height / dpiFactor;
var screenTop = screen.WorkingArea.Top / dpiFactor;
var screenLeft = screen.WorkingArea.Left / dpiFactor;
var screenBottom = screen.WorkingArea.Bottom / dpiFactor;
var screenRight = screen.WorkingArea.Right / dpiFactor;
// 设置窗口尺寸不超过当前屏幕的尺寸 // 设置窗口尺寸不超过当前屏幕的尺寸
if (this.Width > screenWidth / dpiFactor) if (this.Width > screenWidth)
{ {
this.Width = screenWidth / dpiFactor; this.Width = screenWidth;
} }
if (this.Height > screenHeight / dpiFactor) if (this.Height > screenHeight)
{ {
this.Height = screenHeight / dpiFactor; this.Height = screenHeight;
} }
// 设置窗口不要显示在屏幕外面 // 设置窗口不要显示在屏幕外面
if (this.Top < screenTop / dpiFactor) if (this.Top < screenTop)
{ {
this.Top = screenTop / dpiFactor; this.Top = screenTop;
}
if (this.Left < screenLeft)
{
this.Left = screenLeft;
}
if (this.Top + this.Height > screenBottom)
{
this.Top = screenBottom - this.Height;
}
if (this.Left + this.Width > screenRight)
{
this.Left = screenRight - this.Width;
} }
} }

View file

@ -37,12 +37,6 @@ namespace v2rayN.Views
private void Window_Loaded(object sender, RoutedEventArgs e) 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 缩放因素 // 获取屏幕的 DPI 缩放因素
double dpiFactor = 1; double dpiFactor = 1;
PresentationSource source = PresentationSource.FromVisual(this); PresentationSource source = PresentationSource.FromVisual(this);
@ -51,20 +45,41 @@ namespace v2rayN.Views
dpiFactor = source.CompositionTarget.TransformToDevice.M11; dpiFactor = source.CompositionTarget.TransformToDevice.M11;
} }
// 获取当前屏幕的尺寸
var screen = System.Windows.Forms.Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle);
var screenWidth = screen.WorkingArea.Width / dpiFactor;
var screenHeight = screen.WorkingArea.Height / dpiFactor;
var screenTop = screen.WorkingArea.Top / dpiFactor;
var screenLeft = screen.WorkingArea.Left / dpiFactor;
var screenBottom = screen.WorkingArea.Bottom / dpiFactor;
var screenRight = screen.WorkingArea.Right / dpiFactor;
// 设置窗口尺寸不超过当前屏幕的尺寸 // 设置窗口尺寸不超过当前屏幕的尺寸
if (this.Width > screenWidth / dpiFactor) if (this.Width > screenWidth)
{ {
this.Width = screenWidth / dpiFactor; this.Width = screenWidth;
} }
if (this.Height > screenHeight / dpiFactor) if (this.Height > screenHeight)
{ {
this.Height = screenHeight / dpiFactor; this.Height = screenHeight;
} }
// 设置窗口不要显示在屏幕外面 // 设置窗口不要显示在屏幕外面
if (this.Top < screenTop / dpiFactor) if (this.Top < screenTop)
{ {
this.Top = screenTop / dpiFactor; this.Top = screenTop;
}
if (this.Left < screenLeft)
{
this.Left = screenLeft;
}
if (this.Top + this.Height > screenBottom)
{
this.Top = screenBottom - this.Height;
}
if (this.Left + this.Width > screenRight)
{
this.Left = screenRight - this.Width;
} }
} }

View file

@ -238,12 +238,6 @@ namespace v2rayN.Views
private void Window_Loaded(object sender, RoutedEventArgs e) 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 缩放因素 // 获取屏幕的 DPI 缩放因素
double dpiFactor = 1; double dpiFactor = 1;
PresentationSource source = PresentationSource.FromVisual(this); PresentationSource source = PresentationSource.FromVisual(this);
@ -252,20 +246,41 @@ namespace v2rayN.Views
dpiFactor = source.CompositionTarget.TransformToDevice.M11; dpiFactor = source.CompositionTarget.TransformToDevice.M11;
} }
// 获取当前屏幕的尺寸
var screen = System.Windows.Forms.Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle);
var screenWidth = screen.WorkingArea.Width / dpiFactor;
var screenHeight = screen.WorkingArea.Height / dpiFactor;
var screenTop = screen.WorkingArea.Top / dpiFactor;
var screenLeft = screen.WorkingArea.Left / dpiFactor;
var screenBottom = screen.WorkingArea.Bottom / dpiFactor;
var screenRight = screen.WorkingArea.Right / dpiFactor;
// 设置窗口尺寸不超过当前屏幕的尺寸 // 设置窗口尺寸不超过当前屏幕的尺寸
if (this.Width > screenWidth / dpiFactor) if (this.Width > screenWidth)
{ {
this.Width = screenWidth / dpiFactor; this.Width = screenWidth;
} }
if (this.Height > screenHeight / dpiFactor) if (this.Height > screenHeight)
{ {
this.Height = screenHeight / dpiFactor; this.Height = screenHeight;
} }
// 设置窗口不要显示在屏幕外面 // 设置窗口不要显示在屏幕外面
if (this.Top < screenTop / dpiFactor) if (this.Top < screenTop)
{ {
this.Top = screenTop / dpiFactor; this.Top = screenTop;
}
if (this.Left < screenLeft)
{
this.Left = screenLeft;
}
if (this.Top + this.Height > screenBottom)
{
this.Top = screenBottom - this.Height;
}
if (this.Left + this.Width > screenRight)
{
this.Left = screenRight - this.Width;
} }
} }

View file

@ -1,4 +1,5 @@
using ReactiveUI; using Microsoft.Win32;
using ReactiveUI;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Reactive.Disposables; using System.Reactive.Disposables;
@ -185,12 +186,6 @@ namespace v2rayN.Views
private void Window_Loaded(object sender, RoutedEventArgs e) 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 缩放因素 // 获取屏幕的 DPI 缩放因素
double dpiFactor = 1; double dpiFactor = 1;
PresentationSource source = PresentationSource.FromVisual(this); PresentationSource source = PresentationSource.FromVisual(this);
@ -199,20 +194,41 @@ namespace v2rayN.Views
dpiFactor = source.CompositionTarget.TransformToDevice.M11; dpiFactor = source.CompositionTarget.TransformToDevice.M11;
} }
// 获取当前屏幕的尺寸
var screen = System.Windows.Forms.Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle);
var screenWidth = screen.WorkingArea.Width / dpiFactor;
var screenHeight = screen.WorkingArea.Height / dpiFactor;
var screenTop = screen.WorkingArea.Top / dpiFactor;
var screenLeft = screen.WorkingArea.Left / dpiFactor;
var screenBottom = screen.WorkingArea.Bottom / dpiFactor;
var screenRight = screen.WorkingArea.Right / dpiFactor;
// 设置窗口尺寸不超过当前屏幕的尺寸 // 设置窗口尺寸不超过当前屏幕的尺寸
if (this.Width > screenWidth / dpiFactor) if (this.Width > screenWidth)
{ {
this.Width = screenWidth / dpiFactor; this.Width = screenWidth;
} }
if (this.Height > screenHeight / dpiFactor) if (this.Height > screenHeight)
{ {
this.Height = screenHeight / dpiFactor; this.Height = screenHeight;
} }
// 设置窗口不要显示在屏幕外面 // 设置窗口不要显示在屏幕外面
if (this.Top < screenTop / dpiFactor) if (this.Top < screenTop)
{ {
this.Top = screenTop / dpiFactor; this.Top = screenTop;
}
if (this.Left < screenLeft)
{
this.Left = screenLeft;
}
if (this.Top + this.Height > screenBottom)
{
this.Top = screenBottom - this.Height;
}
if (this.Left + this.Width > screenRight)
{
this.Left = screenRight - this.Width;
} }
} }

View file

@ -61,12 +61,6 @@ namespace v2rayN.Views
{ {
cmbOutboundTag.Focus(); 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 缩放因素 // 获取屏幕的 DPI 缩放因素
double dpiFactor = 1; double dpiFactor = 1;
PresentationSource source = PresentationSource.FromVisual(this); PresentationSource source = PresentationSource.FromVisual(this);
@ -75,20 +69,41 @@ namespace v2rayN.Views
dpiFactor = source.CompositionTarget.TransformToDevice.M11; dpiFactor = source.CompositionTarget.TransformToDevice.M11;
} }
// 获取当前屏幕的尺寸
var screen = System.Windows.Forms.Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle);
var screenWidth = screen.WorkingArea.Width / dpiFactor;
var screenHeight = screen.WorkingArea.Height / dpiFactor;
var screenTop = screen.WorkingArea.Top / dpiFactor;
var screenLeft = screen.WorkingArea.Left / dpiFactor;
var screenBottom = screen.WorkingArea.Bottom / dpiFactor;
var screenRight = screen.WorkingArea.Right / dpiFactor;
// 设置窗口尺寸不超过当前屏幕的尺寸 // 设置窗口尺寸不超过当前屏幕的尺寸
if (this.Width > screenWidth / dpiFactor) if (this.Width > screenWidth)
{ {
this.Width = screenWidth / dpiFactor; this.Width = screenWidth;
} }
if (this.Height > screenHeight / dpiFactor) if (this.Height > screenHeight)
{ {
this.Height = screenHeight / dpiFactor; this.Height = screenHeight;
} }
// 设置窗口不要显示在屏幕外面 // 设置窗口不要显示在屏幕外面
if (this.Top < screenTop / dpiFactor) if (this.Top < screenTop)
{ {
this.Top = screenTop / dpiFactor; this.Top = screenTop;
}
if (this.Left < screenLeft)
{
this.Left = screenLeft;
}
if (this.Top + this.Height > screenBottom)
{
this.Top = screenBottom - this.Height;
}
if (this.Left + this.Width > screenRight)
{
this.Left = screenRight - this.Width;
} }
} }

View file

@ -66,12 +66,6 @@ namespace v2rayN.Views
{ {
txtRemarks.Focus(); 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 缩放因素 // 获取屏幕的 DPI 缩放因素
double dpiFactor = 1; double dpiFactor = 1;
PresentationSource source = PresentationSource.FromVisual(this); PresentationSource source = PresentationSource.FromVisual(this);
@ -80,20 +74,41 @@ namespace v2rayN.Views
dpiFactor = source.CompositionTarget.TransformToDevice.M11; dpiFactor = source.CompositionTarget.TransformToDevice.M11;
} }
// 获取当前屏幕的尺寸
var screen = System.Windows.Forms.Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle);
var screenWidth = screen.WorkingArea.Width / dpiFactor;
var screenHeight = screen.WorkingArea.Height / dpiFactor;
var screenTop = screen.WorkingArea.Top / dpiFactor;
var screenLeft = screen.WorkingArea.Left / dpiFactor;
var screenBottom = screen.WorkingArea.Bottom / dpiFactor;
var screenRight = screen.WorkingArea.Right / dpiFactor;
// 设置窗口尺寸不超过当前屏幕的尺寸 // 设置窗口尺寸不超过当前屏幕的尺寸
if (this.Width > screenWidth / dpiFactor) if (this.Width > screenWidth)
{ {
this.Width = screenWidth / dpiFactor; this.Width = screenWidth;
} }
if (this.Height > screenHeight / dpiFactor) if (this.Height > screenHeight)
{ {
this.Height = screenHeight / dpiFactor; this.Height = screenHeight;
} }
// 设置窗口不要显示在屏幕外面 // 设置窗口不要显示在屏幕外面
if (this.Top < screenTop / dpiFactor) if (this.Top < screenTop)
{ {
this.Top = screenTop / dpiFactor; this.Top = screenTop;
}
if (this.Left < screenLeft)
{
this.Left = screenLeft;
}
if (this.Top + this.Height > screenBottom)
{
this.Top = screenBottom - this.Height;
}
if (this.Left + this.Width > screenRight)
{
this.Left = screenRight - this.Width;
} }
} }

View file

@ -72,12 +72,6 @@ namespace v2rayN.Views
private void Window_Loaded(object sender, RoutedEventArgs e) 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 缩放因素 // 获取屏幕的 DPI 缩放因素
double dpiFactor = 1; double dpiFactor = 1;
PresentationSource source = PresentationSource.FromVisual(this); PresentationSource source = PresentationSource.FromVisual(this);
@ -86,20 +80,41 @@ namespace v2rayN.Views
dpiFactor = source.CompositionTarget.TransformToDevice.M11; dpiFactor = source.CompositionTarget.TransformToDevice.M11;
} }
// 获取当前屏幕的尺寸
var screen = System.Windows.Forms.Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle);
var screenWidth = screen.WorkingArea.Width / dpiFactor;
var screenHeight = screen.WorkingArea.Height / dpiFactor;
var screenTop = screen.WorkingArea.Top / dpiFactor;
var screenLeft = screen.WorkingArea.Left / dpiFactor;
var screenBottom = screen.WorkingArea.Bottom / dpiFactor;
var screenRight = screen.WorkingArea.Right / dpiFactor;
// 设置窗口尺寸不超过当前屏幕的尺寸 // 设置窗口尺寸不超过当前屏幕的尺寸
if (this.Width > screenWidth / dpiFactor) if (this.Width > screenWidth)
{ {
this.Width = screenWidth / dpiFactor; this.Width = screenWidth;
} }
if (this.Height > screenHeight / dpiFactor) if (this.Height > screenHeight)
{ {
this.Height = screenHeight / dpiFactor; this.Height = screenHeight;
} }
// 设置窗口不要显示在屏幕外面 // 设置窗口不要显示在屏幕外面
if (this.Top < screenTop / dpiFactor) if (this.Top < screenTop)
{ {
this.Top = screenTop / dpiFactor; this.Top = screenTop;
}
if (this.Left < screenLeft)
{
this.Left = screenLeft;
}
if (this.Top + this.Height > screenBottom)
{
this.Top = screenBottom - this.Height;
}
if (this.Left + this.Width > screenRight)
{
this.Left = screenRight - this.Width;
} }
} }

View file

@ -42,12 +42,6 @@ namespace v2rayN.Views
{ {
txtRemarks.Focus(); 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 缩放因素 // 获取屏幕的 DPI 缩放因素
double dpiFactor = 1; double dpiFactor = 1;
PresentationSource source = PresentationSource.FromVisual(this); PresentationSource source = PresentationSource.FromVisual(this);
@ -56,20 +50,41 @@ namespace v2rayN.Views
dpiFactor = source.CompositionTarget.TransformToDevice.M11; dpiFactor = source.CompositionTarget.TransformToDevice.M11;
} }
// 获取当前屏幕的尺寸
var screen = System.Windows.Forms.Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle);
var screenWidth = screen.WorkingArea.Width / dpiFactor;
var screenHeight = screen.WorkingArea.Height / dpiFactor;
var screenTop = screen.WorkingArea.Top / dpiFactor;
var screenLeft = screen.WorkingArea.Left / dpiFactor;
var screenBottom = screen.WorkingArea.Bottom / dpiFactor;
var screenRight = screen.WorkingArea.Right / dpiFactor;
// 设置窗口尺寸不超过当前屏幕的尺寸 // 设置窗口尺寸不超过当前屏幕的尺寸
if (this.Width > screenWidth / dpiFactor) if (this.Width > screenWidth)
{ {
this.Width = screenWidth / dpiFactor; this.Width = screenWidth;
} }
if (this.Height > screenHeight / dpiFactor) if (this.Height > screenHeight)
{ {
this.Height = screenHeight / dpiFactor; this.Height = screenHeight;
} }
// 设置窗口不要显示在屏幕外面 // 设置窗口不要显示在屏幕外面
if (this.Top < screenTop / dpiFactor) if (this.Top < screenTop)
{ {
this.Top = screenTop / dpiFactor; this.Top = screenTop;
}
if (this.Left < screenLeft)
{
this.Left = screenLeft;
}
if (this.Top + this.Height > screenBottom)
{
this.Top = screenBottom - this.Height;
}
if (this.Left + this.Width > screenRight)
{
this.Left = screenRight - this.Width;
} }
} }

View file

@ -36,12 +36,6 @@ namespace v2rayN.Views
private void Window_Loaded(object sender, RoutedEventArgs e) 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 缩放因素 // 获取屏幕的 DPI 缩放因素
double dpiFactor = 1; double dpiFactor = 1;
PresentationSource source = PresentationSource.FromVisual(this); PresentationSource source = PresentationSource.FromVisual(this);
@ -50,20 +44,41 @@ namespace v2rayN.Views
dpiFactor = source.CompositionTarget.TransformToDevice.M11; dpiFactor = source.CompositionTarget.TransformToDevice.M11;
} }
// 获取当前屏幕的尺寸
var screen = System.Windows.Forms.Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle);
var screenWidth = screen.WorkingArea.Width / dpiFactor;
var screenHeight = screen.WorkingArea.Height / dpiFactor;
var screenTop = screen.WorkingArea.Top / dpiFactor;
var screenLeft = screen.WorkingArea.Left / dpiFactor;
var screenBottom = screen.WorkingArea.Bottom / dpiFactor;
var screenRight = screen.WorkingArea.Right / dpiFactor;
// 设置窗口尺寸不超过当前屏幕的尺寸 // 设置窗口尺寸不超过当前屏幕的尺寸
if (this.Width > screenWidth / dpiFactor) if (this.Width > screenWidth)
{ {
this.Width = screenWidth / dpiFactor; this.Width = screenWidth;
} }
if (this.Height > screenHeight / dpiFactor) if (this.Height > screenHeight)
{ {
this.Height = screenHeight / dpiFactor; this.Height = screenHeight;
} }
// 设置窗口不要显示在屏幕外面 // 设置窗口不要显示在屏幕外面
if (this.Top < screenTop / dpiFactor) if (this.Top < screenTop)
{ {
this.Top = screenTop / dpiFactor; this.Top = screenTop;
}
if (this.Left < screenLeft)
{
this.Left = screenLeft;
}
if (this.Top + this.Height > screenBottom)
{
this.Top = screenBottom - this.Height;
}
if (this.Left + this.Width > screenRight)
{
this.Left = screenRight - this.Width;
} }
} }