Adding helper class for getting the right bounds for screens

This commit is contained in:
donlaci
2024-06-28 17:58:28 +02:00
parent 9733c1035b
commit ed423d8264
3 changed files with 72 additions and 6 deletions

View File

@@ -0,0 +1,49 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ProjectsEditor.Utils
{
internal sealed class MonitorHelper
{
internal static List<Rectangle> GetDpiUnawareScreenBounds()
{
List<Rectangle> screenBounds = new List<Rectangle>();
var primaryScreen = System.Windows.Forms.Screen.PrimaryScreen;
GetDpiOnScreen(primaryScreen, out uint dpiXPrimary, out uint dpiYPrimary);
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 / dpiXPrimary), (int)(screen.Bounds.Top * 96 / dpiYPrimary), (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)
{
var point = new System.Drawing.Point(screen.Bounds.Left + 1, screen.Bounds.Top + 1);
var hmonitor = NativeMethods.MonitorFromPoint(point, NativeMethods._MONITOR_DEFAULTTONEAREST);
switch (NativeMethods.GetDpiForMonitor(hmonitor, NativeMethods.DpiType.EFFECTIVE, out dpiX, out dpiY).ToInt32())
{
case NativeMethods._S_OK: break;
default:
dpiX = 96;
dpiY = 96;
break;
}
}
}
}

View File

@@ -37,5 +37,22 @@ namespace ProjectsEditor.Utils
[DllImport("user32.dll")]
public static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
public enum DpiType
{
EFFECTIVE = 0,
ANGULAR = 1,
RAW = 2,
}
[DllImport("User32.dll")]
public static extern IntPtr MonitorFromPoint([In] System.Drawing.Point pt, [In] uint dwFlags);
[DllImport("Shcore.dll")]
public static extern IntPtr GetDpiForMonitor([In] IntPtr hmonitor, [In] DpiType dpiType, [Out] out uint dpiX, [Out] out uint dpiY);
public const int _S_OK = 0;
public const int _MONITOR_DEFAULTTONEAREST = 2;
public const int _E_INVALIDARG = -2147024809;
}
}

View File

@@ -10,6 +10,7 @@ using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Timers;
using ManagedCommon;
@@ -397,14 +398,13 @@ namespace ProjectsEditor.ViewModels
{
_mainWindow.WindowState = System.Windows.WindowState.Minimized;
_overlayWindows.Clear();
var screens = System.Windows.Forms.Screen.AllScreens;
foreach (var screen in screens)
foreach (var bounds in MonitorHelper.GetDpiUnawareScreenBounds())
{
OverlayWindow overlayWindow = new OverlayWindow();
overlayWindow.Top = screen.Bounds.Top;
overlayWindow.Left = screen.Bounds.Left;
overlayWindow.Width = screen.Bounds.Width;
overlayWindow.Height = screen.Bounds.Height;
overlayWindow.Top = bounds.Top;
overlayWindow.Left = bounds.Left;
overlayWindow.Width = bounds.Width;
overlayWindow.Height = bounds.Height;
overlayWindow.ShowActivated = true;
overlayWindow.Topmost = true;
overlayWindow.Show();