tangxu
2024-10-22 4d9fe5ed98ceb6b8fe9dc52ebfb80860ad1aee99
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
using System.Drawing;
using System.Windows.Forms;
 
namespace Microsoft.Drawing
{
    /// <summary>
    /// 矩形帮助类
    /// </summary>
    public static class RectangleEx
    {
        /// <summary>
        /// 调整矩形非空,影响原来矩形
        /// </summary>
        /// <param name="rect">要调整的矩形</param>
        public static void MakeNotEmpty(ref Rectangle rect)
        {
            if (rect.Width < 1)
                rect.Width = 1;
            if (rect.Height < 1)
                rect.Height = 1;
        }
 
        /// <summary>
        /// 获取矩形是否可见
        /// </summary>
        /// <param name="rect">矩形</param>
        /// <returns>宽度大于0并且高度大于0返回true,否则返回false</returns>
        public static bool IsVisible(Rectangle rect)
        {
            return rect.Width > 0 && rect.Height > 0;
        }
 
        /// <summary>
        /// 在指定的方向放大矩形,不影响原来的矩形
        /// </summary>
        /// <param name="rect">矩形</param>
        /// <param name="align">方向</param>
        /// <param name="value">放大量</param>
        /// <returns>放大后的矩形</returns>
        public static Rectangle Inflate(Rectangle rect, TabAlignment align, int value)
        {
            switch (align)
            {
                case TabAlignment.Top:
                    rect.Y -= value;
                    rect.Height += value;
                    break;
 
                case TabAlignment.Bottom:
                    rect.Height += value;
                    break;
 
                case TabAlignment.Left:
                    rect.X -= value;
                    rect.Width += value;
                    break;
 
                case TabAlignment.Right:
                    rect.Width += value;
                    break;
 
                default:
                    break;
            }
 
            return rect;
        }
 
        /// <summary>
        /// 在指定的方向和反向放大矩形,不影响原来的矩形
        /// </summary>
        /// <param name="rect">矩形</param>
        /// <param name="align">方向</param>
        /// <param name="value">放大量</param>
        /// <param name="revalue">反向放大量</param>
        /// <returns>放大后矩形</returns>
        public static Rectangle Inflate(Rectangle rect, TabAlignment align, int value, int revalue)
        {
            switch (align)
            {
                case TabAlignment.Top:
                    rect.Y -= value;
                    rect.Height += value + revalue;
                    break;
 
                case TabAlignment.Bottom:
                    rect.Y -= revalue;
                    rect.Height += revalue + value;
                    break;
 
                case TabAlignment.Left:
                    rect.X -= value;
                    rect.Width += value + revalue;
                    break;
 
                case TabAlignment.Right:
                    rect.X -= revalue;
                    rect.Width += revalue + value;
                    break;
 
                default:
                    break;
            }
 
            return rect;
        }
 
        /// <summary>
        /// 在指定的方向的两侧放大矩形,不影响原来的矩形
        /// </summary>
        /// <param name="rect">矩形</param>
        /// <param name="align">方向</param>
        /// <param name="value">放大量</param>
        /// <returns>放大后矩形</returns>
        public static Rectangle InflateSide(Rectangle rect, TabAlignment align, int value)
        {
            int half = value / 2;
            switch (align)
            {
                case TabAlignment.Top:
                case TabAlignment.Bottom:
                    rect.X -= half;
                    rect.Width += value;
                    break;
 
                case TabAlignment.Left:
                case TabAlignment.Right:
                    rect.Y -= half;
                    rect.Height += value;
                    break;
 
                default:
                    break;
            }
 
            return rect;
        }
 
        /// <summary>
        /// 按指定边调整矩形,使在该方向上的大小为指定值
        /// </summary>
        /// <param name="rect">矩形</param>
        /// <param name="align">方向</param>
        /// <param name="value">大小</param>
        /// <returns>调整后矩形</returns>
        public static Rectangle Adjust(Rectangle rect, TabAlignment align, int value)
        {
            switch (align)
            {
                case TabAlignment.Top:
                    rect.Y += (rect.Height - value);
                    rect.Height = value;
                    break;
 
                case TabAlignment.Bottom:
                    rect.Height = value;
                    break;
 
                case TabAlignment.Left:
                    rect.X += (rect.Width - value);
                    rect.Width = value;
                    break;
 
                case TabAlignment.Right:
                    rect.Width = value;
                    break;
 
                default:
                    break;
            }
 
            return rect;
        }
 
        /// <summary>
        /// 调整矩形使其在指定方向上与标准矩形对齐,并加上偏移量,不影响原来的矩形
        /// </summary>
        /// <param name="rect">矩形</param>
        /// <param name="rectStand">标准矩形</param>
        /// <param name="align">方向</param>
        /// <param name="offset">偏移量</param>
        /// <returns>调整后的矩形</returns>
        public static Rectangle Align(Rectangle rect, Rectangle rectStand, TabAlignment align, int offset)
        {
            int value;
            switch (align)
            {
                case TabAlignment.Top:
                    value = rect.Y - rectStand.Y + offset;
                    rect.Y -= value;
                    rect.Height += value;
                    break;
 
                case TabAlignment.Bottom:
                    value = rectStand.Bottom - rect.Bottom + offset;
                    rect.Height += value;
                    break;
 
                case TabAlignment.Left:
                    value = rect.X - rectStand.X + offset;
                    rect.X -= value;
                    rect.Width += value;
                    break;
 
                case TabAlignment.Right:
                    value = rectStand.Right - rect.Right + offset;
                    rect.Width += value;
                    break;
 
                default:
                    break;
            }
 
            return rect;
        }
 
        /// <summary>
        /// 矩形加上Padding后的新矩形
        /// </summary>
        /// <param name="rect">矩形</param>
        /// <param name="padding">外边距</param>
        /// <returns>新矩形</returns>
        public static Rectangle Add(Rectangle rect, Padding padding)
        {
            return new Rectangle(rect.Left - padding.Left, rect.Top - padding.Top, rect.Width + padding.Horizontal, rect.Height + padding.Vertical);
        }
 
        /// <summary>
        /// 矩形减去Padding后的新矩形
        /// </summary>
        /// <param name="rect">矩形</param>
        /// <param name="padding">内边距</param>
        /// <returns>新矩形</returns>
        public static Rectangle Subtract(Rectangle rect, Padding padding)
        {
            return new Rectangle(rect.Left + padding.Left, rect.Top + padding.Top, rect.Width - padding.Horizontal, rect.Height - padding.Vertical);
        }
 
        /// <summary>
        /// 将此矩形的位置调整指定的量。返回新矩形,不影响原来值。
        /// </summary>
        /// <param name="rect">矩形</param>
        /// <param name="pos">该位置的偏移量</param>
        /// <returns>偏移后的新矩形</returns>
        public static Rectangle Offset(Rectangle rect, Point pos)
        {
            rect.Offset(pos);
            return rect;
        }
 
        /// <summary>
        /// 将矩形的位置调整指定的量。返回新矩形,不影响原来值。
        /// </summary>
        /// <param name="rect">矩形</param>
        /// <param name="x">水平偏移量</param>
        /// <param name="y">垂直偏移量</param>
        /// <returns>偏移后的新矩形</returns>
        public static Rectangle Offset(Rectangle rect, int x, int y)
        {
            rect.Offset(x, y);
            return rect;
        }
    }
}