From 5ea3d2d90b9435d4b3549d27c183943b19f404ff Mon Sep 17 00:00:00 2001 From: YukkuriC <799433638@qq.com> Date: Mon, 4 Sep 2023 22:12:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A2=E6=B5=8B=E5=88=B0=E6=A1=8C=E5=AE=A0?= =?UTF-8?q?=E5=9C=A8=E4=B8=BB=E5=B1=8F=E6=97=B6=E5=9B=BA=E5=AE=9A=E7=94=A8?= =?UTF-8?q?=E8=80=81=E5=85=AC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Function/MWController.cs | 59 ++++++++++++++----- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/VPet-Simulator.Windows/Function/MWController.cs b/VPet-Simulator.Windows/Function/MWController.cs index e7b6dd6..4e0baea 100644 --- a/VPet-Simulator.Windows/Function/MWController.cs +++ b/VPet-Simulator.Windows/Function/MWController.cs @@ -16,43 +16,74 @@ namespace VPet_Simulator.Windows this.mw = mw; } - private Rectangle? _screenBorder = null; - private Rectangle ScreenBorder + private bool _screenDetected = false; + private Rectangle _screenBorder; + private bool _isPrimaryScreen = true; + public bool IsPrimaryScreen { get { - if (_screenBorder == null) - { - var windowInteropHelper = new WindowInteropHelper(mw); - var currentScreen = Screen.FromHandle(windowInteropHelper.Handle); - _screenBorder = currentScreen.Bounds; - } - return (Rectangle)_screenBorder; + if(!_screenDetected) DetectCurrentScreen(); + return _isPrimaryScreen; } } + public Rectangle ScreenBorder + { + get + { + if (!_screenDetected) DetectCurrentScreen(); + return _screenBorder; + } + } + private void DetectCurrentScreen() + { + var windowInteropHelper = new WindowInteropHelper(mw); + var currentScreen = Screen.FromHandle(windowInteropHelper.Handle); + // TODO 多屏+非100%缩放下以上API跟Left、Top一样都是错的 + + _isPrimaryScreen = currentScreen.Primary; + _screenBorder = currentScreen.Bounds; + _screenDetected = true; + } public void ClearScreenBorderCache() { - _screenBorder = null; + _screenDetected = false; } public double GetWindowsDistanceLeft() { - return mw.Dispatcher.Invoke(() => mw.Left - ScreenBorder.X); + return mw.Dispatcher.Invoke(() => + { + if (IsPrimaryScreen) return mw.Left; + return mw.Left - ScreenBorder.X; + }); } public double GetWindowsDistanceUp() { - return mw.Dispatcher.Invoke(() => mw.Top - ScreenBorder.Y); + return mw.Dispatcher.Invoke(() => + { + if (IsPrimaryScreen) return mw.Top; + return mw.Top - ScreenBorder.Y; + }); } public double GetWindowsDistanceRight() { - return mw.Dispatcher.Invoke(() => ScreenBorder.Width + ScreenBorder.X - mw.Left - mw.Width); + return mw.Dispatcher.Invoke(() => + { + if (IsPrimaryScreen) return System.Windows.SystemParameters.PrimaryScreenWidth - mw.Left - mw.Width; + return ScreenBorder.Width + ScreenBorder.X - mw.Left - mw.Width; + }); } public double GetWindowsDistanceDown() { - return mw.Dispatcher.Invoke(() => ScreenBorder.Height + ScreenBorder.Y - mw.Top - mw.Height); + return mw.Dispatcher.Invoke(() => + { + if (IsPrimaryScreen) return System.Windows.SystemParameters.PrimaryScreenHeight - mw.Top - mw.Height; + return ScreenBorder.Height + ScreenBorder.Y - mw.Top - mw.Height; + }); } public void MoveWindows(double X, double Y)