2017-11-27 13:11:20 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using Vanara.PInvoke;
|
2019-08-27 18:03:21 -04:00
|
|
|
|
using static Vanara.PInvoke.User32;
|
2017-11-27 13:11:20 -05:00
|
|
|
|
|
|
|
|
|
namespace Vanara.Extensions
|
|
|
|
|
{
|
2020-04-02 23:59:18 -04:00
|
|
|
|
/// <summary>Extension methods for windows to process points in the coordinate space.</summary>
|
2017-11-27 13:11:20 -05:00
|
|
|
|
public static partial class MapPointExtension
|
|
|
|
|
{
|
2020-04-02 23:59:18 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The MapPoint method converts (maps) a point from a coordinate space relative to one window to a coordinate space relative to
|
|
|
|
|
/// another window.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ctrl">The control.</param>
|
|
|
|
|
/// <param name="pt">The pt.</param>
|
|
|
|
|
/// <param name="newWin">The new win.</param>
|
|
|
|
|
/// <returns></returns>
|
2022-01-14 09:32:51 -05:00
|
|
|
|
public static POINT MapPoint(this IWin32Window ctrl, POINT pt, IWin32Window newWin = null)
|
2017-11-27 13:11:20 -05:00
|
|
|
|
{
|
2018-09-22 00:35:34 -04:00
|
|
|
|
MapWindowPoints(GetHandle(ctrl), GetHandle(newWin), ref pt, 1);
|
2017-11-27 13:11:20 -05:00
|
|
|
|
return pt;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-02 23:59:18 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The MapPoint method converts (maps) a set of points from a coordinate space relative to one window to a coordinate space
|
|
|
|
|
/// relative to another window.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ctrl">The control.</param>
|
|
|
|
|
/// <param name="points">The points.</param>
|
|
|
|
|
/// <param name="newWin">The new win.</param>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">points</exception>
|
2022-01-14 09:32:51 -05:00
|
|
|
|
public static void MapPoints(this IWin32Window ctrl, POINT[] points, IWin32Window newWin = null)
|
2017-11-27 13:11:20 -05:00
|
|
|
|
{
|
|
|
|
|
if (points == null) throw new ArgumentNullException(nameof(points));
|
2018-09-22 00:35:34 -04:00
|
|
|
|
MapWindowPoints(GetHandle(ctrl), GetHandle(newWin), points, points.Length);
|
2022-01-14 09:32:51 -05:00
|
|
|
|
/*POINT[] pts = new POINT[points.Length];
|
2017-11-27 13:11:20 -05:00
|
|
|
|
points.CopyTo(pts, 0);
|
|
|
|
|
for (int i = 0; i < pts.Length; i++)
|
|
|
|
|
MapWindowPoints(GetHandleRef(ctrl), GetHandleRef(newWin), ref pts[i], 1);
|
|
|
|
|
return pts;*/
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-02 23:59:18 -04:00
|
|
|
|
/// <summary>The MapPoint method converts (maps) a point from a coordinate space relative to one window to the desktop.</summary>
|
|
|
|
|
/// <param name="ctrl">The control.</param>
|
|
|
|
|
/// <param name="pt">The pt.</param>
|
|
|
|
|
/// <returns></returns>
|
2022-01-14 09:32:51 -05:00
|
|
|
|
public static POINT MapPointToClient(this IWin32Window ctrl, POINT pt) => MapPoint(null, pt, ctrl);
|
2020-04-02 23:59:18 -04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The MapPoint method converts (maps) a rectangle from a coordinate space relative to one window to a coordinate space relative to
|
|
|
|
|
/// another window.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ctrl">The control.</param>
|
|
|
|
|
/// <param name="rectangle">The rectangle.</param>
|
|
|
|
|
/// <param name="newWin">The new win.</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static Rectangle MapRectangle(this IWin32Window ctrl, Rectangle rectangle, IWin32Window newWin = null)
|
|
|
|
|
{
|
|
|
|
|
RECT ir = rectangle;
|
|
|
|
|
MapWindowPoints(GetHandle(ctrl), GetHandle(newWin), ref ir, 2);
|
|
|
|
|
return ir;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-22 00:35:34 -04:00
|
|
|
|
private static HWND GetHandle(IWin32Window ctrl) => new HWND(ctrl?.Handle ?? IntPtr.Zero);
|
2017-11-27 13:11:20 -05:00
|
|
|
|
}
|
2020-04-02 23:59:18 -04:00
|
|
|
|
}
|