Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Eto types for to/from native screen points and fill in comments #2640

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions src/Eto.Wpf/WpfHelpers.cs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading