tangxu
2024-10-22 6a07c4c846ffbb1e93afdf0260e123e4c145f419
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
using System;
using System.Drawing;
using System.Windows.Forms;
 
namespace Microsoft.Windows.Forms.Layout
{
    /// <summary>
    /// 布局工具类
    /// </summary>
    public static class LayoutUtils
    {
        /// <summary>
        /// <para>0*****1*****2</para>
        /// <para>*           *</para>
        /// <para>*           *</para>
        /// <para>4     5     6</para>
        /// <para>*           *</para>
        /// <para>*           *</para>
        /// <para>8*****9*****10</para>
        /// <para>获取对齐方式的索引,如上图</para>
        /// </summary>
        /// <param name="alignment">对齐方式</param>
        /// <returns>索引</returns>
        public static int ContentAlignmentToIndex(ContentAlignment alignment)
        {
            int num = xContentAlignmentToIndex(((int)alignment) & 15);//取0-3位,如果等于4返回3,其余不变
            int num2 = xContentAlignmentToIndex((((int)alignment) >> 4) & 15);//4-7,如果等于4返回3,其余不变
            int num3 = xContentAlignmentToIndex((((int)alignment) >> 8) & 15);//8-11,如果等于4返回3,其余不变
            int num4 = (((((num2 != 0) ? 4 : 0) | ((num3 != 0) ? 8 : 0)) | num) | num2) | num3;
            num4--;
            return num4;
        }
        /// <summary>
        /// 上面方法的辅助
        /// </summary>
        /// <param name="threeBitFlag">4bit数字</param>
        /// <returns>如果等于4返回3,其余不变</returns>
        private static byte xContentAlignmentToIndex(int threeBitFlag)
        {
            return ((threeBitFlag == 4) ? ((byte)3) : ((byte)threeBitFlag));
        }
 
 
        /// <summary>
        /// 获取反向的文本图片相对位置
        /// </summary>
        /// <param name="relation">文本图片相对位置</param>
        /// <returns>反向的文本图片相对位置</returns>
        public static TextImageRelation GetOppositeTextImageRelation(TextImageRelation relation)
        {
            return (TextImageRelation)GetOppositeAnchor((AnchorStyles)relation);
        }
        /// <summary>
        /// 获取反向的锚定方式
        /// </summary>
        /// <param name="anchor">锚定方式</param>
        /// <returns>反向的锚定方式</returns>
        private static AnchorStyles GetOppositeAnchor(AnchorStyles anchor)
        {
            AnchorStyles none = AnchorStyles.None;
            if (anchor != AnchorStyles.None)
            {
                for (int i = 1; i <= 8; i = i << 1)
                {
                    switch ((anchor & (AnchorStyles)i))
                    {
                        case AnchorStyles.Top:
                            none |= AnchorStyles.Bottom;
                            break;
 
                        case AnchorStyles.Bottom:
                            none |= AnchorStyles.Top;
                            break;
 
                        case AnchorStyles.Left:
                            none |= AnchorStyles.Right;
                            break;
 
                        case AnchorStyles.Right:
                            none |= AnchorStyles.Left;
                            break;
                    }
                }
            }
            return none;
        }
 
 
        /// <summary>
        /// 在矩形指定位置划出指定大小的区域
        /// </summary>
        /// <param name="size">大小</param>
        /// <param name="rect">矩形</param>
        /// <param name="align">对齐方式</param>
        /// <returns>划定区域</returns>
        public static Rectangle Align(Size size, Rectangle rect, ContentAlignment align)
        {
            return VAlign(size, HAlign(size, rect, align), align);
        }
        /// <summary>
        /// 调整X坐标和宽度,使宽度为size的宽度,在水平方向上移动对齐到矩形
        /// </summary>
        /// <param name="size">大小</param>
        /// <param name="rect">矩形</param>
        /// <param name="align">水平对齐方式</param>
        /// <returns>新矩形</returns>
        public static Rectangle HAlign(Size size, Rectangle rect, ContentAlignment align)
        {
            if ((align & (ContentAlignment.BottomRight | ContentAlignment.MiddleRight | ContentAlignment.TopRight)) != ((ContentAlignment)0))
            {
                rect.X += rect.Width - size.Width;
            }
            else if ((align & (ContentAlignment.BottomCenter | ContentAlignment.MiddleCenter | ContentAlignment.TopCenter)) != ((ContentAlignment)0))
            {
                rect.X += (rect.Width - size.Width) / 2;
            }
            rect.Width = size.Width;
            return rect;
        }
        /// <summary>
        /// 调整Y坐标和高度,使高度为size的高度,在垂直方向上移动对齐到矩形
        /// </summary>
        /// <param name="size">大小</param>
        /// <param name="rect">矩形</param>
        /// <param name="align">垂直对齐方式</param>
        /// <returns>新矩形</returns>
        public static Rectangle VAlign(Size size, Rectangle rect, ContentAlignment align)
        {
            if ((align & (ContentAlignment.BottomRight | ContentAlignment.BottomCenter | ContentAlignment.BottomLeft)) != ((ContentAlignment)0))
            {
                rect.Y += rect.Height - size.Height;
            }
            else if ((align & (ContentAlignment.MiddleRight | ContentAlignment.MiddleCenter | ContentAlignment.MiddleLeft)) != ((ContentAlignment)0))
            {
                rect.Y += (rect.Height - size.Height) / 2;
            }
            rect.Height = size.Height;
            return rect;
        }
        /// <summary>
        /// 在矩形指定位置划出指定大小的区域
        /// </summary>
        /// <param name="size">大小</param>
        /// <param name="rect">矩形</param>
        /// <param name="align">对齐方式</param>
        /// <returns>划定区域</returns>
        public static RectangleF Align(SizeF size, RectangleF rect, ContentAlignment align)
        {
            return VAlign(size, HAlign(size, rect, align), align);
        }
        /// <summary>
        /// 调整X坐标和宽度,使宽度为size的宽度,在水平方向上移动对齐到矩形
        /// </summary>
        /// <param name="size">大小</param>
        /// <param name="rect">矩形</param>
        /// <param name="align">水平对齐方式</param>
        /// <returns>新矩形</returns>
        public static RectangleF HAlign(SizeF size, RectangleF rect, ContentAlignment align)
        {
            if ((align & (ContentAlignment.BottomRight | ContentAlignment.MiddleRight | ContentAlignment.TopRight)) != ((ContentAlignment)0))
            {
                rect.X += rect.Width - size.Width;
            }
            else if ((align & (ContentAlignment.BottomCenter | ContentAlignment.MiddleCenter | ContentAlignment.TopCenter)) != ((ContentAlignment)0))
            {
                rect.X += (rect.Width - size.Width) / 2f;
            }
            rect.Width = size.Width;
            return rect;
        }
        /// <summary>
        /// 调整Y坐标和高度,使高度为size的高度,在垂直方向上移动对齐到矩形
        /// </summary>
        /// <param name="size">大小</param>
        /// <param name="rect">矩形</param>
        /// <param name="align">垂直对齐方式</param>
        /// <returns>新矩形</returns>
        public static RectangleF VAlign(SizeF size, RectangleF rect, ContentAlignment align)
        {
            if ((align & (ContentAlignment.BottomRight | ContentAlignment.BottomCenter | ContentAlignment.BottomLeft)) != ((ContentAlignment)0))
            {
                rect.Y += rect.Height - size.Height;
            }
            else if ((align & (ContentAlignment.MiddleRight | ContentAlignment.MiddleCenter | ContentAlignment.MiddleLeft)) != ((ContentAlignment)0))
            {
                rect.Y += (rect.Height - size.Height) / 2f;
            }
            rect.Height = size.Height;
            return rect;
        }
 
        /// <summary>
        /// 调整X坐标和宽度,使宽度为size的宽度,在水平方向上移动对齐到矩形
        /// </summary>
        /// <param name="size">大小</param>
        /// <param name="rect">矩形</param>
        /// <param name="align">水平对齐方式</param>
        /// <returns>新矩形</returns>
        public static Rectangle HAlign(Size size, Rectangle rect, HorizontalAlignment align)
        {
            if ((align & HorizontalAlignment.Right) != 0)
            {
                rect.X += rect.Width - size.Width;
            }
            else if ((align & HorizontalAlignment.Center) != 0)
            {
                rect.X += (rect.Width - size.Width) / 2;
            }
            rect.Width = size.Width;
            return rect;
        }
        /// <summary>
        /// 调整X坐标和宽度,使宽度为size的宽度,在水平方向上移动对齐到矩形
        /// </summary>
        /// <param name="size">大小</param>
        /// <param name="rect">矩形</param>
        /// <param name="align">水平对齐方式</param>
        /// <returns>新矩形</returns>
        public static RectangleF HAlign(SizeF size, RectangleF rect, HorizontalAlignment align)
        {
            if ((align & HorizontalAlignment.Right) != 0)
            {
                rect.X += rect.Width - size.Width;
            }
            else if ((align & HorizontalAlignment.Center) != 0)
            {
                rect.X += (rect.Width - size.Width) / 2f;
            }
            rect.Width = size.Width;
            return rect;
        }
 
 
        /// <summary>
        /// 从容器大小中减去指定大小by关系.重叠关系(需要预先手动排除)
        /// </summary>
        /// <param name="containerSize">容器大小</param>
        /// <param name="contentSize">内容大小</param>
        /// <param name="relation">容器内容关系</param>
        /// <returns>新大小</returns>
        public static Size SubAlignedRegion(Size containerSize, Size contentSize, TextImageRelation relation)
        {
            return SubAlignedRegionCore(containerSize, contentSize, IsVerticalRelation(relation));
        }
        /// <summary>
        /// 从容器大小中减去指定大小by是否上下关系.重叠关系(需要预先手动排除)
        /// </summary>
        /// <param name="containerSize">容器大小</param>
        /// <param name="contentSize">内容大小</param>
        /// <param name="vertical">是否垂直关系</param>
        /// <returns>新大小</returns>
        public static Size SubAlignedRegionCore(Size containerSize, Size contentSize, bool vertical)
        {
            if (vertical)
            {
                containerSize.Height -= contentSize.Height;
            }
            else
            {
                containerSize.Width -= contentSize.Width;
            }
            return containerSize;
        }
        /// <summary>
        /// 是否垂直关系
        /// </summary>
        /// <param name="relation">文本图片相对位置</param>
        /// <returns>是垂直关系返回true,否则返回false</returns>
        public static bool IsVerticalRelation(TextImageRelation relation)
        {
            return ((relation & (TextImageRelation.TextAboveImage | TextImageRelation.ImageAboveText)) != TextImageRelation.Overlay);
        }
 
 
        /// <summary>
        /// 两个大小相加,by关系.重叠关系(需要预先手动排除)
        /// </summary>
        /// <param name="contentSize1">内容大小1</param>
        /// <param name="contentSize2">内容大小2</param>
        /// <param name="relation">两个内容关系</param>
        /// <returns>新大小</returns>
        public static Size AddAlignedRegion(Size contentSize1, Size contentSize2, TextImageRelation relation)
        {
            return AddAlignedRegionCore(contentSize1, contentSize2, IsVerticalRelation(relation));
        }
        /// <summary>
        /// 两个大小相加,by是否上下关系.重叠关系(需要预先手动排除)
        /// </summary>
        /// <param name="contentSize1">内容大小1</param>
        /// <param name="contentSize2">内容大小2</param>
        /// <param name="vertical">是否吹关系</param>
        /// <returns>新大小</returns>
        public static Size AddAlignedRegionCore(Size contentSize1, Size contentSize2, bool vertical)
        {
            if (vertical)
            {
                contentSize1.Width = Math.Max(contentSize1.Width, contentSize2.Width);
                contentSize1.Height += contentSize2.Height;
            }
            else
            {
                contentSize1.Width += contentSize2.Width;
                contentSize1.Height = Math.Max(contentSize1.Height, contentSize2.Height);
            }
            return contentSize1;
        }
 
 
        /// <summary>
        /// 获取两个重叠的Size的最小容器的大小
        /// </summary>
        /// <param name="a">大小1</param>
        /// <param name="b">大小2</param>
        /// <returns>容器大小</returns>
        public static Size UnionSizes(Size a, Size b)
        {
            return new Size(Math.Max(a.Width, b.Width), Math.Max(a.Height, b.Height));
        }
        /// <summary>
        /// 按锚定方式分割矩形
        /// </summary>
        /// <param name="bounds">要被分割的矩形</param>
        /// <param name="contentSize">内容大小</param>
        /// <param name="anchor">锚定方式</param>
        /// <param name="region1">第一个矩形</param>
        /// <param name="region2">第二个矩形</param>
        public static void SplitRegion(Rectangle bounds, Size contentSize, AnchorStyles anchor, out Rectangle region1, out Rectangle region2)
        {
            region1 = region2 = bounds;
            switch (anchor)
            {
                case AnchorStyles.Top:
                    region1.Height = contentSize.Height;
                    region2.Y += contentSize.Height;
                    region2.Height -= contentSize.Height;
                    return;
 
                case AnchorStyles.Bottom:
                    region1.Y += bounds.Height - contentSize.Height;
                    region1.Height = contentSize.Height;
                    region2.Height -= contentSize.Height;
                    break;
 
                case (AnchorStyles.Bottom | AnchorStyles.Top):
                    break;
 
                case AnchorStyles.Left:
                    region1.Width = contentSize.Width;
                    region2.X += contentSize.Width;
                    region2.Width -= contentSize.Width;
                    return;
 
                case AnchorStyles.Right:
                    region1.X += bounds.Width - contentSize.Width;
                    region1.Width = contentSize.Width;
                    region2.Width -= contentSize.Width;
                    return;
 
                default:
                    return;
            }
        }
 
 
        /// <summary>
        /// 将region2按anchor锚定到bounds,将region1按反转的anchor锚定到bounds
        /// </summary>
        /// <param name="bounds">容器矩形</param>
        /// <param name="anchor">锚定方式</param>
        /// <param name="region1">矩形1</param>
        /// <param name="region2">矩形2</param>
        public static void ExpandRegionsToFillBounds(Rectangle bounds, AnchorStyles anchor, ref Rectangle region1, ref Rectangle region2)
        {
            switch (anchor)
            {
                case AnchorStyles.Top:
                    region1 = SubstituteSpecifiedBounds(bounds, region1, AnchorStyles.Bottom);
                    region2 = SubstituteSpecifiedBounds(bounds, region2, AnchorStyles.Top);
                    return;
 
                case AnchorStyles.Bottom:
                    region1 = SubstituteSpecifiedBounds(bounds, region1, AnchorStyles.Top);
                    region2 = SubstituteSpecifiedBounds(bounds, region2, AnchorStyles.Bottom);
                    break;
 
                case (AnchorStyles.Bottom | AnchorStyles.Top):
                    break;
 
                case AnchorStyles.Left:
                    region1 = SubstituteSpecifiedBounds(bounds, region1, AnchorStyles.Right);
                    region2 = SubstituteSpecifiedBounds(bounds, region2, AnchorStyles.Left);
                    return;
 
                case AnchorStyles.Right:
                    region1 = SubstituteSpecifiedBounds(bounds, region1, AnchorStyles.Left);
                    region2 = SubstituteSpecifiedBounds(bounds, region2, AnchorStyles.Right);
                    return;
 
                default:
                    return;
            }
        }
        /// <summary>
        /// 锚定内容矩形到容器矩形
        /// </summary>
        /// <param name="containerBounds">容器矩形</param>
        /// <param name="contentBounds">内容矩形</param>
        /// <param name="anchor">锚定方式</param>
        /// <returns>新矩形</returns>
        private static Rectangle SubstituteSpecifiedBounds(Rectangle containerBounds, Rectangle contentBounds, AnchorStyles anchor)
        {
            int left = ((anchor & AnchorStyles.Left) != AnchorStyles.None) ? contentBounds.Left : containerBounds.Left;
            int top = ((anchor & AnchorStyles.Top) != AnchorStyles.None) ? contentBounds.Top : containerBounds.Top;
            int right = ((anchor & AnchorStyles.Right) != AnchorStyles.None) ? contentBounds.Right : containerBounds.Right;
            int bottom = ((anchor & AnchorStyles.Bottom) != AnchorStyles.None) ? contentBounds.Bottom : containerBounds.Bottom;
            return Rectangle.FromLTRB(left, top, right, bottom);
        }
    }
}