yangyin
2025-01-13 36616eb44dc36a4e6e3e7a7540310cb850218ea9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// THIS FILE IS PART OF Vanara PROJECT
// THE Vanara PROJECT IS AN OPENSOURCE LIBRARY LICENSED UNDER THE MIT License.
// COPYRIGHT (C) dahall. ALL RIGHTS RESERVED.
// GITHUB: https://github.com/dahall/Vanara
 
using System;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Runtime.InteropServices;
 
namespace Vanara.PInvoke
{
    /// <summary>Defines the coordinates of the upper-left and lower-right corners of a rectangle.</summary>
    /// <remarks>
    /// By convention, the right and bottom edges of the rectangle are normally considered exclusive. In other words, the pixel whose
    /// coordinates are ( right, bottom ) lies immediately outside of the rectangle. For example, when RECT is passed to the FillRect
    /// function, the rectangle is filled up to, but not including, the right column and bottom row of pixels. This structure is identical to
    /// the RECT structure.
    /// </remarks>
    [StructLayout(LayoutKind.Sequential)]
    public struct RECT : IEquatable<PRECT>, IEquatable<RECT>, IEquatable<Rectangle>
    {
        /// <summary>The x-coordinate of the upper-left corner of the rectangle.</summary>
        public int left;
 
        /// <summary>The y-coordinate of the upper-left corner of the rectangle.</summary>
        public int top;
 
        /// <summary>he x-coordinate of the lower-right corner of the rectangle.</summary>
        public int right;
 
        /// <summary>he y-coordinate of the lower-right corner of the rectangle.</summary>
        public int bottom;
 
        /// <summary>Initializes a new instance of the <see cref="RECT"/> struct.</summary>
        /// <param name="left">The left.</param>
        /// <param name="top">The top.</param>
        /// <param name="right">The right.</param>
        /// <param name="bottom">The bottom.</param>
        public RECT(int left, int top, int right, int bottom)
        {
            this.left = left;
            this.top = top;
            this.right = right;
            this.bottom = bottom;
        }
 
        /// <summary>Initializes a new instance of the <see cref="RECT"/> struct.</summary>
        /// <param name="r">The rectangle.</param>
        public RECT(Rectangle r) : this(r.Left, r.Top, r.Right, r.Bottom)
        {
        }
 
        /// <summary>Gets or sets the x-coordinate of the upper-left corner of this <see cref="RECT"/> structure.</summary>
        /// <value>The x-coordinate of the upper-left corner of this <see cref="RECT"/> structure. The default is 0.</value>
        public int X
        {
            get => left;
            set
            {
                right -= (left - value);
                left = value;
            }
        }
 
        /// <summary>Gets or sets the y-coordinate of the upper-left corner of this <see cref="RECT"/> structure.</summary>
        /// <value>The y-coordinate of the upper-left corner of this <see cref="RECT"/> structure. The default is 0.</value>
        public int Y
        {
            get => top;
            set
            {
                bottom -= (top - value);
                top = value;
            }
        }
 
        /// <summary>Gets or sets the height of this <see cref="RECT"/> structure.</summary>
        /// <value>The height of this <see cref="RECT"/> structure. The default is 0.</value>
        public int Height
        {
            get => bottom - top;
            set => bottom = value + top;
        }
 
        /// <summary>Gets or sets the width of this <see cref="RECT"/> structure.</summary>
        /// <value>The width of this <see cref="RECT"/> structure. The default is 0.</value>
        public int Width
        {
            get => right - left;
            set => right = value + left;
        }
 
        /// <summary>Gets or sets the coordinates of the upper-left corner of this <see cref="RECT"/> structure.</summary>
        /// <value>A Point that represents the upper-left corner of this <see cref="RECT"/> structure.</value>
        public Point Location
        {
            get => new Point(left, top);
            set
            {
                X = value.X;
                Y = value.Y;
            }
        }
 
        /// <summary>Gets or sets the size of this <see cref="RECT"/> structure.</summary>
        /// <value>A Size that represents the width and height of this <see cref="RECT"/> structure.</value>
        public Size Size
        {
            get => new Size(Width, Height);
            set
            {
                Width = value.Width;
                Height = value.Height;
            }
        }
 
        /// <summary>Tests whether all numeric properties of this <see cref="RECT"/> have values of zero.</summary>
        /// <value><c>true</c> if this instance is empty; otherwise, <c>false</c>.</value>
        public bool IsEmpty => left == 0 && top == 0 && right == 0 && bottom == 0;
 
        /// <summary>Performs an implicit conversion from <see cref="RECT"/> to <see cref="Rectangle"/>.</summary>
        /// <param name="r">The <see cref="RECT"/> structure.</param>
        /// <returns>The result of the conversion.</returns>
        public static implicit operator Rectangle(RECT r) => new Rectangle(r.left, r.top, r.Width, r.Height);
 
        /// <summary>Performs an implicit conversion from <see cref="Rectangle"/> to <see cref="RECT"/>.</summary>
        /// <param name="r">The Rectangle structure.</param>
        /// <returns>The result of the conversion.</returns>
        public static implicit operator RECT(Rectangle r) => new RECT(r);
 
        /// <summary>Tests whether two <see cref="RECT"/> structures have equal values.</summary>
        /// <param name="r1">The r1.</param>
        /// <param name="r2">The r2.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(RECT r1, RECT r2) => r1.Equals(r2);
 
        /// <summary>Tests whether two <see cref="RECT"/> structures have different values.</summary>
        /// <param name="r1">The first <see cref="RECT"/> structure.</param>
        /// <param name="r2">The second <see cref="RECT"/> structure.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(RECT r1, RECT r2) => !r1.Equals(r2);
 
        /// <summary>Determines whether the specified <see cref="RECT"/>, is equal to this instance.</summary>
        /// <param name="r">The <see cref="RECT"/> to compare with this instance.</param>
        /// <returns><c>true</c> if the specified <see cref="RECT"/> is equal to this instance; otherwise, <c>false</c>.</returns>
        public bool Equals(RECT r) => r.left == left && r.top == top && r.right == right && r.bottom == bottom;
 
        /// <summary>Determines whether the specified <see cref="PRECT"/>, is equal to this instance.</summary>
        /// <param name="r">The <see cref="PRECT"/> to compare with this instance.</param>
        /// <returns><c>true</c> if the specified <see cref="PRECT"/> is equal to this instance; otherwise, <c>false</c>.</returns>
        public bool Equals(PRECT? r) => Equals(r?.rect);
 
        /// <summary>Determines whether the specified <see cref="Rectangle"/>, is equal to this instance.</summary>
        /// <param name="r">The <see cref="Rectangle"/> to compare with this instance.</param>
        /// <returns><c>true</c> if the specified <see cref="Rectangle"/> is equal to this instance; otherwise, <c>false</c>.</returns>
        public bool Equals(Rectangle r) => r.Left == left && r.Top == top && r.Right == right && r.Bottom == bottom;
 
        /// <summary>Determines whether the specified <see cref="object"/>, is equal to this instance.</summary>
        /// <param name="obj">The <see cref="object"/> to compare with this instance.</param>
        /// <returns><c>true</c> if the specified <see cref="object"/> is equal to this instance; otherwise, <c>false</c>.</returns>
        public override bool Equals(object obj)
        {
            switch (obj)
            {
                case null:
                    return false;
 
                case RECT r:
                    return Equals(r);
 
                case PRECT r:
                    return Equals(r);
 
                case Rectangle r:
                    return Equals(r);
 
                default:
                    return false;
            }
        }
 
        /// <summary>Returns a hash code for this instance.</summary>
        /// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
        public override int GetHashCode() => ((Rectangle)this).GetHashCode();
 
        /// <summary>Returns a <see cref="string"/> that represents this instance.</summary>
        /// <returns>A <see cref="string"/> that represents this instance.</returns>
        public override string ToString() => $"{{left={left},top={top},right={right},bottom={bottom}}}";
 
        /// <summary>Represents an empty instance where all values are set to 0.</summary>
        public static readonly RECT Empty = new RECT();
    }
 
    /// <summary>Defines the coordinates of the upper-left and lower-right corners of a rectangle.</summary>
    /// <remarks>
    /// By convention, the right and bottom edges of the rectangle are normally considered exclusive. In other words, the pixel whose
    /// coordinates are ( right, bottom ) lies immediately outside of the rectangle. For example, when RECT is passed to the FillRect
    /// function, the rectangle is filled up to, but not including, the right column and bottom row of pixels. This structure is identical to
    /// the RECT structure.
    /// </remarks>
    [StructLayout(LayoutKind.Sequential)]
    public class PRECT : IEquatable<PRECT>, IEquatable<RECT>, IEquatable<Rectangle>
    {
        internal RECT rect;
 
        /// <summary>Initializes a new instance of the <see cref="PRECT"/> class with all values set to 0.</summary>
        public PRECT()
        {
        }
 
        /// <summary>Initializes a new instance of the <see cref="PRECT"/> class.</summary>
        /// <param name="left">The left.</param>
        /// <param name="top">The top.</param>
        /// <param name="right">The right.</param>
        /// <param name="bottom">The bottom.</param>
        public PRECT(int left, int top, int right, int bottom) => rect = new RECT(left, top, right, bottom);
 
        /// <summary>Initializes a new instance of the <see cref="PRECT"/> class.</summary>
        /// <param name="r">The <see cref="Rectangle"/> structure.</param>
        public PRECT(Rectangle r) => rect = new RECT(r);
 
        /// <summary>Initializes a new instance of the <see cref="PRECT"/> class.</summary>
        /// <param name="r">The r.</param>
        [ExcludeFromCodeCoverage]
        private PRECT(RECT r) => rect = r;
 
        /// <summary>The x-coordinate of the upper-left corner of the rectangle.</summary>
        public int left
        {
            get => rect.left;
            set => rect.left = value;
        }
 
        /// <summary>The y-coordinate of the upper-left corner of the rectangle.</summary>
        public int top
        {
            get => rect.top;
            set => rect.top = value;
        }
 
        /// <summary>he x-coordinate of the lower-right corner of the rectangle.</summary>
        public int right
        {
            get => rect.right;
            set => rect.right = value;
        }
 
        /// <summary>he y-coordinate of the lower-right corner of the rectangle.</summary>
        public int bottom
        {
            get => rect.bottom;
            set => rect.bottom = value;
        }
 
        /// <summary>Gets or sets the x-coordinate of the upper-left corner of this <see cref="PRECT"/> structure.</summary>
        /// <value>The x-coordinate of the upper-left corner of this <see cref="PRECT"/> structure. The default is 0.</value>
        public int X
        {
            get => rect.X;
            set => rect.X = value;
        }
 
        /// <summary>Gets or sets the y-coordinate of the upper-left corner of this <see cref="PRECT"/> structure.</summary>
        /// <value>The y-coordinate of the upper-left corner of this <see cref="PRECT"/> structure. The default is 0.</value>
        public int Y
        {
            get => rect.Y;
            set => rect.Y = value;
        }
 
        /// <summary>Gets or sets the height of this <see cref="PRECT"/> structure.</summary>
        /// <value>The height of this <see cref="PRECT"/> structure. The default is 0.</value>
        public int Height
        {
            get => rect.Height;
            set => rect.Height = value;
        }
 
        /// <summary>Gets or sets the width of this <see cref="PRECT"/> structure.</summary>
        /// <value>The width of this <see cref="PRECT"/> structure. The default is 0.</value>
        public int Width
        {
            get => rect.Width;
            set => rect.Width = value;
        }
 
        /// <summary>Gets or sets the coordinates of the upper-left corner of this <see cref="PRECT"/> structure.</summary>
        /// <value>A Point that represents the upper-left corner of this <see cref="PRECT"/> structure.</value>
        public Point Location
        {
            get => rect.Location;
            set => rect.Location = value;
        }
 
        /// <summary>Gets or sets the size of this <see cref="PRECT"/> structure.</summary>
        /// <value>A Size that represents the width and height of this <see cref="PRECT"/> structure.</value>
        public Size Size
        {
            get => rect.Size;
            set => rect.Size = value;
        }
 
        /// <summary>Tests whether all numeric properties of this <see cref="RECT"/> have values of zero.</summary>
        /// <value><c>true</c> if this instance is empty; otherwise, <c>false</c>.</value>
        public bool IsEmpty => rect.IsEmpty;
 
        /// <summary>Performs an implicit conversion from <see cref="PRECT"/> to <see cref="Rectangle"/>.</summary>
        /// <param name="r">The r.</param>
        /// <returns>The result of the conversion.</returns>
        public static implicit operator Rectangle(PRECT r) => r.rect;
 
        /// <summary>Performs an implicit conversion from <see cref="System.Nullable{Rectangle}"/> to <see cref="PRECT"/>.</summary>
        /// <param name="r">The r.</param>
        /// <returns>The result of the conversion.</returns>
        public static implicit operator PRECT(Rectangle? r) => r.HasValue ? new PRECT(r.Value) : null;
 
        /// <summary>Performs an implicit conversion from <see cref="Rectangle"/> to <see cref="PRECT"/>.</summary>
        /// <param name="r">The r.</param>
        /// <returns>The result of the conversion.</returns>
        public static implicit operator PRECT(Rectangle r) => new PRECT(r);
 
        /// <summary>Performs an implicit conversion from <see cref="RECT"/> to <see cref="PRECT"/>.</summary>
        /// <param name="r">The r.</param>
        /// <returns>The result of the conversion.</returns>
        public static implicit operator PRECT(RECT r) => new PRECT(r);
 
        /// <summary>Implements the operator ==.</summary>
        /// <param name="r1">The r1.</param>
        /// <param name="r2">The r2.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(PRECT r1, PRECT r2)
        {
            if (ReferenceEquals(r1, r2))
                return true;
            if ((object)r1 == null || (object)r2 == null)
                return false;
            return r1.Equals(r2);
        }
 
        /// <summary>Implements the operator !=.</summary>
        /// <param name="r1">The r1.</param>
        /// <param name="r2">The r2.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(PRECT r1, PRECT r2) => !(r1 == r2);
 
        /// <summary>Determines whether the specified <see cref="PRECT"/>, is equal to this instance.</summary>
        /// <param name="r">The <see cref="PRECT"/> to compare with this instance.</param>
        /// <returns><c>true</c> if the specified <see cref="PRECT"/> is equal to this instance; otherwise, <c>false</c>.</returns>
        public bool Equals(PRECT? r) => rect == r?.rect;
 
        /// <summary>Determines whether the specified <see cref="RECT"/>, is equal to this instance.</summary>
        /// <param name="r">The <see cref="RECT"/> to compare with this instance.</param>
        /// <returns><c>true</c> if the specified <see cref="RECT"/> is equal to this instance; otherwise, <c>false</c>.</returns>
        public bool Equals(RECT r) => rect.Equals(r);
 
        /// <summary>Determines whether the specified <see cref="Rectangle"/>, is equal to this instance.</summary>
        /// <param name="r">The <see cref="Rectangle"/> to compare with this instance.</param>
        /// <returns><c>true</c> if the specified <see cref="Rectangle"/> is equal to this instance; otherwise, <c>false</c>.</returns>
        public bool Equals(Rectangle r) => rect.Equals(r);
 
        /// <summary>Determines whether the specified <see cref="object"/>, is equal to this instance.</summary>
        /// <param name="obj">The <see cref="object"/> to compare with this instance.</param>
        /// <returns><c>true</c> if the specified <see cref="object"/> is equal to this instance; otherwise, <c>false</c>.</returns>
        public override bool Equals(object obj) => rect.Equals(obj);
 
        /// <summary>Returns a hash code for this instance.</summary>
        /// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
        public override int GetHashCode() => rect.GetHashCode();
 
        /// <summary>Returns a <see cref="string"/> that represents this instance.</summary>
        /// <returns>A <see cref="string"/> that represents this instance.</returns>
        public override string ToString() => rect.ToString();
    }
}