-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
NativeRect.cs
334 lines (295 loc) · 10.7 KB
/
NativeRect.cs
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
#region Copyright (C) 2016-2018 Dapplo
// Dapplo - building blocks for desktop applications
// Copyright (C) 2016-2018 Dapplo
//
// For more information see: http://dapplo.net/
// Dapplo repositories are hosted on GitHub: https://github.com/dapplo
//
// This file is part of Dapplo.Windows
//
// Dapplo.Windows is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Dapplo.Windows is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have a copy of the GNU Lesser General Public License
// along with Dapplo.Windows. If not, see <http://www.gnu.org/licenses/lgpl.txt>.
#endregion
#region using
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Drawing;
using System.Runtime.InteropServices;
#if !NETSTANDARD2_0
using System.Windows;
#endif
using Dapplo.Windows.Common.TypeConverters;
#endregion
namespace Dapplo.Windows.Common.Structs
{
/// <summary>
/// NativeRect represents the native RECT structure for calling native methods.
/// See <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/dd162897.aspx">RECT struct</a>
/// It has conversions from and to System.Drawing.Rectangle or System.Windows.Rect
/// </summary>
[StructLayout(LayoutKind.Sequential)]
[Serializable]
[TypeConverter(typeof(NativeRectTypeConverter))]
[SuppressMessage("ReSharper", "ConvertToAutoPropertyWithPrivateSetter")]
public readonly struct NativeRect : IEquatable<NativeRect>
{
private readonly int _left;
private readonly int _top;
private readonly int _right;
private readonly int _bottom;
/// <summary>
/// Constructor from left, right, top, bottom
/// </summary>
/// <param name="left">int</param>
/// <param name="top">int</param>
/// <param name="width">int</param>
/// <param name="height">int</param>
public NativeRect(int left, int top, int width, int height)
{
_left = left;
_top = top;
_right = left + width;
_bottom = top + height;
}
/// <summary>
/// Constructor from location and size
/// </summary>
/// <param name="location">NativePoint</param>
/// <param name="size">NativeSize</param>
public NativeRect(NativePoint location, NativeSize size)
{
_left = location.X;
_top = location.Y;
_right = _left + size.Width;
_bottom = _top + size.Height;
}
/// <summary>
/// Constructor from topLeft and bottomRight
/// </summary>
/// <param name="topLeft">NativePoint</param>
/// <param name="bottomRight">NativePoint</param>
public NativeRect(NativePoint topLeft, NativePoint bottomRight) : this(topLeft.X, topLeft.Y, bottomRight.X - topLeft.X, bottomRight.Y - topLeft.Y)
{
}
/// <summary>
/// Constructor from left, right and size
/// </summary>
/// <param name="left">int</param>
/// <param name="top">int</param>
/// <param name="size">NativeSize</param>
public NativeRect(int left, int top, NativeSize size) : this(new NativePoint(left, top), size)
{
}
/// <summary>
/// X value
/// </summary>
public int X => _left;
/// <summary>
/// X location of the NativeRect
/// </summary>
public int Y => _top;
/// <summary>
/// Left value of the NativeRect
/// </summary>
public int Left => _left;
/// <summary>
/// Top of the NativeRect
/// </summary>
public int Top => _top;
/// <summary>
/// Right of the NativeRect
/// </summary>
public int Right => _right;
/// <summary>
/// Bottom of the NativeRect
/// </summary>
public int Bottom => _bottom;
/// <summary>
/// Height of the NativeRect
/// </summary>
public int Height => unchecked(_bottom - _top);
/// <summary>
/// Width of the NativeRect
/// </summary>
public int Width => unchecked(_right - _left);
/// <summary>
/// Location of this NativeRect
/// </summary>
public NativePoint Location => new NativePoint(Left, Top);
/// <summary>
/// Size for this NativeRect
/// </summary>
public NativeSize Size => new NativeSize(Width, Height);
/// <summary>
/// Coordinates of the bottom left
/// </summary>
public NativePoint BottomLeft => new NativePoint(X, Y + Height);
/// <summary>
/// Coordinates of the top left
/// </summary>
public NativePoint TopLeft => new NativePoint(X, Y);
/// <summary>
/// Coordinates of the bottom right
/// </summary>
public NativePoint BottomRight => new NativePoint(X + Width, Y + Height);
/// <summary>
/// Coordinates of the top right
/// </summary>
public NativePoint TopRight => new NativePoint(X + Width, Y);
#if !NETSTANDARD2_0
/// <summary>
/// Cast NativeRect to Rect
/// </summary>
/// <param name="rectangle">NativeRect</param>
/// <returns>Rect</returns>
public static implicit operator Rect(NativeRect rectangle)
{
return new Rect(rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height);
}
/// <summary>
/// Cast NativeRect to Int32Rect
/// </summary>
/// <param name="rectangle">NativeRect</param>
/// <returns>Int32Rect</returns>
public static implicit operator Int32Rect(NativeRect rectangle)
{
return new Int32Rect(rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height);
}
/// <summary>
/// Cast Int32Rect to NativeRect
/// </summary>
/// <param name="rectangle">Int32Rect</param>
/// <returns>NativeRect</returns>
public static implicit operator NativeRect(Int32Rect rectangle)
{
return new NativeRect(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
}
#endif
/// <summary>
/// Cast NativeRect to RectangleF
/// </summary>
/// <param name="rectangle">NativeRect</param>
/// <returns>RectangleF</returns>
public static implicit operator RectangleF(NativeRect rectangle)
{
return new RectangleF(rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height);
}
/// <summary>
/// Cast NativeRect to Rectangle
/// </summary>
/// <param name="rectangle">NativeRect</param>
/// <returns>Rectangle</returns>
public static implicit operator Rectangle(NativeRect rectangle)
{
return new Rectangle(rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height);
}
/// <summary>
/// Cast Rectangle to NativeRect
/// </summary>
/// <param name="rectangle">Rectangle</param>
/// <returns>NativeRect</returns>
public static implicit operator NativeRect(Rectangle rectangle)
{
return new NativeRect(rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height);
}
/// <summary>
/// Equals for NativeRect
/// </summary>
/// <param name="rectangle1">NativeRect</param>
/// <param name="rectangle2">NativeRect</param>
/// <returns>bool true if they are equal</returns>
public static bool operator ==(NativeRect rectangle1, NativeRect rectangle2)
{
return rectangle1.Equals(rectangle2);
}
/// <summary>
/// Not is operator
/// </summary>
/// <param name="rectangle1"></param>
/// <param name="rectangle2"></param>
/// <returns>bool</returns>
public static bool operator !=(NativeRect rectangle1, NativeRect rectangle2)
{
return !rectangle1.Equals(rectangle2);
}
/// <inheritdoc />
[Pure]
public override string ToString()
{
return $"{{Left: {_left}; Top: {_top}; Right: {_right}; Bottom: {_bottom};}}";
}
/// <summary>
/// Equals
/// </summary>
/// <param name="other">NativeRect</param>
/// <returns>bool</returns>
[Pure]
public bool Equals(NativeRect other)
{
return other.Left == _left && other.Top == _top && other.Right == _right && other.Bottom == _bottom;
}
/// <summary>
/// Checks if this NativeRect is empty
/// </summary>
/// <returns>true when empty</returns>
public bool IsEmpty => unchecked (Width * Height) == 0;
/// <inheritdoc />
[Pure]
public override bool Equals(object obj)
{
switch (obj)
{
case NativeRect nativeRect:
return Equals(nativeRect);
case Rectangle rectangle:
NativeRect rect = rectangle;
return Equals(rect);
}
return false;
}
/// <inheritdoc />
[Pure]
public override int GetHashCode()
{
unchecked
{
var hashCode = _left;
hashCode = (hashCode * 397) ^ _top;
hashCode = (hashCode * 397) ^ _right;
hashCode = (hashCode * 397) ^ _bottom;
return hashCode;
}
}
/// <summary>
/// Decontructor for tuples
/// </summary>
/// <param name="location">NativePoint</param>
/// <param name="size">NativeSize</param>
[Pure]
public void Deconstruct(out NativePoint location, out NativeSize size)
{
location = Location;
size = Size;
}
/// <summary>
/// Empty NativeRect
/// </summary>
public static NativeRect Empty { get; } = new NativeRect();
/// <summary>
/// SizeOf for this struct
/// </summary>
public static int SizeOf => Marshal.SizeOf(typeof(NativeRect));
}
}