Skip to content

Commit 9befc9b

Browse files
authored
Merge pull request #458 from TheJoeFin/Dapplo-Screens
Dapplo screens
2 parents 6f95355 + 682dcb1 commit 9befc9b

File tree

6 files changed

+131
-80
lines changed

6 files changed

+131
-80
lines changed

Tests/ScreenLayoutTests.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Windows;
1+
using Dapplo.Windows.User32;
2+
using System.Windows;
23
using Text_Grab;
34

45
namespace Tests;
@@ -92,7 +93,7 @@ public void SmallRectanglesContained()
9293
double smallLeft2 = display2.CenterPoint().X - (sideLength / 2);
9394
double smallTop2 = display2.CenterPoint().Y - (sideLength / 2);
9495
Rect smallRect2 = new(smallLeft2, smallTop2, sideLength, sideLength);
95-
96+
9697
Assert.True(display2.Contains(smallRect2));
9798
Assert.False(display1.Contains(smallRect2));
9899
Assert.False(display3.Contains(smallRect2));
@@ -121,7 +122,7 @@ public void SmallRectanglesContained456()
121122
double smallLeft5 = display5.CenterPoint().X - (sideLength / 2);
122123
double smallTop5 = display5.CenterPoint().Y - (sideLength / 2);
123124
Rect smallRect5 = new(smallLeft5, smallTop5, sideLength, sideLength);
124-
125+
125126
Assert.True(display5.Contains(smallRect5));
126127
Assert.False(display4.Contains(smallRect5));
127128
Assert.False(display6.Contains(smallRect5));
@@ -134,4 +135,26 @@ public void SmallRectanglesContained456()
134135
Assert.False(display4.Contains(smallRect6));
135136
Assert.False(display5.Contains(smallRect6));
136137
}
138+
139+
140+
[Fact]
141+
public void CompareDapploToWinForms()
142+
{
143+
DisplayInfo[] dapploDisplays = Dapplo.Windows.User32.DisplayInfo.AllDisplayInfos;
144+
145+
System.Windows.Forms.Screen[] winFormsDisplays = System.Windows.Forms.Screen.AllScreens;
146+
147+
Assert.Equal(dapploDisplays.Length, winFormsDisplays.Length);
148+
149+
for (int i = 0; i < dapploDisplays.Length; i++)
150+
{
151+
Rect dapploRect = dapploDisplays[i].Bounds;
152+
Rect winFormsRect = winFormsDisplays[i].Bounds.AsRect();
153+
154+
Point dapploCenterPoint = dapploRect.CenterPoint();
155+
Point winFormsCenterPoint = winFormsRect.CenterPoint();
156+
157+
Assert.Equal(dapploCenterPoint, winFormsCenterPoint);
158+
}
159+
}
137160
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Dapplo.Windows.User32;
2+
using System.Windows;
3+
4+
namespace Text_Grab.Extensions;
5+
public static class DapploExtensions
6+
{
7+
public static Point ScaledCenterPoint(this DisplayInfo displayInfo)
8+
{
9+
Rect displayRect = displayInfo.Bounds;
10+
NativeMethods.GetScaleFactorForMonitor(displayInfo.MonitorHandle, out uint scaleFactor);
11+
double scaleFraction = scaleFactor / 100.0;
12+
Point rawCenter = displayRect.CenterPoint();
13+
Point displayScaledCenterPoint = new(rawCenter.X / scaleFraction, rawCenter.Y / scaleFraction);
14+
return displayScaledCenterPoint;
15+
}
16+
17+
public static Rect ScaledBounds(this DisplayInfo displayInfo)
18+
{
19+
Rect displayRect = displayInfo.Bounds;
20+
NativeMethods.GetScaleFactorForMonitor(displayInfo.MonitorHandle, out uint scaleFactor);
21+
double scaleFraction = scaleFactor / 100.0;
22+
23+
// Scale size and position
24+
Rect scaledBounds = new(
25+
displayRect.X / scaleFraction,
26+
displayRect.Y / scaleFraction,
27+
displayRect.Width / scaleFraction,
28+
displayRect.Height / scaleFraction);
29+
return scaledBounds;
30+
}
31+
}

Text-Grab/NativeMethods.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ internal static partial class NativeMethods
1919
[LibraryImport("user32.dll")]
2020
[return: MarshalAs(UnmanagedType.Bool)]
2121
internal static partial bool GetKeyboardState(byte[] keyState);
22+
23+
[LibraryImport("shcore.dll")]
24+
public static partial void GetScaleFactorForMonitor(IntPtr hMon, out uint pScale);
2225
}

Text-Grab/Text-Grab.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@
6868

6969
<ItemGroup>
7070
<PackageReference Include="CliWrap" Version="3.6.6" />
71+
<PackageReference Include="Dapplo.Windows.User32" Version="1.0.28" />
7172
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
7273
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.1.3" />
7374
<PackageReference Include="System.Drawing.Common" Version="9.0.0-preview.1.24081.2" />
7475
<PackageReference Include="WPF-UI" Version="3.0.4" />
75-
<PackageReference Include="WpfScreenHelper" Version="2.1.0" />
7676
<PackageReference Include="ZXing.Net" Version="0.16.9" />
7777
<PackageReference Include="ZXing.Net.Bindings.Windows.Compatibility" Version="0.16.12" />
7878
</ItemGroup>

Text-Grab/Utilities/WindowUtilities.cs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
using System;
1+
using Dapplo.Windows.User32;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Threading.Tasks;
56
using System.Windows;
67
using System.Windows.Controls;
78
using System.Windows.Input;
8-
using Text_Grab.Properties;
9+
using Text_Grab.Extensions;
910
using Text_Grab.Views;
10-
// using Screen = System.Windows.Forms.Screen;
11-
using WpfScreenHelper;
1211
using static OSInterop;
1312

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

5352
if (parsedHei < 10 || parsedWid < 10)
5453
return;
5554

56-
foreach (Screen screen in allScreens)
57-
if (screen.WpfBounds.IntersectsWith(storedSize))
55+
foreach (DisplayInfo screen in allScreens)
56+
{
57+
Rect screenRect = screen.Bounds;
58+
if (screenRect.IntersectsWith(storedSize))
5859
isStoredRectWithinScreen = true;
60+
}
5961

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

7274
public static void LaunchFullScreenGrab(TextBox? destinationTextBox = null)
7375
{
74-
IEnumerable<Screen> allScreens = Screen.AllScreens;
76+
DisplayInfo[] allScreens = DisplayInfo.AllDisplayInfos;
7577
WindowCollection allWindows = Application.Current.Windows;
7678

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

9496
double sideLength = 40;
9597

96-
foreach (Screen screen in allScreens)
98+
foreach (DisplayInfo screen in allScreens)
9799
{
98100
FullscreenGrab fullScreenGrab = allFullscreenGrab[count];
99101
fullScreenGrab.WindowStartupLocation = WindowStartupLocation.Manual;
@@ -102,7 +104,7 @@ public static void LaunchFullScreenGrab(TextBox? destinationTextBox = null)
102104
fullScreenGrab.DestinationTextBox = destinationTextBox;
103105
fullScreenGrab.WindowState = WindowState.Normal;
104106

105-
Point screenCenterPoint = screen.GetCenterPoint();
107+
Point screenCenterPoint = screen.ScaledCenterPoint();
106108

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

117-
public static Point GetCenterPoint(this Screen screen)
119+
public static Point GetCenterPoint(this DisplayInfo screen)
118120
{
119-
double x = screen.WpfBounds.Left + (screen.WpfBounds.Width / 2);
120-
double y = screen.WpfBounds.Top + (screen.WpfBounds.Height / 2);
121+
Rect screenRect = screen.Bounds;
122+
double x = screenRect.Left + (screenRect.Width / 2);
123+
double y = screenRect.Top + (screenRect.Height / 2);
121124
return new(x, y);
122125
}
123126

@@ -151,8 +154,8 @@ internal static async void CloseAllFullscreenGrabs()
151154
{
152155
if (window is FullscreenGrab fsg)
153156
{
154-
if (!string.IsNullOrWhiteSpace(fsg.textFromOCR))
155-
stringFromOCR = fsg.textFromOCR;
157+
if (!string.IsNullOrWhiteSpace(fsg.TextFromOCR))
158+
stringFromOCR = fsg.TextFromOCR;
156159

157160
if (fsg.DestinationTextBox is not null)
158161
{

0 commit comments

Comments
 (0)