Skip to content

Commit

Permalink
Merge pull request #458 from TheJoeFin/Dapplo-Screens
Browse files Browse the repository at this point in the history
Dapplo screens
  • Loading branch information
TheJoeFin committed May 5, 2024
2 parents 6f95355 + 682dcb1 commit 9befc9b
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 80 deletions.
29 changes: 26 additions & 3 deletions Tests/ScreenLayoutTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Windows;
using Dapplo.Windows.User32;
using System.Windows;
using Text_Grab;

namespace Tests;
Expand Down Expand Up @@ -92,7 +93,7 @@ public void SmallRectanglesContained()
double smallLeft2 = display2.CenterPoint().X - (sideLength / 2);
double smallTop2 = display2.CenterPoint().Y - (sideLength / 2);
Rect smallRect2 = new(smallLeft2, smallTop2, sideLength, sideLength);

Assert.True(display2.Contains(smallRect2));
Assert.False(display1.Contains(smallRect2));
Assert.False(display3.Contains(smallRect2));
Expand Down Expand Up @@ -121,7 +122,7 @@ public void SmallRectanglesContained456()
double smallLeft5 = display5.CenterPoint().X - (sideLength / 2);
double smallTop5 = display5.CenterPoint().Y - (sideLength / 2);
Rect smallRect5 = new(smallLeft5, smallTop5, sideLength, sideLength);

Assert.True(display5.Contains(smallRect5));
Assert.False(display4.Contains(smallRect5));
Assert.False(display6.Contains(smallRect5));
Expand All @@ -134,4 +135,26 @@ public void SmallRectanglesContained456()
Assert.False(display4.Contains(smallRect6));
Assert.False(display5.Contains(smallRect6));
}


[Fact]
public void CompareDapploToWinForms()
{
DisplayInfo[] dapploDisplays = Dapplo.Windows.User32.DisplayInfo.AllDisplayInfos;

System.Windows.Forms.Screen[] winFormsDisplays = System.Windows.Forms.Screen.AllScreens;

Assert.Equal(dapploDisplays.Length, winFormsDisplays.Length);

for (int i = 0; i < dapploDisplays.Length; i++)
{
Rect dapploRect = dapploDisplays[i].Bounds;
Rect winFormsRect = winFormsDisplays[i].Bounds.AsRect();

Point dapploCenterPoint = dapploRect.CenterPoint();
Point winFormsCenterPoint = winFormsRect.CenterPoint();

Assert.Equal(dapploCenterPoint, winFormsCenterPoint);
}
}
}
31 changes: 31 additions & 0 deletions Text-Grab/Extensions/DapploExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Dapplo.Windows.User32;
using System.Windows;

namespace Text_Grab.Extensions;
public static class DapploExtensions
{
public static Point ScaledCenterPoint(this DisplayInfo displayInfo)
{
Rect displayRect = displayInfo.Bounds;
NativeMethods.GetScaleFactorForMonitor(displayInfo.MonitorHandle, out uint scaleFactor);
double scaleFraction = scaleFactor / 100.0;
Point rawCenter = displayRect.CenterPoint();
Point displayScaledCenterPoint = new(rawCenter.X / scaleFraction, rawCenter.Y / scaleFraction);
return displayScaledCenterPoint;
}

public static Rect ScaledBounds(this DisplayInfo displayInfo)
{
Rect displayRect = displayInfo.Bounds;
NativeMethods.GetScaleFactorForMonitor(displayInfo.MonitorHandle, out uint scaleFactor);
double scaleFraction = scaleFactor / 100.0;

// Scale size and position
Rect scaledBounds = new(
displayRect.X / scaleFraction,
displayRect.Y / scaleFraction,
displayRect.Width / scaleFraction,
displayRect.Height / scaleFraction);
return scaledBounds;
}
}
3 changes: 3 additions & 0 deletions Text-Grab/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ internal static partial class NativeMethods
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static partial bool GetKeyboardState(byte[] keyState);

[LibraryImport("shcore.dll")]
public static partial void GetScaleFactorForMonitor(IntPtr hMon, out uint pScale);
}
2 changes: 1 addition & 1 deletion Text-Grab/Text-Grab.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@

<ItemGroup>
<PackageReference Include="CliWrap" Version="3.6.6" />
<PackageReference Include="Dapplo.Windows.User32" Version="1.0.28" />
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.1.3" />
<PackageReference Include="System.Drawing.Common" Version="9.0.0-preview.1.24081.2" />
<PackageReference Include="WPF-UI" Version="3.0.4" />
<PackageReference Include="WpfScreenHelper" Version="2.1.0" />
<PackageReference Include="ZXing.Net" Version="0.16.9" />
<PackageReference Include="ZXing.Net.Bindings.Windows.Compatibility" Version="0.16.12" />
</ItemGroup>
Expand Down
33 changes: 18 additions & 15 deletions Text-Grab/Utilities/WindowUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System;
using Dapplo.Windows.User32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Text_Grab.Properties;
using Text_Grab.Extensions;
using Text_Grab.Views;
// using Screen = System.Windows.Forms.Screen;
using WpfScreenHelper;
using static OSInterop;

namespace Text_Grab.Utilities;
Expand Down Expand Up @@ -47,15 +46,18 @@ public static void SetWindowPosition(Window passedWindow)
couldParseAll = double.TryParse(storedPosition[2], out double parsedWid);
couldParseAll = double.TryParse(storedPosition[3], out double parsedHei);
Rect storedSize = new((int)parsedX, (int)parsedY, (int)parsedWid, (int)parsedHei);
IEnumerable<Screen> allScreens = Screen.AllScreens;
DisplayInfo[] allScreens = DisplayInfo.AllDisplayInfos;
WindowCollection allWindows = Application.Current.Windows;

if (parsedHei < 10 || parsedWid < 10)
return;

foreach (Screen screen in allScreens)
if (screen.WpfBounds.IntersectsWith(storedSize))
foreach (DisplayInfo screen in allScreens)
{
Rect screenRect = screen.Bounds;
if (screenRect.IntersectsWith(storedSize))
isStoredRectWithinScreen = true;
}

if (isStoredRectWithinScreen && couldParseAll)
{
Expand All @@ -71,7 +73,7 @@ public static void SetWindowPosition(Window passedWindow)

public static void LaunchFullScreenGrab(TextBox? destinationTextBox = null)
{
IEnumerable<Screen> allScreens = Screen.AllScreens;
DisplayInfo[] allScreens = DisplayInfo.AllDisplayInfos;
WindowCollection allWindows = Application.Current.Windows;

List<FullscreenGrab> allFullscreenGrab = new();
Expand All @@ -93,7 +95,7 @@ public static void LaunchFullScreenGrab(TextBox? destinationTextBox = null)

double sideLength = 40;

foreach (Screen screen in allScreens)
foreach (DisplayInfo screen in allScreens)
{
FullscreenGrab fullScreenGrab = allFullscreenGrab[count];
fullScreenGrab.WindowStartupLocation = WindowStartupLocation.Manual;
Expand All @@ -102,7 +104,7 @@ public static void LaunchFullScreenGrab(TextBox? destinationTextBox = null)
fullScreenGrab.DestinationTextBox = destinationTextBox;
fullScreenGrab.WindowState = WindowState.Normal;

Point screenCenterPoint = screen.GetCenterPoint();
Point screenCenterPoint = screen.ScaledCenterPoint();

fullScreenGrab.Left = screenCenterPoint.X - (sideLength / 2);
fullScreenGrab.Top = screenCenterPoint.Y - (sideLength / 2);
Expand All @@ -114,10 +116,11 @@ public static void LaunchFullScreenGrab(TextBox? destinationTextBox = null)
}
}

public static Point GetCenterPoint(this Screen screen)
public static Point GetCenterPoint(this DisplayInfo screen)
{
double x = screen.WpfBounds.Left + (screen.WpfBounds.Width / 2);
double y = screen.WpfBounds.Top + (screen.WpfBounds.Height / 2);
Rect screenRect = screen.Bounds;
double x = screenRect.Left + (screenRect.Width / 2);
double y = screenRect.Top + (screenRect.Height / 2);
return new(x, y);
}

Expand Down Expand Up @@ -151,8 +154,8 @@ internal static async void CloseAllFullscreenGrabs()
{
if (window is FullscreenGrab fsg)
{
if (!string.IsNullOrWhiteSpace(fsg.textFromOCR))
stringFromOCR = fsg.textFromOCR;
if (!string.IsNullOrWhiteSpace(fsg.TextFromOCR))
stringFromOCR = fsg.TextFromOCR;

if (fsg.DestinationTextBox is not null)
{
Expand Down
Loading

0 comments on commit 9befc9b

Please sign in to comment.