mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 11:48:06 +01:00
get dpi unaware screen bounds
This commit is contained in:
@@ -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 Screen[] GetDpiUnawareScreenBounds()
|
||||||
|
{
|
||||||
|
Thread dpiUnawareThread = new Thread(new ThreadStart(SaveDpiUnawareScreens));
|
||||||
|
dpiUnawareThread.Start();
|
||||||
|
dpiUnawareThread.Join();
|
||||||
|
|
||||||
|
return screens;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void GetDpiOnScreen(Screen screen, out uint dpiX, out uint dpiY)
|
public static Screen[] GetDpiUnawareScreens()
|
||||||
{
|
{
|
||||||
var point = new System.Drawing.Point(screen.Bounds.Left + 1, screen.Bounds.Top + 1);
|
MonitorHelper monitorHelper = new MonitorHelper();
|
||||||
var hmonitor = NativeMethods.MonitorFromPoint(point, NativeMethods._MONITOR_DEFAULTTONEAREST);
|
return monitorHelper.GetDpiUnawareScreenBounds();
|
||||||
|
|
||||||
switch (NativeMethods.GetDpiForMonitor(hmonitor, NativeMethods.DpiType.EFFECTIVE, out dpiX, out dpiY).ToInt32())
|
|
||||||
{
|
|
||||||
case NativeMethods._S_OK: break;
|
|
||||||
default:
|
|
||||||
dpiX = 96;
|
|
||||||
dpiY = 96;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user