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
// 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.Design;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
 
namespace AntdUI
{
    /// <summary>
    /// Spin 加载中
    /// </summary>
    /// <remarks>用于页面和区块的加载中状态。</remarks>
    [Description("Spin 加载中")]
    [ToolboxItem(true)]
    public class Spin : IControl
    {
        #region 属性
 
        [Description("颜色"), Category("外观"), DefaultValue(null)]
        [Editor(typeof(Design.ColorEditor), typeof(UITypeEditor))]
        public Color? Fill { get; set; }
 
        string? text = null;
        /// <summary>
        /// 文本
        /// </summary>
        [Description("文本"), Category("外观"), DefaultValue(null)]
        public override string? Text
        {
            get => text;
            set
            {
                if (text == value) return;
                text = value;
                spin_core.Clear();
                Invalidate();
                OnTextChanged(EventArgs.Empty);
            }
        }
 
        #endregion
 
        #region 动画
 
        SpinCore spin_core = new SpinCore();
        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            spin_core.Start(this);
        }
 
        protected override void OnFontChanged(EventArgs e)
        {
            spin_core.Clear();
            base.OnFontChanged(e);
        }
 
        #endregion
 
        protected override void OnPaint(PaintEventArgs e)
        {
            var rect = ClientRectangle.PaddingRect(Padding);
            if (rect.Width == 0 || rect.Height == 0) return;
            spin_core.Paint(e.Graphics.High(), rect, text, Fill ?? Style.Db.Primary, null, this);
        }
 
        protected override void Dispose(bool disposing)
        {
            spin_core.Dispose();
            base.Dispose(disposing);
        }
 
        /// <summary>
        /// 配置
        /// </summary>
        public class Config
        {
            /// <summary>
            /// 文本
            /// </summary>
            public string? Text { get; set; }
 
            /// <summary>
            /// 背景颜色
            /// </summary>
            public Color? Back { get; set; }
 
            /// <summary>
            /// 颜色
            /// </summary>
            public Color? Color { get; set; }
 
            /// <summary>
            /// 字体
            /// </summary>
 
            public Font? Font { get; set; }
 
            /// <summary>
            /// 圆角
            /// </summary>
            public int? Radius { get; set; }
        }
 
        #region 静态方法
 
        /// <summary>
        /// Spin 加载中
        /// </summary>
        /// <param name="control">控件主体</param>
        /// <param name="action">需要等待的委托</param>
        /// <param name="end">运行结束后的回调</param>
        public static void open(Control control, Action action, Action? end = null)
        {
            open(control, new Config(), action, end);
        }
 
        /// <summary>
        /// Spin 加载中
        /// </summary>
        /// <param name="control">控件主体</param>
        /// <param name="text">加载文本</param>
        /// <param name="action">需要等待的委托</param>
        /// <param name="end">运行结束后的回调</param>
        public static void open(Control control, string text, Action action, Action? end = null)
        {
            open(control, new Config { Text = text }, action, end);
        }
 
        /// <summary>
        /// Spin 加载中
        /// </summary>
        /// <param name="control">控件主体</param>
        /// <param name="config">自定义配置</param>
        /// <param name="action">需要等待的委托</param>
        /// <param name="end">运行结束后的回调</param>
        public static void open(Control control, Config config, Action action, Action? end = null)
        {
            var parent = control.FindPARENT();
            if (parent is LayeredFormModal model)
            {
                model.Load += (a, b) =>
                {
                    control.BeginInvoke(new Action(() =>
                    {
                        open_core(control, parent, config, action, end);
                    }));
                };
                return;
            }
            else if (parent is LayeredFormDrawer drawer)
            {
                drawer.LoadOK = () =>
                {
                    control.BeginInvoke(new Action(() =>
                    {
                        open_core(control, parent, config, action, end);
                    }));
                };
                return;
            }
            else if (control.InvokeRequired)
            {
                control.BeginInvoke(new Action(() =>
                {
                    open_core(control, parent, config, action, end);
                }));
                return;
            }
            open_core(control, parent, config, action, end);
        }
 
        static void open_core(Control control, Form? parent, Config config, Action action, Action? end = null)
        {
            var frm = new SpinForm(control, parent, config);
            frm.Show(control);
            ITask.Run(() =>
            {
                try
                {
                    action();
                }
                catch { }
                frm.Invoke(new Action(() =>
                {
                    frm.Dispose();
                }));
            }, end);
        }
 
        #endregion
    }
 
    internal class SpinCore : IDisposable
    {
        ITask? thread = null;
 
        float LineWidth = 6, LineAngle = 0;
        float prog_size = 0;
        public void Clear()
        {
            prog_size = 0;
        }
 
        public void Start(IControl control)
        {
            bool ProgState = false;
            thread = new ITask(control, () =>
            {
                if (ProgState)
                {
                    LineAngle = LineAngle.Calculate(9F);
                    LineWidth = LineWidth.Calculate(0.6F);
                    if (LineWidth > 75) ProgState = false;
                }
                else
                {
                    LineAngle = LineAngle.Calculate(9.6F);
                    LineWidth = LineWidth.Calculate(-0.6F);
                    if (LineWidth < 6) ProgState = true;
                }
                if (LineAngle >= 360) LineAngle = 0;
                control.Invalidate();
                return true;
            }, 10);
        }
        public void Start(ILayeredForm control)
        {
            bool ProgState = false;
            thread = new ITask(control, () =>
            {
                if (ProgState)
                {
                    LineAngle = LineAngle.Calculate(9F);
                    LineWidth = LineWidth.Calculate(0.6F);
                    if (LineWidth > 75) ProgState = false;
                }
                else
                {
                    LineAngle = LineAngle.Calculate(9.6F);
                    LineWidth = LineWidth.Calculate(-0.6F);
                    if (LineWidth < 6) ProgState = true;
                }
                if (LineAngle >= 360) LineAngle = 0;
                control.Print();
                return true;
            }, 10);
        }
 
        readonly StringFormat s_f = Helper.SF_ALL();
        public void Paint(Graphics g, Rectangle rect, string? text, Color color, Font? font, Control control)
        {
            if (prog_size == 0) prog_size = g.MeasureString(text ?? Config.NullText, font ?? control.Font).Height;
 
            float rprog_size = prog_size * 1.4F, size = prog_size * .1F, size2 = prog_size / 2F;
 
            var rect_prog = new RectangleF(rect.X + (rect.Width - rprog_size) / 2, rect.Y + (rect.Height - rprog_size) / 2, rprog_size, rprog_size);
            if (text != null)
            {
                var y = rect_prog.Bottom;
                rect_prog.Offset(0, -size2);
                using (var brush = new SolidBrush(control.ForeColor))
                {
                    g.DrawStr(text, font ?? control.Font, brush, new RectangleF(rect.X, y, rect.Width, prog_size), s_f);
                }
            }
            using (var brush = new Pen(color, size))
            {
                brush.StartCap = brush.EndCap = LineCap.Round;
                try
                {
                    g.DrawArc(brush, rect_prog, LineAngle, LineWidth * 3.6F);
                }
                catch { }
 
            }
        }
 
        public void Dispose()
        {
            thread?.Dispose();
        }
    }
    internal class SpinForm : ILayeredFormOpacity
    {
        Control control;
        Form? parent = null;
 
        Spin.Config config;
        public SpinForm(Control _control, Form? _parent, Spin.Config _config)
        {
            control = _control;
            parent = _parent;
            Font = _control.Font;
            config = _config;
            _control.SetTopMost(Handle);
            SetSize(_control.Size);
            SetLocation(_control.PointToScreen(Point.Empty));
            if (_config.Radius.HasValue) Radius = _config.Radius.Value;
            else if (_control is IControl icontrol) gpath = icontrol.RenderRegion;
        }
 
        GraphicsPath? gpath = null;
        int Radius = 0;
 
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            spin_core.Start(this);
            if (parent != null)
            {
                parent.LocationChanged += Parent_LocationChanged;
                parent.SizeChanged += Parent_SizeChanged;
            }
        }
 
        private void Parent_LocationChanged(object? sender, EventArgs e)
        {
            SetLocation(control.PointToScreen(Point.Empty));
        }
        private void Parent_SizeChanged(object? sender, EventArgs e)
        {
            SetLocation(control.PointToScreen(Point.Empty));
            SetSize(control.Size);
            if (!config.Radius.HasValue && control is IControl icontrol)
            {
                gpath?.Dispose();
                gpath = icontrol.RenderRegion;
            }
        }
 
        #region 渲染
 
        SpinCore spin_core = new SpinCore();
        public override Bitmap PrintBit()
        {
            var rect = TargetRectXY;
            var original_bmp = new Bitmap(rect.Width, rect.Height);
            using (var g = Graphics.FromImage(original_bmp).HighLay())
            {
                try
                {
                    using (var brush = new SolidBrush(config.Back ?? Color.FromArgb(100, Style.Db.TextBase)))
                    {
                        if (gpath != null) g.FillPath(brush, gpath);
                        else if (Radius > 0)
                        {
                            using (var path = rect.RoundPath(Radius)) { g.FillPath(brush, path); }
                        }
                        else g.FillRectangle(brush, rect);
                    }
                }
                catch { }
                spin_core.Paint(g, rect, config.Text, config.Color ?? Style.Db.Primary, config.Font, this);
            }
            return original_bmp;
        }
 
        #endregion
 
        protected override void Dispose(bool disposing)
        {
            spin_core.Dispose();
            if (parent != null)
            {
                parent.LocationChanged -= Parent_LocationChanged;
                parent.SizeChanged -= Parent_SizeChanged;
            }
            gpath?.Dispose();
            base.Dispose(disposing);
            if (control == null) return;
        }
    }
}