Skip to content

Commit

Permalink
Merge pull request #2640 from cwensley/curtis/wpf-add-logical-to-nati…
Browse files Browse the repository at this point in the history
…ve-screen-conversion

Use Eto types for to/from native screen points and fill in comments
  • Loading branch information
cwensley committed Apr 4, 2024
2 parents 210024b + 12ea2d4 commit ae78e85
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
31 changes: 25 additions & 6 deletions src/Eto.Wpf/WpfHelpers.cs
Expand Up @@ -85,25 +85,44 @@ public static Window ToEtoWindow(IntPtr windowHandle)
}

/// <summary>
/// Converts a System.Drawing.Point in screen coordinates to an Eto point in screen coordinates.
/// Converts a point in native (Windows Forms/Win32) screen coordinates to a point in Eto logical screen coordinates.
/// </summary>
/// <param name="point">A point in Windows Forms/win32 screen coordinates.</param>
/// <param name="screen">The screen to translate to, if known.</param>
/// <param name="perMonitor">True to get screen bounds/location in per monitor mode, false to remain in current mode.</param>
/// <returns>A point in Eto screen coordinates.</returns>
public static PointF ToEtoScreen(this sd.Point point, swf.Screen sdscreen = null, bool perMonitor = false) => Win32.ScreenToLogical(point, sdscreen, perMonitor);
public static PointF ToEtoScreen(this Point point, Screen screen = null, bool perMonitor = false) => Win32.ScreenToLogical(point, ScreenHandler.GetControl(screen), perMonitor);

public static RectangleF ToEtoScreen(this sd.Rectangle rect, swf.Screen sdscreen = null, bool perMonitor = false)
/// <summary>
/// Converts a rectangle in native (Windows Forms/Win32) screen coordinates to an rectangle in Eto logical screen coordinates.
/// </summary>
/// <param name="rect">A rectangle in Windows Forms/win32 screen coordinates.</param>
/// <param name="screen">The screen to translate to, if known.</param>
/// <param name="perMonitor">True to get screen bounds/location in per monitor mode, false to remain in current mode.</param>
/// <returns>A point in Eto screen coordinates.</returns>
public static RectangleF ToEtoScreen(this Rectangle rect, Screen screen = null, bool perMonitor = false)
{
var topLeft = ToEtoScreen(rect.Location, sdscreen, perMonitor);
var bottomRight = ToEtoScreen(new sd.Point(rect.X + rect.Width, rect.Y + rect.Height), sdscreen, perMonitor);
var topLeft = ToEtoScreen(rect.Location, screen, perMonitor);
var bottomRight = ToEtoScreen(new Point(rect.X + rect.Width, rect.Y + rect.Height), screen, perMonitor);
return new RectangleF(topLeft, new SizeF(bottomRight.X - topLeft.X, bottomRight.Y - topLeft.Y));
}

/// <summary>
/// Converts a point in Eto screen coordinates to a point in Windows Forms/win32 screen coordinates.
/// Converts a point in Eto logical screen coordinates to a point in native (Windows Forms/win32) screen coordinates.
/// </summary>
/// <param name="point">A point in Eto screen coordinates.</param>
/// <param name="screen">The screen to translate to, if known.</param>
/// <param name="perMonitor">True to get screen bounds/location in per monitor mode, false to remain in current mode.</param>
/// <returns>A point in Windows Forms/win32 screen coordinates.</returns>
public static Point ToNativeScreen(this PointF point, Screen screen = null, bool perMonitor = false) => Win32.LogicalToScreen(point, screen, perMonitor);

/// <summary>
/// Converts a rectangle in Eto logical screen coordinates to a rectangle in native (Windows Forms/win32) screen coordinates.
/// </summary>
/// <param name="rectangle">A rectangle in Eto screen coordinates.</param>
/// <param name="screen">The screen to translate to, if known.</param>
/// <param name="perMonitor">True to get screen bounds/location in per monitor mode, false to remain in current mode.</param>
/// <returns>A rectangle in Windows Forms/win32 screen coordinates.</returns>
public static Rectangle ToNativeScreen(this RectangleF rect, Screen screen = null, bool perMonitor = false)
{
var topLeft = rect.TopLeft.ToNativeScreen(screen, perMonitor);
Expand Down
7 changes: 4 additions & 3 deletions test/Eto.Test.Wpf/UnitTests/ScreenToClientTests.cs
Expand Up @@ -6,6 +6,7 @@
using NUnit.Framework;
using swf = System.Windows.Forms;
using sd = System.Drawing;
using Eto.Wpf.Forms;

namespace Eto.Test.Wpf.UnitTests
{
Expand Down Expand Up @@ -53,7 +54,7 @@ void CreateWindow(Rectangle rect)
window.Show();
windows.Add(window);
bool perMonitor = true;
bool perMonitor = false;
var sdrect = WpfHelpers.ToNativeScreen(rect, window.Screen, perMonitor: perMonitor).ToSD();
Expand All @@ -70,12 +71,12 @@ void CreateWindow(Rectangle rect)
wfwindow.LocationChanged += (sender, e) =>
{
var loc = wfwindow.RectangleToScreen(wfwindow.ClientRectangle);
window.Bounds = (Rectangle)WpfHelpers.ToEtoScreen(loc, swf.Screen.FromControl(wfwindow), perMonitor: perMonitor);
window.Bounds = (Rectangle)WpfHelpers.ToEtoScreen(loc.ToEto(), window.Screen, perMonitor: perMonitor);
};
wfwindow.SizeChanged += (sender, e) =>
{
var loc = wfwindow.RectangleToScreen(wfwindow.ClientRectangle);
window.Bounds = (Rectangle)WpfHelpers.ToEtoScreen(loc, swf.Screen.FromControl(wfwindow), perMonitor: perMonitor);
window.Bounds = (Rectangle)WpfHelpers.ToEtoScreen(loc.ToEto(), window.Screen, perMonitor: perMonitor);
};
wfwindow.Show();
wfwindows.Add(wfwindow);
Expand Down

0 comments on commit ae78e85

Please sign in to comment.