chenn
2025-04-11 e98de937b28d42493de5dea6365c853d6b412d3c
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
498
499
500
501
502
503
// COPYRIGHT (C) Tom. ALL RIGHTS RESERVED.
// THE AntdUI PROJECT IS AN WINFORM LIBRARY LICENSED UNDER THE Apache-2.0 License.
// LICENSED UNDER THE Apache License, VERSION 2.0 (THE "License")
// YOU MAY NOT USE THIS FILE EXCEPT IN COMPLIANCE WITH THE License.
// YOU MAY OBTAIN A COPY OF THE LICENSE AT
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING, SOFTWARE
// DISTRIBUTED UNDER THE LICENSE IS DISTRIBUTED ON AN "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
// SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING PERMISSIONS AND
// LIMITATIONS UNDER THE License.
// GITEE: https://gitee.com/antdui/AntdUI
// GITHUB: https://github.com/AntdUI/AntdUI
// CSDN: https://blog.csdn.net/v_132
// QQ: 17379620
 
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
 
namespace AntdUI
{
    /// <summary>
    /// Alert 警告提示
    /// </summary>
    /// <remarks>警告提示,展现需要关注的信息。</remarks>
    [Description("Alert 警告提示")]
    [ToolboxItem(true)]
    [DefaultProperty("Text")]
    [Designer(typeof(IControlDesigner))]
    public class Alert : IControl
    {
        #region 属性
 
        int radius = 6;
        /// <summary>
        /// 圆角
        /// </summary>
        [Description("圆角"), Category("外观"), DefaultValue(6)]
        public int Radius
        {
            get => radius;
            set
            {
                if (radius == value) return;
                radius = value;
                Invalidate();
            }
        }
 
        float borWidth = 0F;
        /// <summary>
        /// 边框宽度
        /// </summary>
        [Description("边框宽度"), Category("边框"), DefaultValue(0F)]
        public float BorderWidth
        {
            get => borWidth;
            set
            {
                if (borWidth == value) return;
                borWidth = value;
                Invalidate();
            }
        }
 
        string? text = null;
        /// <summary>
        /// 文本
        /// </summary>
        [Description("文本"), Category("外观"), DefaultValue(null)]
        public override string? Text
        {
            get => text;
            set
            {
                if (text == value) return;
                if (loop && string.IsNullOrEmpty(value))
                {
                    task?.Dispose();
                    task = null;
                }
                font_size = null;
                text = value;
                Invalidate();
                OnTextChanged(EventArgs.Empty);
            }
        }
 
        string? textTitle = null;
        /// <summary>
        /// 标题
        /// </summary>
        [Description("标题"), Category("外观"), DefaultValue(null)]
        public string? TextTitle
        {
            get => textTitle;
            set
            {
                if (textTitle == value) return;
                textTitle = value;
                Invalidate();
            }
        }
 
        TType icon = TType.None;
        /// <summary>
        /// 样式
        /// </summary>
        [Description("样式"), Category("外观"), DefaultValue(TType.None)]
        public TType Icon
        {
            get => icon;
            set
            {
                if (icon == value) return;
                icon = value;
                Invalidate();
            }
        }
 
        bool loop = false;
        /// <summary>
        /// 文本轮播
        /// </summary>
        [Description("文本轮播"), Category("外观"), DefaultValue(false)]
        public bool Loop
        {
            get => loop;
            set
            {
                if (loop == value) return;
                loop = value;
                if (IsHandleCreated) StartTask();
            }
        }
 
        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            if (loop) StartTask();
        }
 
        protected override void OnFontChanged(EventArgs e)
        {
            font_size = null;
            base.OnFontChanged(e);
        }
 
        /// <summary>
        /// 文本轮播速率
        /// </summary>
        [Description("文本轮播速率"), Category("外观"), DefaultValue(10)]
        public int LoopSpeed { get; set; } = 10;
 
        #region 动画
 
        ITask? task = null;
        private void StartTask()
        {
            task?.Dispose();
            if (loop)
            {
                task = new ITask(this, () =>
                {
                    if (font_size.HasValue)
                    {
                        val += 1F;
                        if (val > font_size.Value.Width)
                        {
                            if (Width > font_size.Value.Width) val = 0F;
                            else val = -(Width - Padding.Horizontal);
                        }
                        Invalidate();
                    }
                    return loop;
                }, LoopSpeed);
            }
            else Invalidate();
        }
 
        #endregion
 
        #region 参数
 
        float val = 0;
        SizeF? font_size = null;
 
        #endregion
 
        /// <summary>
        /// 显示区域(容器)
        /// </summary>
        public override Rectangle DisplayRectangle
        {
            get => ClientRectangle.DeflateRect(Padding);
        }
 
        #endregion
 
        #region 渲染
 
        protected override void OnPaint(PaintEventArgs e)
        {
            var rect = DisplayRectangle;
            var g = e.Graphics.High();
            if (icon == TType.None)
            {
                if (loop)
                {
                    if (font_size == null && !string.IsNullOrEmpty(text)) font_size = g.MeasureString(text, Font);
                    if (font_size.HasValue)
                    {
                        g.SetClip(rect);
                        PaintText(g, rect, font_size.Value, ForeColor);
                        g.ResetClip();
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(textTitle))
                    {
                        var size = g.MeasureString(text, Font);
                        font_size = size;
                        float icon_size = size.Height * 0.86F, gap = icon_size * 0.4F;
 
                        using (var brush = new SolidBrush(ForeColor))
                        {
                            var rect_txt = new RectangleF(rect.X + gap, rect.Y, rect.Width - gap * 2, rect.Height);
                            g.DrawStr(text, Font, brush, rect_txt, stringLeft);
                        }
                    }
                    else
                    {
                        using (var font_title = new Font(Font.FontFamily, Font.Size * 1.14F, Font.Style))
                        {
                            var size_title = g.MeasureString(textTitle, font_title);
                            float icon_size = size_title.Height * 1.2F, gap = icon_size * 0.5F;
 
                            using (var brush = new SolidBrush(ForeColor))
                            {
                                var rect_txt = new RectangleF(rect.X + gap, rect.Y + gap, rect.Width - (gap * 2), size_title.Height);
                                g.DrawStr(textTitle, font_title, brush, rect_txt, stringLeft);
 
                                var desc_y = rect_txt.Bottom + icon_size * 0.33F;
                                var rect_txt_desc = new RectangleF(rect_txt.X, desc_y, rect_txt.Width, rect.Height - (desc_y + gap));
                                g.DrawStr(text, Font, brush, rect_txt_desc, stringLTEllipsis);
                            }
                        }
                    }
                }
            }
            else
            {
                float _radius = radius * Config.Dpi;
                Color back, bor_color, color = Style.Db.Text;
                switch (icon)
                {
                    case TType.Success:
                        back = Style.Db.SuccessBg;
                        bor_color = Style.Db.SuccessBorder;
                        break;
                    case TType.Info:
                        back = Style.Db.InfoBg;
                        bor_color = Style.Db.InfoBorder;
                        break;
                    case TType.Warn:
                        back = Style.Db.WarningBg;
                        bor_color = Style.Db.WarningBorder;
                        break;
                    case TType.Error:
                        back = Style.Db.ErrorBg;
                        bor_color = Style.Db.ErrorBorder;
                        break;
                    default:
                        back = Style.Db.SuccessBg;
                        bor_color = Style.Db.SuccessBorder;
                        break;
                }
                using (var path = rect.RoundPath(_radius))
                {
                    using (var brush = new SolidBrush(back))
                    {
                        g.FillPath(brush, path);
                    }
                    if (loop)
                    {
                        if (font_size == null && !string.IsNullOrEmpty(text)) font_size = g.MeasureString(text, Font);
                        if (font_size.HasValue)
                        {
                            int icon_size = (int)(font_size.Value.Height * .86F), gap = (int)(icon_size * .4F);
                            var rect_icon = new Rectangle(gap, rect.Y + (rect.Height - icon_size) / 2, icon_size, icon_size);
                            PaintText(g, rect, rect_icon, font_size.Value, color, back, _radius);
                            g.ResetClip();
                            g.PaintIcons(icon, rect_icon, Style.Db.BgBase);
                        }
                    }
                    else
                    {
                        var size = g.MeasureString(text, Font);
                        font_size = size;
                        if (string.IsNullOrEmpty(textTitle))
                        {
                            int icon_size = (int)(font_size.Value.Height * .86F), gap = (int)(icon_size * .4F);
                            var rect_icon = new Rectangle(rect.X + gap, rect.Y + (rect.Height - icon_size) / 2, icon_size, icon_size);
                            g.PaintIcons(icon, rect_icon, Style.Db.BgBase);
                            using (var brush = new SolidBrush(color))
                            {
                                var rect_txt = new RectangleF(rect_icon.X + rect_icon.Width + gap, rect.Y, rect.Width - (rect_icon.Width + gap * 2), rect.Height);
                                g.DrawStr(text, Font, brush, rect_txt, stringLeft);
                            }
                        }
                        else
                        {
                            using (var font_title = new Font(Font.FontFamily, Font.Size * 1.14F, Font.Style))
                            {
                                var size_title = g.MeasureString(textTitle, font_title);
                                int icon_size = (int)(font_size.Value.Height * 1.2F), gap = (int)(icon_size * .5F);
 
                                var rect_icon = new Rectangle(rect.X + gap, rect.Y + gap, icon_size, icon_size);
                                g.PaintIcons(icon, rect_icon, Style.Db.BgBase);
 
                                using (var brush = new SolidBrush(color))
                                {
                                    var rect_txt = new RectangleF(rect_icon.X + rect_icon.Width + icon_size / 2F, rect_icon.Y, rect.Width - (rect_icon.Width + gap * 2), rect_icon.Height);
                                    g.DrawStr(textTitle, font_title, brush, rect_txt, stringLeft);
 
                                    var desc_y = rect_txt.Bottom + icon_size * 0.2F;
                                    var rect_txt_desc = new RectangleF(rect_txt.X, desc_y, rect_txt.Width, rect.Height - (desc_y + gap));
                                    g.DrawStr(text, Font, brush, rect_txt_desc, stringLTEllipsis);
                                }
                            }
                        }
                    }
 
                    if (borWidth > 0)
                    {
                        using (var brush_bor = new Pen(bor_color, borWidth * Config.Dpi))
                        {
                            g.DrawPath(brush_bor, path);
                        }
                    }
                }
            }
            this.PaintBadge(g);
            base.OnPaint(e);
        }
 
        #region 渲染帮助
 
        readonly StringFormat stringLTEllipsis = Helper.SF_Ellipsis(tb: StringAlignment.Near, lr: StringAlignment.Near);
        readonly StringFormat stringCenter = Helper.SF_NoWrap();
        readonly StringFormat stringLeft = Helper.SF_ALL(lr: StringAlignment.Near);
 
        /// <summary>
        /// 渲染文字
        /// </summary>
        /// <param name="g">GDI</param>
        /// <param name="rect">区域</param>
        /// <param name="size">文字大小</param>
        /// <param name="fore">文字颜色</param>
        void PaintText(Graphics g, RectangleF rect, SizeF size, Color fore)
        {
            using (var brush = new SolidBrush(fore))
            {
                if (string.IsNullOrEmpty(textTitle))
                {
                    var rect_txt = new RectangleF(rect.X - val, rect.Y, size.Width, rect.Height);
                    g.DrawStr(text, Font, brush, rect_txt, stringCenter);
                    if (rect.Width > size.Width)
                    {
                        var maxw = rect.Width + rect_txt.Width / 2;
                        var rect_txt2 = new RectangleF(rect_txt.Right, rect_txt.Y, rect_txt.Width, rect_txt.Height);
                        while (rect_txt2.X < maxw)
                        {
                            g.DrawStr(text, Font, brush, rect_txt2, stringCenter);
                            rect_txt2.X = rect_txt2.Right;
                        }
                    }
                }
                else
                {
                    var size_title = g.MeasureString(textTitle, Font);
                    var rect_txt = new RectangleF(rect.X + size_title.Width - val, rect.Y, size.Width, rect.Height);
                    g.DrawStr(text, Font, brush, rect_txt, stringCenter);
                    if (rect.Width > size.Width)
                    {
                        var maxw = rect.Width + rect_txt.Width / 2;
                        var rect_txt2 = new RectangleF(rect_txt.Right, rect_txt.Y, rect_txt.Width, rect_txt.Height);
                        while (rect_txt2.X < maxw)
                        {
                            g.DrawStr(text, Font, brush, rect_txt2, stringCenter);
                            rect_txt2.X = rect_txt2.Right;
                        }
                    }
 
                    var rect_icon_l = new RectangleF(rect.X, rect.Y, (size.Height + size_title.Width) * 2, rect.Height);
                    using (var brush2 = new LinearGradientBrush(rect_icon_l, BackColor, Color.Transparent, 0F))
                    {
                        g.FillRectangle(brush2, rect_icon_l);
                        g.FillRectangle(brush2, rect_icon_l);
                    }
                    g.DrawStr(TextTitle, Font, brush, new RectangleF(rect.X, rect.Y, size_title.Width, rect.Height), stringCenter);
                }
            }
        }
 
        /// <summary>
        /// 渲染文字(渐变遮罩)
        /// </summary>
        /// <param name="g">GDI</param>
        /// <param name="rect">区域</param>
        /// <param name="size">文字大小</param>
        /// <param name="fore">文字颜色</param>
        /// <param name="back">背景颜色</param>
        void PaintText(Graphics g, RectangleF rect, RectangleF rect_icon, SizeF size, Color fore, Color back, float radius)
        {
            using (var brush_fore = new SolidBrush(fore))
            {
                var rect_txt = new RectangleF(rect.X - val, rect.Y, size.Width, rect.Height);
 
                g.SetClip(new RectangleF(rect.X, rect_txt.Y + ((rect.Height - size.Height) / 2), rect.Width, size.Height));
 
                g.DrawStr(text, Font, brush_fore, rect_txt, stringCenter);
                if (rect.Width > size.Width)
                {
                    var maxw = rect.Width + rect_txt.Width / 2;
                    var rect_txt2 = new RectangleF(rect_txt.Right, rect_txt.Y, rect_txt.Width, rect_txt.Height);
                    while (rect_txt2.X < maxw)
                    {
                        g.DrawStr(text, Font, brush_fore, rect_txt2, stringCenter);
                        rect_txt2.X = rect_txt2.Right;
                    }
                }
 
                RectangleF rect_icon_l;
                if (string.IsNullOrEmpty(textTitle))
                {
                    rect_icon_l = new RectangleF(rect.X, rect.Y, size.Height * 2, rect.Height);
                    using (var brush = new LinearGradientBrush(rect_icon_l, back, Color.Transparent, 0F))
                    {
                        rect_icon_l.Width -= 1F;
                        g.FillRectangle(brush, rect_icon_l);
                        g.FillRectangle(brush, rect_icon_l);
                    }
                }
                else
                {
                    var size_title = g.MeasureString(TextTitle, Font).Size();
                    rect_icon_l = new RectangleF(rect.X, rect.Y, (size.Height + size_title.Width) * 2, rect.Height);
                    using (var brush = new LinearGradientBrush(rect_icon_l, back, Color.Transparent, 0F))
                    {
                        g.FillRectangle(brush, rect_icon_l);
                        g.FillRectangle(brush, rect_icon_l);
                    }
                    g.DrawStr(TextTitle, Font, brush_fore, new RectangleF(rect_icon.Right, rect.Y, size_title.Width, rect.Height), stringCenter);
                }
                var rect_icon_r = new RectangleF(rect.Right - rect_icon_l.Width, rect_icon_l.Y, rect_icon_l.Width, rect_icon_l.Height);
                using (var brush = new LinearGradientBrush(rect_icon_r, Color.Transparent, back, 0F))
                {
                    rect_icon_r.X += 1F;
                    rect_icon_r.Width -= 1F;
                    g.FillRectangle(brush, rect_icon_r);
                    g.FillRectangle(brush, rect_icon_r);
                }
            }
        }
 
        #endregion
 
        public override Rectangle ReadRectangle
        {
            get => DisplayRectangle;
        }
 
        public override GraphicsPath RenderRegion
        {
            get => DisplayRectangle.RoundPath(radius * Config.Dpi);
        }
 
        #endregion
 
        #region 事件
 
        protected override void OnMouseEnter(EventArgs e)
        {
            task?.Dispose();
            base.OnMouseEnter(e);
        }
 
        protected override void OnMouseLeave(EventArgs e)
        {
            if (loop) StartTask();
            base.OnMouseLeave(e);
        }
 
        #endregion
    }
}