tangxu
2024-12-29 72e75456f8b30ec5b6f355539d9c883b0f810d21
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
 
namespace Microsoft.Windows.Forms
{
    public static partial class RenderEngine
    {
        /// <summary>
        /// 创建路径。
        /// </summary>
        /// <param name="rect">矩形。</param>
        /// <returns>创建的路径。</returns>
        public static GraphicsPath CreateGraphicsPath(Rectangle rect)
        {
            return CreateGraphicsPath(rect.X, rect.Y, rect.Width, rect.Height);
        }
 
        /// <summary>
        /// 创建路径。
        /// </summary>
        /// <param name="pt">左上角。</param>
        /// <param name="sz">大小</param>
        /// <returns>创建的路径。</returns>
        public static GraphicsPath CreateGraphicsPath(Point pt, Size sz)
        {
            return CreateGraphicsPath(pt.X, pt.Y, sz.Width, sz.Height);
        }
 
        /// <summary>
        /// 创建路径。
        /// </summary>
        /// <param name="x">左上角水平坐标。</param>
        /// <param name="y">左上角垂直坐标</param>
        /// <param name="width">宽度。</param>
        /// <param name="height">高度。</param>
        /// <returns>创建的路劲。</returns>
        public static GraphicsPath CreateGraphicsPath(int x, int y, int width, int height)
        {
            Point[] points = new Point[4];
            points[0] = new Point(x, y);
            points[1] = new Point(x + width, y);
            points[2] = new Point(x + width, y + height);
            points[3] = new Point(x, y + height);
            GraphicsPath shape = new GraphicsPath();
            shape.AddPolygon(points);
            return shape;
        }
 
        /// <summary>
        /// 创建路径。
        /// </summary>
        /// <param name="rect">用来创建路径的矩形。</param>
        /// <param name="cornerStyle">圆角弯曲样式。</param>
        /// <param name="roundStyle">圆角的样式。</param>
        /// <param name="radius">圆角半径。</param>
        /// <param name="correct">是否把矩形长宽减 1,以便画出边框。</param>
        /// <returns>创建的路径。</returns>
        public static GraphicsPath CreateGraphicsPath(Rectangle rect, CornerStyle cornerStyle, RoundStyle roundStyle, float radius, bool correct)
        {
            //-----------------校准-----------------
            if (correct)
            {
                rect.Width--;
                rect.Height--;
            }
 
            //-----------------定义返回值-----------------
            GraphicsPath path = new GraphicsPath();
            //-----------------特殊情况处理-----------------
            if (float.IsNaN(radius) || radius <= 0f)
            {
                path.AddRectangle(rect);
                return path;
            }
            //-----------------临时变量定义-----------------
            float diameter = radius * 2;//直径
            float halfWidth = rect.Width / 2f;//宽度一半
            float halfHeight = rect.Height / 2f;//高度一半
            PointF ptMiddleCenter = new PointF(rect.X + halfWidth, rect.Y + halfHeight);//中心点            
            float lrDegrees = 0f;//半径大于半高,圆心角
            float lrOffset = 0f;//半径大于半高,圆弧到边距离
            if ((roundStyle & RoundStyle.All) != 0 && radius > halfHeight)
            {
                double lrRadian = Math.Acos((radius - halfHeight) / radius);//弧度
                lrDegrees = (float)MathEx.ToDegrees(lrRadian);//角度
                lrOffset = (float)(radius * Math.Sin(lrRadian));
            }
            float tbDegrees = 0f;//半径大于半宽,圆心角
            float tbOffset = 0f;//半径大于办宽,圆弧到边距离
            if ((roundStyle & RoundStyle.All) != 0 && radius > halfWidth)
            {
                double tbRadian = Math.Acos((radius - halfWidth) / radius);//弧度
                tbDegrees = (float)MathEx.ToDegrees(tbRadian);//角度
                tbOffset = (float)(radius * Math.Sin(tbRadian));
            }
            //临时变量
            PointF ptBegin;
            PointF ptEnd;
 
 
            #region 左上
            if ((roundStyle & RoundStyle.TopLeft) == 0)//直角
            {
                if ((cornerStyle & CornerStyle.LeftIn) != 0)
                {
                    ptBegin = new PointF(rect.X + radius, ptMiddleCenter.Y);
                    ptEnd = new PointF(rect.X, rect.Y);
                    path.AddLine(ptBegin, ptEnd);
                }
                else if ((cornerStyle & CornerStyle.LeftOut) != 0)
                {
                    ptBegin = new PointF(rect.X, ptMiddleCenter.Y);
                    ptEnd = new PointF(rect.X + radius, rect.Y);
                    path.AddLine(ptBegin, ptEnd);
                }
                else if ((cornerStyle & CornerStyle.TopIn) != 0)
                {
                    ptBegin = new PointF(rect.X, rect.Y);
                    ptEnd = new PointF(ptMiddleCenter.X, rect.Y + radius);
                    path.AddLine(ptBegin, ptEnd);
                }
                else if ((cornerStyle & CornerStyle.TopOut) != 0)
                {
                    ptBegin = new PointF(rect.X, rect.Y + radius);
                    ptEnd = new PointF(ptMiddleCenter.X, rect.Y);
                    path.AddLine(ptBegin, ptEnd);
                }
                else
                {
                    ptBegin = ptEnd = new PointF(rect.X, rect.Y);
                    path.AddLine(ptBegin, ptEnd);
                }
            }
            else//圆角
            {
                if ((cornerStyle & CornerStyle.LeftIn) != 0)
                {
                    if (radius > halfHeight)
                        path.AddArc(rect.X - radius, rect.Y, diameter, diameter, 270 + lrDegrees, -lrDegrees);
                    else
                        path.AddArc(rect.X - radius, rect.Y, diameter, diameter, 0, -90);
                }
                else if ((cornerStyle & CornerStyle.LeftOut) != 0)
                {
                    if (radius > halfHeight)
                        path.AddArc(rect.X - (radius - lrOffset), rect.Y, diameter, diameter, 270 - lrDegrees, lrDegrees);
                    else
                        path.AddArc(rect.X, rect.Y, diameter, diameter, 180, 90);
                }
                else if ((cornerStyle & CornerStyle.TopIn) != 0)
                {
                    if (radius > halfWidth)
                        path.AddArc(rect.X, rect.Y - radius, diameter, diameter, 180, -tbDegrees);
                    else
                        path.AddArc(rect.X, rect.Y - radius, diameter, diameter, 180, -90);
                }
                else if ((cornerStyle & CornerStyle.TopOut) != 0)
                {
                    if (radius > halfWidth)
                        path.AddArc(rect.X, rect.Y - (radius - tbOffset), diameter, diameter, 180, tbDegrees);
                    else
                        path.AddArc(rect.X, rect.Y, diameter, diameter, 180, 90);
                }
                else
                {
                    path.AddArc(rect.X, rect.Y, diameter, diameter, 180, 90);
                }
            }
            #endregion
 
 
            #region 右上
            if ((roundStyle & RoundStyle.TopRight) == 0)
            {
                if ((cornerStyle & CornerStyle.RightIn) != 0)
                {
                    ptBegin = new PointF(rect.Right, rect.Y);
                    ptEnd = new PointF(rect.Right - radius, ptMiddleCenter.Y);
                    path.AddLine(ptBegin, ptEnd);
                }
                else if ((cornerStyle & CornerStyle.RightOut) != 0)
                {
                    ptBegin = new PointF(rect.Right - radius, rect.Y);
                    ptEnd = new PointF(rect.Right, ptMiddleCenter.Y);
                    path.AddLine(ptBegin, ptEnd);
                }
                else if ((cornerStyle & CornerStyle.TopIn) != 0)
                {
                    ptBegin = new PointF(ptMiddleCenter.X, rect.Y + radius);
                    ptEnd = new PointF(rect.Right, rect.Y);
                    path.AddLine(ptBegin, ptEnd);
                }
                else if ((cornerStyle & CornerStyle.TopOut) != 0)
                {
                    ptBegin = new PointF(ptMiddleCenter.X, rect.Y);
                    ptEnd = new PointF(rect.Right, rect.Y + radius);
                    path.AddLine(ptBegin, ptEnd);
                }
                else//矩形
                {
                    ptBegin = ptEnd = new PointF(rect.Right, rect.Y);
                    path.AddLine(ptBegin, ptEnd);
                }
            }
            else
            {
                if ((cornerStyle & CornerStyle.RightIn) != 0)
                {
                    if (radius > halfHeight)
                        path.AddArc(rect.Right - radius, rect.Y, diameter, diameter, 270, -lrDegrees);
                    else
                        path.AddArc(rect.Right - radius, rect.Y, diameter, diameter, 270, -90);
                }
                else if ((cornerStyle & CornerStyle.RightOut) != 0)
                {
                    if (radius > halfHeight)
                        path.AddArc(rect.Right - radius - lrOffset, rect.Y, diameter, diameter, 270, lrDegrees);
                    else
                        path.AddArc(rect.Right - diameter, rect.Y, diameter, diameter, 270, 90);
                }
                else if ((cornerStyle & CornerStyle.TopIn) != 0)
                {
                    if (radius > halfWidth)
                        path.AddArc(rect.Right - diameter, rect.Y - radius, diameter, diameter, tbDegrees, -tbDegrees);
                    else
                        path.AddArc(rect.Right - diameter, rect.Y - radius, diameter, diameter, 90, -90);
                }
                else if ((cornerStyle & CornerStyle.TopOut) != 0)
                {
                    if (radius > halfWidth)
                        path.AddArc(rect.Right - diameter, rect.Y - (radius - tbOffset), diameter, diameter, 360 - tbDegrees, tbDegrees);
                    else
                        path.AddArc(rect.Right - diameter, rect.Y, diameter, diameter, 270, 90);
                }
                else
                {
                    path.AddArc(rect.Right - diameter, rect.Y, diameter, diameter, 270, 90);
                }
            }
            #endregion
 
 
            #region 右下
            if ((roundStyle & RoundStyle.BottomRight) == 0)
            {
                if ((cornerStyle & CornerStyle.RightIn) != 0)
                {
                    ptBegin = new PointF(rect.Right - radius, ptMiddleCenter.Y);
                    ptEnd = new PointF(rect.Right, rect.Bottom);
                    path.AddLine(ptBegin, ptEnd);
                }
                else if ((cornerStyle & CornerStyle.RightOut) != 0)
                {
                    ptBegin = new PointF(rect.Right, ptMiddleCenter.Y);
                    ptEnd = new PointF(rect.Right - radius, rect.Bottom);
                    path.AddLine(ptBegin, ptEnd);
                }
                else if ((cornerStyle & CornerStyle.BottomIn) != 0)
                {
                    ptBegin = new PointF(rect.Right, rect.Bottom);
                    ptEnd = new PointF(ptMiddleCenter.X, rect.Bottom - radius);
                    path.AddLine(ptBegin, ptEnd);
                }
                else if ((cornerStyle & CornerStyle.BottomOut) != 0)
                {
                    ptBegin = new PointF(rect.Right, rect.Bottom - radius);
                    ptEnd = new PointF(ptMiddleCenter.X, rect.Bottom);
                    path.AddLine(ptBegin, ptEnd);
                }
                else//矩形
                {
                    ptBegin = ptEnd = new PointF(rect.Right, rect.Bottom);
                    path.AddLine(ptBegin, ptEnd);
                }
            }
            else
            {
                if ((cornerStyle & CornerStyle.RightIn) != 0)
                {
                    if (radius > halfHeight)
                        path.AddArc(rect.Right - radius, rect.Bottom - diameter, diameter, diameter, 90 + lrDegrees, -lrDegrees);
                    else
                        path.AddArc(rect.Right - radius, rect.Bottom - diameter, diameter, diameter, 180, -90);
                }
                else if ((cornerStyle & CornerStyle.RightOut) != 0)
                {
                    if (radius > halfHeight)
                        path.AddArc(rect.Right - radius - lrOffset, rect.Bottom - diameter, diameter, diameter, 90 - lrDegrees, lrDegrees);
                    else
                        path.AddArc(rect.Right - diameter, rect.Bottom - diameter, diameter, diameter, 0, 90);
                }
                else if ((cornerStyle & CornerStyle.BottomIn) != 0)
                {
                    if (radius > halfWidth)
                        path.AddArc(rect.Right - diameter, rect.Bottom - radius, diameter, diameter, 0, -tbDegrees);
                    else
                        path.AddArc(rect.Right - diameter, rect.Bottom - radius, diameter, diameter, 0, -90);
                }
                else if ((cornerStyle & CornerStyle.BottomOut) != 0)
                {
                    if (radius > halfWidth)
                        path.AddArc(rect.Right - diameter, rect.Bottom - radius - tbOffset, diameter, diameter, 0, tbDegrees);
                    else
                        path.AddArc(rect.Right - diameter, rect.Bottom - diameter, diameter, diameter, 0, 90);
                }
                else
                {
                    path.AddArc(rect.Right - diameter, rect.Bottom - diameter, diameter, diameter, 0, 90);
                }
            }
            #endregion
 
 
            #region 左下
            if ((roundStyle & RoundStyle.BottomLeft) == 0)
            {
                if ((cornerStyle & CornerStyle.LeftIn) != 0)
                {
                    ptBegin = new PointF(rect.X, rect.Bottom);
                    ptEnd = new PointF(rect.X + radius, ptMiddleCenter.Y);
                    path.AddLine(ptBegin, ptEnd);
                }
                else if ((cornerStyle & CornerStyle.LeftOut) != 0)
                {
                    ptBegin = new PointF(rect.X + radius, rect.Bottom);
                    ptEnd = new PointF(rect.X, ptMiddleCenter.Y);
                    path.AddLine(ptBegin, ptEnd);
                }
                else if ((cornerStyle & CornerStyle.BottomIn) != 0)
                {
                    ptBegin = new PointF(ptMiddleCenter.X, rect.Bottom - radius);
                    ptEnd = new PointF(rect.X, rect.Bottom);
                    path.AddLine(ptBegin, ptEnd);
                }
                else if ((cornerStyle & CornerStyle.BottomOut) != 0)
                {
                    ptBegin = new PointF(ptMiddleCenter.X, rect.Bottom);
                    ptEnd = new PointF(rect.X, rect.Bottom - radius);
                    path.AddLine(ptBegin, ptEnd);
                }
                else
                {
                    ptBegin = ptEnd = new PointF(rect.X, rect.Bottom);
                    path.AddLine(ptBegin, ptEnd);
                }
            }
            else
            {
                if ((cornerStyle & CornerStyle.LeftIn) != 0)
                {
                    if (radius > halfHeight)
                        path.AddArc(rect.X - radius, rect.Bottom - diameter, diameter, diameter, 90, -lrDegrees);
                    else
                        path.AddArc(rect.X - radius, rect.Bottom - diameter, diameter, diameter, 90, -90);
                }
                else if ((cornerStyle & CornerStyle.LeftOut) != 0)
                {
                    if (radius > halfHeight)
                        path.AddArc(rect.X - (radius - lrOffset), rect.Bottom - diameter, diameter, diameter, 90, lrDegrees);
                    else
                        path.AddArc(rect.X, rect.Bottom - diameter, diameter, diameter, 90, 90);
                }
                else if ((cornerStyle & CornerStyle.BottomIn) != 0)
                {
                    if (radius > halfWidth)
                        path.AddArc(rect.X, rect.Bottom - radius, diameter, diameter, 180 + tbDegrees, -tbDegrees);
                    else
                        path.AddArc(rect.X, rect.Bottom - radius, diameter, diameter, 270, -90);
                }
                else if ((cornerStyle & CornerStyle.BottomOut) != 0)
                {
                    if (radius > halfWidth)
                        path.AddArc(rect.X, rect.Bottom - radius - tbOffset, diameter, diameter, 180 - tbDegrees, tbDegrees);
                    else
                        path.AddArc(rect.X, rect.Bottom - diameter, diameter, diameter, 90, 90);
                }
                else
                {
                    path.AddArc(rect.X, rect.Bottom - diameter, diameter, diameter, 90, 90);
                }
            }
            #endregion
 
 
            //闭合返回
            path.CloseFigure();
            return path;
        }
 
        /// <summary>
        /// 建立带有圆角样式和标题的路径。
        /// </summary>
        /// <param name="rect">用来简历路径的矩形。</param>
        /// <param name="style">圆角样式。</param>
        /// <param name="radius">圆角大小。</param>
        /// <param name="tabSize">标题大小。</param>
        /// <param name="tabRound">标题是否圆角。</param>
        /// <param name="tabRadius">标题圆角大小。</param>
        /// <param name="correct">是否把矩形长宽减 1,以便画出边框。</param>
        /// <returns>简历的路径。</returns>
        public static GraphicsPath CreateGroupBoxTabGraphicsPath(Rectangle rect, RoundStyle style, float radius, Size tabSize, bool tabRound, float tabRadius, bool correct)
        {
            //校正
            if (correct)
            {
                rect.Width--;
                rect.Height--;
            }
            style = (float.IsNaN(radius) || radius <= 0f) ? RoundStyle.None : style;
            tabRound = (float.IsNaN(tabRadius) || tabRadius <= 0f) ? false : tabRound;
 
            //定义
            GraphicsPath path = new GraphicsPath();
            Rectangle tabRect = new Rectangle(rect.Location, tabSize);
            float diameter = radius * 2;
            float tabDiameter = tabRadius * 2;
            Point pt;
 
            //左上
            if ((style & RoundStyle.TopLeft) == 0)
            {
                pt = new Point(rect.X, rect.Y);
                path.AddLine(pt, pt);
            }
            else
            {
                path.AddArc(rect.X, rect.Y, diameter, diameter, 180, 90);
            }
 
            //Tab右上
            if (tabRound)
            {
                if (tabRadius > tabRect.Height)//不到90度
                {
                    double radians = Math.Acos((tabRadius - tabRect.Height) / tabRadius);
                    path.AddArc((float)(tabRect.Right - tabRadius * Math.Sin(radians) - tabRadius),
                        tabRect.Y, tabDiameter, tabDiameter, 270, (float)MathEx.ToDegrees(radians));
                }
                else//到90度
                {
                    path.AddArc(tabRect.Right - tabDiameter, tabRect.Y, tabDiameter, tabDiameter, 270, 90);
 
                    //Tab右下
                    pt = new Point(tabRect.Right, tabRect.Bottom);
                    path.AddLine(pt, pt);
                }
            }
            else
            {
                pt = new Point(tabRect.Right, tabRect.Y);
                path.AddLine(pt, pt);
 
                //Tab右下
                pt = new Point(tabRect.Right, tabRect.Bottom);
                path.AddLine(pt, pt);
            }
 
            //右上
            if ((style & RoundStyle.TopRight) == 0)
            {
                pt = new Point(rect.Right, tabRect.Bottom);
                path.AddLine(pt, pt);
            }
            else
            {
                path.AddArc(rect.Right - diameter, tabRect.Bottom, diameter, diameter, 270, 90);
            }
 
            //右下
            if ((style & RoundStyle.BottomRight) == 0)
            {
                pt = new Point(rect.Right, rect.Bottom);
                path.AddLine(pt, pt);
            }
            else
            {
                path.AddArc(rect.Right - diameter, rect.Bottom - diameter, diameter, diameter, 0, 90);
            }
 
            //左下
            if ((style & RoundStyle.BottomLeft) == 0)
            {
                pt = new Point(rect.X, rect.Bottom);
                path.AddLine(pt, pt);
            }
            else
            {
                path.AddArc(rect.X, rect.Bottom - diameter, diameter, diameter, 90, 90);
            }
 
            //闭合返回
            path.CloseFigure();
            return path;
        }
    }
}