tangxu
2024-11-04 ebd031e3bed6c1cfddce8fc9b98f7f9e95fb9e32
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
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Animate.Poison;
using DPumpHydr.WinFrmUI.RLT.Drawing.Poison;
using DPumpHydr.WinFrmUI.RLT.Enum.Poison;
using DPumpHydr.WinFrmUI.RLT.Forms;
using DPumpHydr.WinFrmUI.RLT.Interface.Poison;
using DPumpHydr.WinFrmUI.RLT.Manager;
using DPumpHydr.WinFrmUI.RLT.Native;
using System;
using System.Drawing;
using System.Windows.Forms;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region PoisonTaskWindow
 
    public sealed class PoisonTaskWindow : PoisonForm
    {
        private static PoisonTaskWindow singletonWindow;
 
        public static void ShowTaskWindow(IWin32Window parent, string title, Control userControl, int secToClose)
        {
            if (singletonWindow != null)
            {
                singletonWindow.Close();
                singletonWindow.Dispose();
                singletonWindow = null;
            }
 
            singletonWindow = new PoisonTaskWindow(secToClose, userControl)
            {
                Text = title,
                Resizable = false,
                Movable = true,
                StartPosition = FormStartPosition.Manual
            };
 
            if (parent is not null and IPoisonForm form)
            {
                singletonWindow.Theme = form.Theme;
                singletonWindow.Style = form.Style;
                singletonWindow.StyleManager = form.StyleManager.Clone(singletonWindow) as PoisonStyleManager;
            }
 
            singletonWindow.Show();
        }
 
        public static bool IsVisible()
        {
            return singletonWindow != null && singletonWindow.Visible;
        }
 
        public static void ShowTaskWindow(IWin32Window parent, string text, Control userControl)
        {
            ShowTaskWindow(parent, text, userControl, 0);
        }
 
        public static void ShowTaskWindow(string text, Control userControl, int secToClose)
        {
            ShowTaskWindow(null, text, userControl, secToClose);
        }
 
        public static void ShowTaskWindow(string text, Control userControl)
        {
            ShowTaskWindow(null, text, userControl);
        }
 
        public static void CancelAutoClose()
        {
            if (singletonWindow != null)
            {
                singletonWindow.CancelTimer = true;
            }
        }
 
        public static void ForceClose()
        {
            if (singletonWindow != null)
            {
                CancelAutoClose();
                singletonWindow.Close();
                singletonWindow.Dispose();
                singletonWindow = null;
            }
        }
 
        public bool CancelTimer { get; set; } = false;
 
        private readonly int closeTime = 0;
        private int elapsedTime = 0;
        private int progressWidth = 0;
        private DelayedCall timer;
 
        public bool StartLocation { get; set; } = true;
 
        public bool CustomSize { get; set; } = false;
 
        private readonly PoisonPanel controlContainer;
 
        public PoisonTaskWindow()
        {
            controlContainer = new PoisonPanel();
            Controls.Add(controlContainer);
        }
 
        public PoisonTaskWindow(int duration, Control userControl) : this()
        {
            controlContainer.Controls.Add(userControl);
            userControl.Dock = DockStyle.Fill;
            closeTime = duration * 500;
 
            if (closeTime > 0)
            {
                timer = DelayedCall.Start(UpdateProgress, 5);
            }
        }
 
 
        private bool isInitialized = false;
        protected override void OnActivated(EventArgs e)
        {
            if (!isInitialized)
            {
                controlContainer.Theme = Theme;
                controlContainer.Style = Style;
                controlContainer.StyleManager = StyleManager;
 
                MaximizeBox = false;
                MinimizeBox = false;
                Movable = true;
 
                TopMost = true;
 
                if (!CustomSize)
                {
                    Size = new(400, 200);
                }
 
                if (!StartLocation)
                {
                    TaskBar myTaskbar = new();
                    Location = myTaskbar.Position switch
                    {
                        TaskBarPosition.Left => new Point(myTaskbar.Bounds.Width + 5, myTaskbar.Bounds.Height - Height - 5),
                        TaskBarPosition.Top => new Point(myTaskbar.Bounds.Width - Width - 5, myTaskbar.Bounds.Height + 5),
                        TaskBarPosition.Right => new Point(myTaskbar.Bounds.X - Width - 5, myTaskbar.Bounds.Height - Height - 5),
                        TaskBarPosition.Bottom => new Point(myTaskbar.Bounds.Width - Width - 5, myTaskbar.Bounds.Y - Height - 5),
                        _ => new Point(Screen.PrimaryScreen.Bounds.Width - Width - 5, Screen.PrimaryScreen.Bounds.Height - Height - 5),
                    };
                }
 
                controlContainer.Location = new(0, 60);
                controlContainer.Size = new(Width - 40, Height - 80);
                controlContainer.Anchor = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Left;
 
                controlContainer.AutoScroll = false;
                controlContainer.HorizontalScrollbar = false;
                controlContainer.VerticalScrollbar = false;
                controlContainer.Refresh();
 
                if (StyleManager != null)
                {
                    StyleManager.Update();
                }
 
                isInitialized = true;
 
                MoveAnimation myMoveAnim = new();
                myMoveAnim.Start(controlContainer, new Point(20, 60), TransitionType.EaseInOutCubic, 15);
            }
 
            base.OnActivated(e);
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
 
            using SolidBrush b = new(PoisonPaint.BackColor.Form(Theme));
            e.Graphics.FillRectangle(b, new Rectangle(Width - progressWidth, 0, progressWidth, 5));
        }
 
        private void UpdateProgress()
        {
            if (elapsedTime == closeTime)
            {
                timer.Dispose();
                timer = null;
                Close();
                return;
            }
 
            elapsedTime += 5;
 
            if (CancelTimer)
            {
                elapsedTime = 0;
            }
 
            double perc = elapsedTime / ((double)closeTime / 100);
            progressWidth = (int)(Width * (perc / 100));
            Invalidate(new Rectangle(0, 0, Width, 5));
 
            if (!CancelTimer)
            {
                timer.Reset();
            }
        }
    }
 
    #endregion
}