tangxu
2024-12-16 23fadc9cb0c09b665a1bbcef7eaf16f916045dc4
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
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Colors;
using DPumpHydr.WinFrmUI.RLT.Util;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region HopeNotify
 
    public class HopeNotify : Control
    {
        #region Variables
 
 
        private readonly Color _DefaultBackColor = HopeColors.PrimaryColor;
        private readonly Color _DefaultTextColor = HopeColors.PrimaryColor;
 
        public enum AlertType
        {
            Success = 0,
            Warning = 1,
            Info = 2,
            Error = 3
        };
        #endregion
 
        #region Settings
 
        [RefreshProperties(RefreshProperties.Repaint)]
        public AlertType Type { get; set; } = AlertType.Success;
 
        private Timer _timer;
        private Timer _Timer
        {
            get => _timer;
            set
            {
                if (_timer != null)
                {
                    _timer.Tick -= Timer_Tick;
                }
 
                _timer = value;
                if (_timer != null)
                {
                    _timer.Tick += Timer_Tick;
                }
            }
        }
 
        [RefreshProperties(RefreshProperties.Repaint)]
        public bool Close { get; set; } = true;
 
        [RefreshProperties(RefreshProperties.Repaint)]
        public Color SuccessBackColor { get; set; } = Color.FromArgb(25, HopeColors.Success);
 
        [RefreshProperties(RefreshProperties.Repaint)]
        public Color SuccessTextColor { get; set; } = HopeColors.Success;
 
        [RefreshProperties(RefreshProperties.Repaint)]
        public Color WarningBackColor { get; set; } = Color.FromArgb(25, HopeColors.Warning);
 
        [RefreshProperties(RefreshProperties.Repaint)]
        public Color WarningTextColor { get; set; } = HopeColors.Warning;
 
        [RefreshProperties(RefreshProperties.Repaint)]
        public Color InfoBackColor { get; set; } = Color.FromArgb(25, HopeColors.Info);
 
        [RefreshProperties(RefreshProperties.Repaint)]
        public Color InfoTextColor { get; set; } = HopeColors.Info;
 
        [RefreshProperties(RefreshProperties.Repaint)]
        public Color ErrorBackColor { get; set; } = Color.FromArgb(25, HopeColors.Danger);
 
        [RefreshProperties(RefreshProperties.Repaint)]
        public Color ErrorTextColor { get; set; } = HopeColors.Danger;
 
        [RefreshProperties(RefreshProperties.Repaint)]
        public Color CloseColor { get; set; } = RoundRectangle.DarkBackColor;
 
        #endregion
 
        #region Events
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            Height = 34;
        }
 
        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
            if (Close)
            {
                Visible = false;
            }
        }
        #endregion
 
        #region OnPaint
 
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics graphics = e.Graphics;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
            graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            graphics.Clear(Parent.BackColor);
 
            SolidBrush backBrush = new(_DefaultBackColor);
            SolidBrush textBrush = new(_DefaultTextColor);
            switch (Type)
            {
                case AlertType.Success:
                    backBrush = new(SuccessBackColor);
                    textBrush = new(SuccessTextColor);
                    break;
                case AlertType.Warning:
                    backBrush = new(WarningBackColor);
                    textBrush = new(WarningTextColor);
                    break;
                case AlertType.Info:
                    backBrush = new(InfoBackColor);
                    textBrush = new(InfoTextColor);
                    break;
                case AlertType.Error:
                    backBrush = new(ErrorBackColor);
                    textBrush = new(ErrorTextColor);
                    break;
                default:
                    break;
            }
 
            GraphicsPath back = RoundRectangle.CreateRoundRect(0.5f, 0.5f, Width - 1, Height - 1, 3);
            graphics.FillPath(new SolidBrush(Color.White), back);
            graphics.FillPath(backBrush, back);
            graphics.DrawPath(new(textBrush, 1f), back);
            if (Close)
            {
                graphics.DrawString(Text, Font, textBrush, new RectangleF(20, 0, Width - 40, Height), HopeStringAlign.Left);
                graphics.DrawString("r", new Font("Marlett", 10), new SolidBrush(CloseColor), new Rectangle(Width - 34, 1, 34, 34), HopeStringAlign.Center);
            }
            else
            {
                graphics.DrawString(Text, Font, textBrush, new RectangleF(20, 0, Width - 35, Height), HopeStringAlign.Left);
            }
        }
 
        #endregion
 
        public HopeNotify()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer, true);
            DoubleBuffered = true;
            Font = new("Segoe UI", 12);
            Width = 150;
            Cursor = Cursors.Hand;
        }
 
        private void Timer_Tick(object sender, EventArgs e)
        {
            if (Close)
            {
                Visible = false;
            }
 
            _Timer.Enabled = false;
            _Timer.Dispose();
        }
 
        /// <summary>
        /// How to use: HopeNotify.ShowAlertBox(Type, String, Interval)
        /// </summary>
 
        public void ShowAlertBox(AlertType type, string text, int Interval)
        {
            Type = type;
            Text = text;
            Visible = true;
            _Timer = new Timer
            {
                Interval = Interval,
                Enabled = true
            };
        }
    }
 
    #endregion
}