get dpi unaware screen bounds

This commit is contained in:
seraphima
2024-07-01 19:53:26 +02:00
parent fd5e4590d7
commit 10a3f1162f
2 changed files with 26 additions and 32 deletions

View File

@@ -3,47 +3,40 @@
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
using System; using System;
using System.Collections.Generic; using System.Runtime.InteropServices;
using System.Drawing; using System.Threading;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
namespace ProjectsEditor.Utils namespace ProjectsEditor.Utils
{ {
internal sealed class MonitorHelper public class MonitorHelper
{ {
internal static List<Rectangle> GetDpiUnawareScreenBounds() private const int DpiAwarenessContextUnaware = -1;
private Screen[] screens;
[DllImport("user32.dll")]
private static extern IntPtr SetThreadDpiAwarenessContext(IntPtr dpiContext);
private void SaveDpiUnawareScreens()
{ {
List<Rectangle> screenBounds = new List<Rectangle>(); SetThreadDpiAwarenessContext(DpiAwarenessContextUnaware);
screens = Screen.AllScreens;
var primaryScreen = System.Windows.Forms.Screen.PrimaryScreen;
GetDpiOnScreen(primaryScreen, out uint dpiX_Primary, out uint dpiY_Primary);
foreach (var screen in System.Windows.Forms.Screen.AllScreens)
{
GetDpiOnScreen(screen, out uint dpiX, out uint dpiY);
screenBounds.Add(new Rectangle((int)(screen.Bounds.Left * 96 / dpiX_Primary), (int)(screen.Bounds.Top * 96 / dpiY_Primary), (int)(screen.Bounds.Width * 96 / dpiX), (int)(screen.Bounds.Height * 96 / dpiY)));
}
return screenBounds;
} }
private static void GetDpiOnScreen(Screen screen, out uint dpiX, out uint dpiY) private Screen[] GetDpiUnawareScreenBounds()
{ {
var point = new System.Drawing.Point(screen.Bounds.Left + 1, screen.Bounds.Top + 1); Thread dpiUnawareThread = new Thread(new ThreadStart(SaveDpiUnawareScreens));
var hmonitor = NativeMethods.MonitorFromPoint(point, NativeMethods._MONITOR_DEFAULTTONEAREST); dpiUnawareThread.Start();
dpiUnawareThread.Join();
switch (NativeMethods.GetDpiForMonitor(hmonitor, NativeMethods.DpiType.EFFECTIVE, out dpiX, out dpiY).ToInt32()) return screens;
{ }
case NativeMethods._S_OK: break;
default: public static Screen[] GetDpiUnawareScreens()
dpiX = 96; {
dpiY = 96; MonitorHelper monitorHelper = new MonitorHelper();
break; return monitorHelper.GetDpiUnawareScreenBounds();
}
} }
} }
} }

View File

@@ -398,8 +398,9 @@ namespace ProjectsEditor.ViewModels
{ {
_mainWindow.WindowState = System.Windows.WindowState.Minimized; _mainWindow.WindowState = System.Windows.WindowState.Minimized;
_overlayWindows.Clear(); _overlayWindows.Clear();
foreach (var bounds in MonitorHelper.GetDpiUnawareScreenBounds()) foreach (var screen in MonitorHelper.GetDpiUnawareScreens())
{ {
var bounds = screen.Bounds;
OverlayWindow overlayWindow = new OverlayWindow(); OverlayWindow overlayWindow = new OverlayWindow();
overlayWindow.Top = bounds.Top; overlayWindow.Top = bounds.Top;
overlayWindow.Left = bounds.Left; overlayWindow.Left = bounds.Left;