Skip to content

Commit

Permalink
feat: 实现核心功能
Browse files Browse the repository at this point in the history
  • Loading branch information
ZGGSONG committed Jul 30, 2024
1 parent 8968429 commit 04518bb
Show file tree
Hide file tree
Showing 17 changed files with 798 additions and 996 deletions.
6 changes: 3 additions & 3 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Dapplo.Windows.User32" Version="1.0.28" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageVersion Include="xunit" Version="2.8.0" />
<PackageVersion Include="Dapplo.Windows.User32" Version="1.0.28"/>
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0"/>
<PackageVersion Include="xunit" Version="2.8.0"/>
</ItemGroup>
</Project>
10 changes: 5 additions & 5 deletions src/ScreenGrab/Extensions/DapploExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ 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();
NativeMethods.GetScaleFactorForMonitor(displayInfo.MonitorHandle, out var scaleFactor);
var scaleFraction = scaleFactor / 100.0;
var 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;
NativeMethods.GetScaleFactorForMonitor(displayInfo.MonitorHandle, out var scaleFactor);
var scaleFraction = scaleFactor / 100.0;

// Scale size and position
Rect scaledBounds = new(
Expand Down
43 changes: 22 additions & 21 deletions src/ScreenGrab/Extensions/ShapeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Drawing;
using System.Windows;
using Point = System.Windows.Point;

namespace ScreenGrab.Extensions;

Expand All @@ -18,43 +19,43 @@ public static Rectangle AsRectangle(this Rect rect)
public static Rect GetScaledDownByDpi(this Rect rect, DpiScale dpi)
{
return new Rect(rect.X / dpi.DpiScaleX,
rect.Y / dpi.DpiScaleY,
rect.Width / dpi.DpiScaleX,
rect.Height / dpi.DpiScaleY);
rect.Y / dpi.DpiScaleY,
rect.Width / dpi.DpiScaleX,
rect.Height / dpi.DpiScaleY);
}

public static Rect GetScaledUpByDpi(this Rect rect, DpiScale dpi)
{
return new Rect(rect.X * dpi.DpiScaleX,
rect.Y * dpi.DpiScaleY,
rect.Width * dpi.DpiScaleX,
rect.Height * dpi.DpiScaleY);
rect.Y * dpi.DpiScaleY,
rect.Width * dpi.DpiScaleX,
rect.Height * dpi.DpiScaleY);
}

public static Rect GetScaledUpByFraction(this Rect rect, Double scaleFactor)
public static Rect GetScaledUpByFraction(this Rect rect, double scaleFactor)
{
return new Rect(rect.X * scaleFactor,
rect.Y * scaleFactor,
rect.Width * scaleFactor,
rect.Height * scaleFactor);
rect.Y * scaleFactor,
rect.Width * scaleFactor,
rect.Height * scaleFactor);
}

public static Rect GetScaleSizeByFraction(this Rect rect, Double scaleFactor)
public static Rect GetScaleSizeByFraction(this Rect rect, double scaleFactor)
{
return new Rect(rect.X,
rect.Y,
rect.Width * scaleFactor,
rect.Height * scaleFactor);
rect.Y,
rect.Width * scaleFactor,
rect.Height * scaleFactor);
}

public static bool IsGood(this Rect rect)
{
if (double.IsNaN(rect.X)
if (double.IsNaN(rect.X)
|| double.IsNegativeInfinity(rect.X)
|| double.IsPositiveInfinity(rect.X))
return false;
if (double.IsNaN(rect.Y)

if (double.IsNaN(rect.Y)
|| double.IsNegativeInfinity(rect.Y)
|| double.IsPositiveInfinity(rect.Y))
return false;
Expand All @@ -74,10 +75,10 @@ public static bool IsGood(this Rect rect)
return true;
}

public static System.Windows.Point CenterPoint(this Rect rect)
public static Point CenterPoint(this Rect rect)
{
double x = rect.Left + (rect.Width / 2);
double y = rect.Top + (rect.Height / 2);
return new(x, y);
var x = rect.Left + rect.Width / 2;
var y = rect.Top + rect.Height / 2;
return new Point(x, y);
}
}
14 changes: 7 additions & 7 deletions src/ScreenGrab/Extensions/WpfExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Runtime.InteropServices;

namespace ScreenGrab.Extensions;

Expand All @@ -13,21 +12,22 @@ public static Point GetAbsolutePosition(this Window w)
return new Point(w.Left, w.Top);

Int32Rect r;
bool multimonSupported = OSInterop.GetSystemMetrics(OSInterop.SM_CMONITORS) != 0;
var multimonSupported = OSInterop.GetSystemMetrics(OSInterop.SM_CMONITORS) != 0;
if (!multimonSupported)
{
OSInterop.RECT rc = new OSInterop.RECT();
var rc = new OSInterop.RECT();
OSInterop.SystemParametersInfo(48, 0, ref rc, 0);
r = new Int32Rect(rc.left, rc.top, rc.width, rc.height);
}
else
{
WindowInteropHelper helper = new WindowInteropHelper(w);
IntPtr hmonitor = OSInterop.MonitorFromWindow(new HandleRef(null, helper.EnsureHandle()), 2);
OSInterop.MONITORINFOEX info = new OSInterop.MONITORINFOEX();
var helper = new WindowInteropHelper(w);
var hmonitor = OSInterop.MonitorFromWindow(new HandleRef(null, helper.EnsureHandle()), 2);
var info = new OSInterop.MONITORINFOEX();
OSInterop.GetMonitorInfo(new HandleRef(null, hmonitor), info);
r = new Int32Rect(info.rcMonitor.left, info.rcMonitor.top, info.rcMonitor.width, info.rcMonitor.height);
}

return new Point(r.X, r.Y);
}
}
39 changes: 39 additions & 0 deletions src/ScreenGrab/Grab.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Drawing;
using System.Windows;
using Dapplo.Windows.User32;
using ScreenGrab.Extensions;

namespace ScreenGrab;

public class Grab
{
public Action<Bitmap>? OnImageCaptured { get; set; }

public void Capture()
{
var allDisplayInfos = DisplayInfo.AllDisplayInfos;
var allScreenGrab = Application.Current.Windows.OfType<ScreenGrabView>().ToList();
var numberOfScreenGrabWindowsToCreate = allDisplayInfos.Length - allScreenGrab.Count;

for (var i = 0; i < numberOfScreenGrabWindowsToCreate; i++)
allScreenGrab.Add(new ScreenGrabView(OnImageCaptured));

const double sideLength = 40;

foreach (var (displayInfo, screenGrab) in allDisplayInfos.Zip(allScreenGrab,
(displayInfo, screenGrab) => (displayInfo, screenGrab)))
{
screenGrab.WindowStartupLocation = WindowStartupLocation.Manual;
screenGrab.Width = sideLength;
screenGrab.Height = sideLength;
screenGrab.WindowState = WindowState.Normal;

var screenCenterPoint = displayInfo.ScaledCenterPoint();
screenGrab.Left = screenCenterPoint.X - sideLength / 2;
screenGrab.Top = screenCenterPoint.Y - sideLength / 2;

screenGrab.Show();
screenGrab.Activate();
}
}
}
Loading

0 comments on commit 04518bb

Please sign in to comment.