33using System . Linq ;
44using System . Text ;
55using System . Threading . Tasks ;
6+ using Microsoft . UI . Content ;
67using Windows . Foundation ;
78using Windows . Graphics ;
89using Windows . Win32 ;
@@ -21,9 +22,19 @@ private class RectRegion : Region
2122 {
2223 private readonly Rect _rect ;
2324 public RectRegion ( Rect rect ) { _rect = rect ; }
24- internal override HRGN Create ( double scalefactor )
25- => PInvoke . CreateRectRgn ( ( int ) ( _rect . Left * scalefactor ) , ( int ) ( _rect . Top * scalefactor ) ,
26- ( int ) ( _rect . Right * scalefactor ) , ( int ) ( _rect . Bottom * scalefactor ) ) ;
25+ internal override HRGN Create ( ContentCoordinateConverter converter , PointInt32 screenLoc , double scaleFactor )
26+ {
27+ var p1 = Convert ( _rect . Left , _rect . Top , converter , screenLoc , scaleFactor ) ;
28+ var p2 = Convert ( _rect . Right , _rect . Bottom , converter , screenLoc , scaleFactor ) ;
29+ return PInvoke . CreateRectRgn ( p1 . X , p1 . Y , p2 . X , p2 . Y ) ;
30+ }
31+ }
32+ private class RectInt32Region : Region
33+ {
34+ private readonly RectInt32 _rect ;
35+ public RectInt32Region ( RectInt32 rect ) { _rect = rect ; }
36+ internal override HRGN Create ( ContentCoordinateConverter converter , PointInt32 screenLoc , double scaleFactor )
37+ => PInvoke . CreateRectRgn ( _rect . X , _rect . Y , _rect . X + _rect . Width , _rect . Y + _rect . Height ) ;
2738 }
2839
2940 private class RoundRectRegion : Region
@@ -37,9 +48,70 @@ public RoundRectRegion(Rect rect, double w, double h)
3748 _w = w ;
3849 _h = h ;
3950 }
40- internal override HRGN Create ( double scalefactor )
41- => PInvoke . CreateRoundRectRgn ( ( int ) ( _rect . Left * scalefactor ) , ( int ) ( _rect . Top * scalefactor ) ,
42- ( int ) ( _rect . Right * scalefactor ) , ( int ) ( _rect . Bottom * scalefactor ) , ( int ) ( _w * scalefactor ) , ( int ) ( _h * scalefactor ) ) ;
51+ internal override HRGN Create ( ContentCoordinateConverter converter , PointInt32 screenLoc , double scaleFactor )
52+ {
53+ var p1 = Convert ( _rect . Left , _rect . Top , converter , screenLoc , scaleFactor ) ;
54+ var p2 = Convert ( _rect . Right , _rect . Bottom , converter , screenLoc , scaleFactor ) ;
55+ return PInvoke . CreateRoundRectRgn ( p1 . X , p1 . Y , p2 . X , p2 . Y , ( int ) ( _w * scaleFactor ) , ( int ) ( _h * scaleFactor ) ) ;
56+ }
57+ }
58+
59+ private class EllipticRegion : Region
60+ {
61+ private readonly double _x1 ;
62+ private readonly double _y1 ;
63+ private readonly double _x2 ;
64+ private readonly double _y2 ;
65+ public EllipticRegion ( double x1 , double y1 , double x2 , double y2 )
66+ {
67+ _x1 = x1 ;
68+ _y1 = y1 ;
69+ _x2 = x2 ;
70+ _y2 = y2 ;
71+ }
72+ internal override HRGN Create ( ContentCoordinateConverter converter , PointInt32 screenLoc , double scaleFactor )
73+ {
74+ var p1 = Convert ( _x1 , _y1 , converter , screenLoc , scaleFactor ) ;
75+ var p2 = Convert ( _x2 , _y2 , converter , screenLoc , scaleFactor ) ;
76+ return PInvoke . CreateEllipticRgn ( p1 . X , p1 . Y , p2 . X , p2 . Y ) ;
77+ }
78+ }
79+
80+ private class PolygonRegion : Region
81+ {
82+ private readonly IEnumerable < Point > _points ;
83+ private readonly CREATE_POLYGON_RGN_MODE _mode ;
84+ public PolygonRegion ( IEnumerable < Point > points , CREATE_POLYGON_RGN_MODE mode )
85+ {
86+ _points = points ;
87+ _mode = mode ;
88+ }
89+ internal unsafe override HRGN Create ( ContentCoordinateConverter converter , PointInt32 screenLoc , double scaleFactor )
90+ {
91+ var pint32 = converter . ConvertLocalToScreen ( _points . Select ( p => new Point ( p . X * scaleFactor , p . Y * scaleFactor ) ) . ToArray ( ) ) ;
92+ var array = pint32 . Select ( p => new System . Drawing . Point ( p . X - screenLoc . X , p . Y - screenLoc . Y ) ) . ToArray ( ) ;
93+ fixed ( System . Drawing . Point * pArray = array )
94+ return PInvoke . CreatePolygonRgn ( pArray , array . Length , _mode ) ;
95+ }
96+
97+ }
98+
99+ private class CurrentRegion : Region
100+ {
101+ private nint _WindowHandle ;
102+ public CurrentRegion ( nint windowHandle )
103+ {
104+ _WindowHandle = windowHandle ;
105+ }
106+ internal override HRGN Create ( ContentCoordinateConverter converter , PointInt32 screenLoc , double scaleFactor )
107+ {
108+ var dest = PInvoke . CreateRectRgn ( 0 , 0 , 0 , 0 ) ;
109+ //PInvoke.GetWindowRgnBox(new Windows.Win32.Foundation.HWND(_WindowHandle), dest);
110+ var type = PInvoke . GetWindowRgnBox ( new Windows . Win32 . Foundation . HWND ( _WindowHandle ) , out var rect ) ;
111+ return PInvoke . CreateRectRgn ( rect . left , rect . top , rect . right , rect . bottom ) ;
112+ // PInvoke.InvertRgn(dest, null);
113+ // return dest;
114+ }
43115 }
44116
45117 private class CombinedRegion : Region
@@ -54,10 +126,10 @@ public CombinedRegion(Region region1, Region region2, CombineMode mode)
54126 _mode = mode ;
55127 }
56128
57- internal override HRGN Create ( double scaleFactor )
129+ internal override HRGN Create ( ContentCoordinateConverter converter , PointInt32 screenLoc , double scaleFactor )
58130 {
59- var rgn1 = _region1 . Create ( scaleFactor ) ;
60- var rgn2 = _region2 . Create ( scaleFactor ) ;
131+ var rgn1 = _region1 . Create ( converter , screenLoc , scaleFactor ) ;
132+ var rgn2 = _region2 . Create ( converter , screenLoc , scaleFactor ) ;
61133
62134 var dest = PInvoke . CreateRectRgn ( 0 , 0 , 0 , 0 ) ;
63135 PInvoke . CombineRgn ( dest , rgn1 , rgn2 , ( RGN_COMBINE_MODE ) _mode ) ;
@@ -68,11 +140,18 @@ internal override HRGN Create(double scaleFactor)
68140 }
69141
70142 /// <summary>
71- /// Creates a rectangular region.
143+ /// Creates a rectangular region in device independent units .
72144 /// </summary>
73145 /// <param name="rect">Rectangular region.</param>
74146 /// <returns></returns>
75- public static Region Create ( Rect rect ) => new RectRegion ( rect ) ;
147+ public static Region CreateRectangle ( Rect rect ) => new RectRegion ( rect ) ;
148+
149+ /// <summary>
150+ /// Creates a rectangular region in screen units.
151+ /// </summary>
152+ /// <param name="rect">Rectangular region.</param>
153+ /// <returns></returns>
154+ public static Region CreateRectangle ( RectInt32 rect ) => new RectInt32Region ( rect ) ;
76155
77156 /// <summary>
78157 /// Creates a rectangular region with rounded corners.
@@ -81,13 +160,48 @@ internal override HRGN Create(double scaleFactor)
81160 /// <param name="w">Specifies the width of the ellipse used to create the rounded corners.</param>
82161 /// <param name="h">Specifies the height of the ellipse used to create the rounded corners.</param>
83162 /// <returns></returns>
84- public static Region Create ( Rect rect , double w , double h ) => new RoundRectRegion ( rect , w , h ) ;
163+ public static Region CreateRoundedRectangle ( Rect rect , double w , double h ) => new RoundRectRegion ( rect , w , h ) ;
164+
165+ /// <summary>
166+ /// Creates an elliptical region.
167+ /// </summary>
168+ /// <param name="x1">Specifies the x-coordinate in device independent units, of the upper-left corner of the bounding rectangle of the ellipse.</param>
169+ /// <param name="y1">Specifies the yx-coordinate in device independent units, of the upper-left corner of the bounding rectangle of the ellipse.</param>
170+ /// <param name="x2">Specifies the x-coordinate in device independent units, of the lower-right corner of the bounding rectangle of the ellipse.</param>
171+ /// <param name="y2">Specifies the y-coordinate in device independent units, of the lower-right corner of the bounding rectangle of the ellipse.</param>
172+ /// <returns></returns>
173+ public static Region CreateElliptic ( double x1 , double y1 , double x2 , double y2 ) => new EllipticRegion ( x1 , y1 , x2 , y2 ) ;
174+
175+ /// <summary>
176+ /// Creates a polygonal region using alternate mode (fills area between odd-numbered and even-numbered polygon sides on each scan line).
177+ /// </summary>
178+ /// <param name="points">The vertices of the polygon in device independent units. The polygon is presumed closed. Each vertex can be specified only once.</param>
179+ /// <returns></returns>
180+ public static Region CreatePolygon ( IEnumerable < Point > points ) => new PolygonRegion ( points , CREATE_POLYGON_RGN_MODE . ALTERNATE ) ;
181+
182+ /*
183+ /// <summary>
184+ /// Creates a region that represents the current visible region of a window.
185+ /// </summary>
186+ /// <param name="window">The window to obtain the region from</param>
187+ /// <returns></returns>
188+ public static Region GetWindowRegion(Microsoft.UI.Xaml.Window window) => new CurrentRegion(window.GetWindowHandle());*/
85189
86190 private Region ( )
87191 {
88192 }
89193
90- internal abstract HRGN Create ( double scalefactor ) ;
194+ internal abstract HRGN Create ( ContentCoordinateConverter converter , PointInt32 screenLoc , double scaleFactor ) ;
195+
196+
197+ private protected static PointInt32 Convert ( double x , double y , ContentCoordinateConverter converter , PointInt32 screenLoc , double scaleFactor ) => Convert ( new Point ( x , y ) , converter , screenLoc , scaleFactor ) ;
198+
199+ private protected static PointInt32 Convert ( Point local , ContentCoordinateConverter converter , PointInt32 screenLoc , double scaleFactor )
200+ {
201+ local = new Point ( local . X * scaleFactor , local . Y * scaleFactor ) ;
202+ var p = converter . ConvertLocalToScreen ( local ) ;
203+ return new PointInt32 ( p . X - screenLoc . X , p . Y - screenLoc . Y ) ;
204+ }
91205
92206 /// <summary>
93207 /// The Combine function combines two regions. The two regions are combined according to the specified mode.
0 commit comments