yangyin
2025-01-13 36616eb44dc36a4e6e3e7a7540310cb850218ea9
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
// 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.Collections.Concurrent;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
 
namespace AntdUI
{
    public abstract class ILayeredFormAnimate : ILayeredForm
    {
        internal static Dictionary<string, List<ILayeredFormAnimate>> list = new Dictionary<string, List<ILayeredFormAnimate>>();
 
        internal string key = "";
        internal virtual TAlignFrom Align => TAlignFrom.TR;
        internal virtual bool ActiveAnimation => true;
 
        int start_X = 0, end_X = 0, start_Y = 0, end_Y = 0;
 
        #region 位置
 
        public int readY
        {
            get => TargetRect.Y + TargetRect.Height;
        }
 
        internal bool SetPosition(Form form, bool InWindow)
        {
            Rectangle workingArea;
            if (InWindow || Config.ShowInWindow) workingArea = new Rectangle(form.Location, form.Size);
            else workingArea = Screen.FromControl(form).WorkingArea;
            key = Align.ToString() + "|" + workingArea.X + "|" + workingArea.Y + "|" + workingArea.Right + "|" + workingArea.Bottom;
            int width = TargetRect.Width, height = TargetRect.Height;
            switch (Align)
            {
                case TAlignFrom.Top:
                    if (TopY(workingArea, out var resultTop)) return true;
                    int xt = start_X = end_X = workingArea.X + (workingArea.Width - width) / 2;
                    end_Y = resultTop;
                    SetLocation(xt, start_Y = end_Y - height / 2);
                    break;
                case TAlignFrom.Bottom:
                    if (BottomY(workingArea, out var resultBottom)) return true;
                    int xb = start_X = end_X = workingArea.X + (workingArea.Width - width) / 2;
                    end_Y = resultBottom;
                    SetLocation(xb, start_Y = end_Y + height / 2);
                    break;
                case TAlignFrom.TL:
                    if (TopY(workingArea, out var resultTL)) return true;
                    end_X = workingArea.X;
                    SetLocation(start_X = end_X - width / 3, start_Y = end_Y = resultTL);
                    break;
                case TAlignFrom.TR:
                    if (TopY(workingArea, out var resultTR)) return true;
                    end_X = workingArea.X + workingArea.Width - width;
                    SetLocation(start_X = end_X + width / 3, start_Y = end_Y = resultTR);
                    break;
                case TAlignFrom.BL:
                    if (BottomY(workingArea, out var resultBL)) return true;
                    end_X = workingArea.X;
                    SetLocation(start_X = end_X - width / 3, start_Y = end_Y = resultBL);
                    break;
                case TAlignFrom.BR:
                    if (BottomY(workingArea, out var resultBR)) return true;
                    end_X = workingArea.X + workingArea.Width - width;
                    SetLocation(start_X = end_X + width / 3, start_Y = end_Y = resultBR);
                    break;
            }
            Add();
            return false;
        }
        void Add()
        {
            if (list.TryGetValue(key, out var its)) its.Add(this);
            else list.Add(key, new List<ILayeredFormAnimate> { this });
        }
 
        #region 核心
 
        bool TopY(Rectangle workingArea, out int result)
        {
            int offset = (int)(Config.NoticeWindowOffsetXY * Config.Dpi);
            var y = TopYCore(workingArea, offset);
            if (y < workingArea.Bottom - TargetRect.Height)
            {
                result = y;
                return false;
            }
            else
            {
                result = 0;
                return true;
            }
        }
        int TopYCore(Rectangle workingArea, int offset)
        {
            if (list.TryGetValue(key, out var its) && its.Count > 0)
            {
                int tmp = its[its.Count - 1].TargetRect.Bottom;
                for (int i = 0; i < its.Count - 1; i++)
                {
                    var it = its[i];
                    if (it.TargetRect.Bottom > tmp) tmp = it.TargetRect.Bottom;
                }
                return tmp;
            }
            else return workingArea.Y + offset;
        }
        bool BottomY(Rectangle workingArea, out int result)
        {
            int offset = (int)(Config.NoticeWindowOffsetXY * Config.Dpi);
            var y = BottomYCore(workingArea, offset) - TargetRect.Height;
            if (y >= 0)
            {
                result = y;
                return false;
            }
            else
            {
                result = 0;
                return true;
            }
        }
        int BottomYCore(Rectangle workingArea, int offset)
        {
            if (list.TryGetValue(key, out var its) && its.Count > 0)
            {
                int tmp = its[its.Count - 1].TargetRect.Y;
                for (int i = 0; i < its.Count - 1; i++)
                {
                    var it = its[i];
                    if (it.TargetRect.Y < tmp) tmp = it.TargetRect.Y;
                }
                return tmp;
            }
            else return workingArea.Bottom - offset;
        }
 
        #endregion
 
        internal void SetPositionCenter(int w)
        {
            if (Align == TAlignFrom.Top || Align == TAlignFrom.Bottom)
            {
                int x = TargetRect.X + (w - TargetRect.Width) / 2;
                SetLocationX(x);
                start_X = end_X = x;
            }
            else if (Align == TAlignFrom.TR || Align == TAlignFrom.BR)
            {
                int x = TargetRect.X - (TargetRect.Width - w);
                SetLocationX(x);
                start_X = end_X = x;
            }
            Print();
        }
        internal void SetPositionY(int y)
        {
            SetLocationY(y);
            end_Y = y;
            start_Y = y - TargetRect.Height / 2;
        }
        internal void SetPositionYB(int y)
        {
            SetLocationY(y);
            end_Y = y;
            start_Y = y + TargetRect.Height / 2;
        }
 
        #endregion
 
        ITask? task_start = null;
        protected override void OnLoad(EventArgs e)
        {
            if (ActiveAnimation) PlayAnimation();
            base.OnLoad(e);
        }
 
        #region 动画
 
        #region 开始动画
 
        Bitmap? bmp_tmp = null;
        public void PlayAnimation()
        {
            if (Config.Animation)
            {
                var t = Animation.TotalFrames(10, 200);
                task_start = new ITask(start_X == end_X ? i =>
                {
                    var val = Animation.Animate(i, t, 1F, AnimationType.Ball);
                    SetAnimateValueY(start_Y + (int)((end_Y - start_Y) * val), (byte)(240 * val));
                    return true;
                }
                : i =>
                {
                    var val = Animation.Animate(i, t, 1F, AnimationType.Ball);
                    SetAnimateValueX(start_X + (int)((end_X - start_X) * val), (byte)(240 * val));
                    return true;
                }, 10, t, () =>
                {
                    DisposeAnimation();
                    SetAnimateValue(end_X, end_Y, 240);
                });
            }
            else
            {
                SetAnimateValue(end_X, end_Y, 240);
            }
        }
 
        #endregion
 
        #region 关闭动画
 
        internal ITask StopAnimation()
        {
            task_start?.Dispose();
            var t = Animation.TotalFrames(10, 200);
            return new ITask(start_X == end_X ? (i) =>
            {
                var val = Animation.Animate(i, t, 1F, AnimationType.Ball);
                SetAnimateValueY(end_Y - (int)((end_Y - start_Y) * val), (byte)(240 * (1F - val)));
                return true;
            }
            : (i) =>
            {
                var val = Animation.Animate(i, t, 1F, AnimationType.Ball);
                SetAnimateValueX(end_X - (int)((end_X - start_X) * val), (byte)(240 * (1F - val)));
                return true;
            }, 10, t, () =>
            {
                DisposeAnimation();
            });
        }
        public void DisposeAnimation()
        {
            bmp_tmp?.Dispose();
            bmp_tmp = null;
        }
 
        #endregion
 
        #region 设置动画参数
 
        void SetAnimateValueX(int x, byte _alpha)
        {
            if (TargetRect.X != x || alpha != _alpha)
            {
                SetLocationX(x);
                alpha = _alpha;
                if (bmp_tmp == null) bmp_tmp = PrintBit();
                if (bmp_tmp == null) return;
                Render(bmp_tmp);
            }
        }
        void SetAnimateValueY(int y, byte _alpha)
        {
            if (TargetRect.Y != y || alpha != _alpha)
            {
                SetLocationY(y);
                alpha = _alpha;
                if (bmp_tmp == null) bmp_tmp = PrintBit();
                if (bmp_tmp == null) return;
                Render(bmp_tmp);
            }
        }
        internal void SetAnimateValueY(int y)
        {
            if (closepassive) return;
            if (TargetRect.Y != y)
            {
                SetLocationY(y);
                if (bmp_tmp == null) bmp_tmp = PrintBit();
                if (bmp_tmp == null) return;
                Render(bmp_tmp);
            }
        }
        void SetAnimateValue(int x, int y, byte _alpha)
        {
            if (TargetRect.X != x || TargetRect.Y != y || alpha != _alpha)
            {
                SetLocation(x, y);
                alpha = _alpha;
                if (bmp_tmp == null) bmp_tmp = PrintBit();
                if (bmp_tmp == null) return;
                Render(bmp_tmp);
            }
        }
 
        #endregion
 
        #endregion
 
        #region 关闭
 
        bool handclose = false;
        bool closepassive = false;
        public void CloseMe(bool passive)
        {
            if (handclose) return;
            handclose = true;
            task_start?.Dispose();
            if (passive) MsgQueue.Add(this);
            else
            {
                closepassive = true;
                //执行关闭动画
                if (Config.Animation)
                {
                    ITask.Run(() =>
                    {
                        StopAnimation().Wait();
                    }, () =>
                    {
                        bool isRemove = list[key].Remove(this);
                        IClose(true);
                        if (isRemove) MsgQueue.Add(new object[] { Align, key });
                    });
                }
                else
                {
                    bool isRemove = list[key].Remove(this);
                    IClose(true);
                    if (isRemove) MsgQueue.Add(new object[] { Align, key });
                }
            }
        }
 
        #endregion
 
        protected override void Dispose(bool disposing)
        {
            task_start?.Dispose();
            list[key].Remove(this);
            base.Dispose(disposing);
        }
    }
 
    public static class MsgQueue
    {
        static ManualResetEvent _event = new ManualResetEvent(false);
        static ConcurrentQueue<object> queue = new ConcurrentQueue<object>();
        internal static List<string> volley = new List<string>();
 
        #region 添加队列
 
        public static void Add(Notification.Config config)
        {
            queue.Enqueue(config);
            _event.Set();
        }
        public static void Add(Message.Config config)
        {
            queue.Enqueue(config);
            _event.Set();
        }
 
        public static void Add(object?[] command)
        {
            queue.Enqueue(command);
            _event.Set();
        }
 
        internal static void Add(ILayeredFormAnimate command)
        {
            queue.Enqueue(command);
            _event.Set();
        }
 
        #endregion
 
        static MsgQueue()
        {
            new Thread(LongTask) { IsBackground = true }.Start();
        }
 
        static void LongTask()
        {
            while (true)
            {
                if (_event.Wait()) return;
                while (queue.TryDequeue(out var d))
                {
                    try
                    {
                        if (d is Notification.Config configNotification) Open(configNotification);
                        else if (d is Message.Config configMessage) Open(configMessage);
                        else if (d is ILayeredFormAnimate formAnimate)
                        {
                            if (Config.Animation) formAnimate.StopAnimation().Wait();
                            formAnimate.IClose(true);
                            Close(formAnimate.Align, formAnimate.key);
                        }
                        else if (d is object?[] command)
                        {
                            if (command[0] is TAlignFrom align && command[1] is string key) Close(align, key);
                        }
                    }
                    catch { }
                }
                volley.Clear();
                _event.Reset();
            }
        }
 
        static void Open(Notification.Config config)
        {
            if (config.Form.IsHandleCreated)
            {
                if (config.ID != null && volley.Contains("N" + config.ID)) return;
                bool ishand = false;
                config.Form.Invoke(new Action(() =>
                {
                    var from = new NotificationFrm(config);
                    if (from.IInit())
                    {
                        from.Dispose();
                        ishand = true;
                    }
                    else
                    {
                        if (config.TopMost) from.Show();
                        else from.Show(config.Form);
                    }
                }));
                if (ishand) Add(config);
            }
        }
 
        static void Open(Message.Config config)
        {
            if (config.Form.IsHandleCreated)
            {
                if (config.ID != null && volley.Contains("M" + config.ID)) return;
                bool ishand = false;
                config.Form.Invoke(new Action(() =>
                {
                    var from = new MessageFrm(config);
                    if (from.IInit())
                    {
                        from.Dispose();
                        ishand = true;
                    }
                    else from.Show(config.Form);
                }));
                if (ishand) Add(config);
            }
        }
 
        static void Close(TAlignFrom align, string key)
        {
            try
            {
                switch (align)
                {
                    case TAlignFrom.Bottom:
                    case TAlignFrom.BL:
                    case TAlignFrom.BR:
                        CloseB(key);
                        break;
                    case TAlignFrom.Top:
                    case TAlignFrom.TL:
                    case TAlignFrom.TR:
                    default:
                        CloseT(key);
                        break;
                }
            }
            catch { }
        }
 
        static void CloseT(string key)
        {
            var arr = key.Split('|');
            int y = int.Parse(arr[2]);
            int offset = (int)(Config.NoticeWindowOffsetXY * Config.Dpi);
            int y_temp = y + offset;
            var list = ILayeredFormAnimate.list[key];
            var dir = new Dictionary<ILayeredFormAnimate, int[]>(list.Count);
            foreach (var it in list)
            {
                int y2 = it.TargetRect.Y;
                if (y2 != y_temp) dir.Add(it, new int[] { y_temp, y2 - y_temp });
                y_temp += it.TargetRect.Height;
            }
            if (dir.Count > 0)
            {
                if (Config.Animation)
                {
                    var t = Animation.TotalFrames(10, 200);
                    new ITask(i =>
                    {
                        var val = Animation.Animate(i, t, 1F, AnimationType.Ball);
                        foreach (var it in dir) it.Key.SetAnimateValueY(it.Value[0] + (it.Value[1] - (int)(it.Value[1] * val)));
                        return true;
                    }, 10, t, () =>
                    {
                        y_temp = y + offset;
                        for (int i = 0; i < list.Count; i++)
                        {
                            var it = list[i];
                            it.DisposeAnimation();
                            it.SetAnimateValueY(y_temp);
                            it.SetPositionY(y_temp);
                            y_temp += it.TargetRect.Height;
                        }
                    }).Wait();
                }
                else
                {
                    y_temp = y + offset;
                    for (int i = 0; i < list.Count; i++)
                    {
                        var it = list[i];
                        it.DisposeAnimation();
                        it.SetAnimateValueY(y_temp);
                        it.SetPositionY(y_temp);
                        y_temp += it.TargetRect.Height;
                    }
                }
            }
        }
        static void CloseB(string key)
        {
            var arr = key.Split('|');
            int b = int.Parse(arr[4]);
            int offset = (int)(Config.NoticeWindowOffsetXY * Config.Dpi);
            int y_temp = b - offset;
            var list = ILayeredFormAnimate.list[key];
            var dir = new Dictionary<ILayeredFormAnimate, int[]>(list.Count);
            foreach (var it in list)
            {
                y_temp -= it.TargetRect.Height;
                int y2 = it.TargetRect.Y;
                if (y2 != y_temp) dir.Add(it, new int[] { y_temp, y2 - y_temp });
            }
            if (dir.Count > 0)
            {
                if (Config.Animation)
                {
                    var t = Animation.TotalFrames(10, 200);
                    new ITask(i =>
                    {
                        var val = Animation.Animate(i, t, 1F, AnimationType.Ball);
                        foreach (var it in dir) it.Key.SetAnimateValueY(it.Value[0] + (it.Value[1] - (int)(it.Value[1] * val)));
                        return true;
                    }, 10, t, () =>
                    {
                        y_temp = b - offset;
                        for (int i = 0; i < list.Count; i++)
                        {
                            var it = list[i];
                            it.DisposeAnimation();
                            y_temp -= it.TargetRect.Height;
                            it.SetAnimateValueY(y_temp);
                            it.SetPositionY(y_temp);
                        }
                    }).Wait();
                }
                else
                {
                    y_temp = b - offset;
                    for (int i = 0; i < list.Count; i++)
                    {
                        var it = list[i];
                        it.DisposeAnimation();
                        y_temp -= it.TargetRect.Height;
                        it.SetAnimateValueY(y_temp);
                        it.SetPositionY(y_temp);
                    }
                }
            }
        }
    }
}