-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBrumAlert.cs
266 lines (244 loc) · 9.13 KB
/
BrumAlert.cs
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
using BrumCustomAlerts.Properties;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace BrumCustomAlerts
{
internal partial class BrumAlert : Form
{
int interval = 0;
AlertLocation alertLocation;
#region Adding TOPMOST without stealing focus
protected override bool ShowWithoutActivation
{
get { return true; }
}
private const int WS_EX_TOPMOST = 0x00000008;
protected override CreateParams CreateParams
{
get
{
CreateParams createParams = base.CreateParams;
createParams.ExStyle |= WS_EX_TOPMOST;
createParams.ExStyle |= 0x80;
return createParams;
}
}
#endregion
#region constructors
public BrumAlert(string message, AlertType type, int delay, AlertLocation alertLocation)
{
InitializeComponent();
lblMessage.Text = message;
this.alertLocation = alertLocation;
switch (type)
{
case AlertType.Success:
pbMain.Image = Resources.success_white;
BackColor = Color.LimeGreen;
ForeColor = Color.White;
lblMessage.ForeColor = Color.White;
break;
case AlertType.Info:
pbMain.Image = Resources.info_white;
BackColor = Color.DeepSkyBlue;
ForeColor = Color.White;
lblMessage.ForeColor = Color.White;
break;
case AlertType.Warning:
pbMain.Image = Resources.warning_white;
BackColor = Color.Orange;
ForeColor = Color.White;
lblMessage.ForeColor = Color.White;
break;
case AlertType.Error:
pbMain.Image = Resources.error_white;
BackColor = Color.Crimson;
ForeColor = Color.WhiteSmoke;
lblMessage.ForeColor = Color.White;
break;
default:
throw new Exception("Unkown setting!");
}
Show();
timerClose.Interval = delay;
timerClose.Start();
}
public BrumAlert(string message, Color foregroundColor, Color backgroundColor, AlertType type, int delay, AlertLocation alertLocation)
{
InitializeComponent();
lblMessage.Text = message;
this.alertLocation = alertLocation;
switch (type)
{
case AlertType.Success:
pbMain.Image = Resources.success_white;
break;
case AlertType.Info:
pbMain.Image = Resources.info_white;
break;
case AlertType.Warning:
pbMain.Image = Resources.warning_white;
break;
case AlertType.Error:
pbMain.Image = Resources.error_white;
break;
default:
throw new Exception("Unkown setting!");
}
BackColor = backgroundColor;
lblMessage.ForeColor = foregroundColor;
Show();
timerClose.Interval = delay;
timerClose.Start();
}
public BrumAlert(string message, Color foregroundColor, Color backgroundColor, Bitmap image, int delay, AlertLocation alertLocation)
{
InitializeComponent();
lblMessage.Text = message;
this.alertLocation = alertLocation;
pbMain.Image = image;
BackColor = backgroundColor;
lblMessage.ForeColor = foregroundColor;
Show();
timerClose.Interval = delay;
timerClose.Start();
}
#endregion
private void alert_Load(object sender, EventArgs e)
{
switch (alertLocation)
{
case AlertLocation.TopRight:
Top = 0 - this.Height - 50;
Left = Screen.PrimaryScreen.Bounds.Width - this.Width - 50;
break;
case AlertLocation.TopMiddle:
Top = 0 - this.Height - 50;
Left = (Screen.PrimaryScreen.Bounds.Width / 2) - (Width / 2);
break;
case AlertLocation.TopLeft:
Top = 0 - this.Height - 50;
Left = 50;
break;
case AlertLocation.BottomRight:
Top = Screen.PrimaryScreen.Bounds.Height;
Left = Screen.PrimaryScreen.Bounds.Width - this.Width - 50;
break;
case AlertLocation.BottomMiddle:
Top = Screen.PrimaryScreen.Bounds.Height;
Left = (Screen.PrimaryScreen.Bounds.Width / 2) - (Width / 2);
break;
case AlertLocation.BottomLeft:
Top = Screen.PrimaryScreen.Bounds.Height;
Left = 50;
break;
default:
break;
}
}
private void OnClose(object sender, EventArgs e)
{
timerClosing.Start();
}
private void timerClose_Tick(object sender, EventArgs e)
{
timerClosing.Start();
}
private void timerShow_Tick(object sender, EventArgs e)
{
if (alertLocation == AlertLocation.TopLeft || alertLocation == AlertLocation.TopRight || alertLocation == AlertLocation.TopMiddle)
{
if (Top < this.Height - 30)
{
Top += interval;
interval += 1;
}
else
{
timerShow.Stop();
}
}
else
{
if (Top > Screen.PrimaryScreen.Bounds.Height - this.Height - 55)
{
Top -= interval;
interval += 2;
}
else
{
timerShow.Stop();
}
}
}
private void timerClosing_Tick(object sender, EventArgs e)
{
if (Opacity > 0)
{
Opacity -= 0.1;
}
else
{
Close();
}
}
}
public class BrumAlertFactory
{
/// <summary>
/// Default alert factory.
/// </summary>
/// <param name="message">Alert message text</param>
/// <param name="type">Alert type</param>
/// <param name="delay">Delay in miliseconds. Default 2000 miliseconds</param>
/// <param name="alertLocation">Alert location. Default BottomRight</param>
public static void OpenAlert(string message, AlertType type, int delay = 2000, AlertLocation alertLocation = AlertLocation.BottomRight)
{
BrumAlert alert = new BrumAlert(message, type, delay, alertLocation);
}
/// <summary>
/// Open custom made alert with custom image
/// </summary>
/// <param name="message">Alert message text</param>
/// <param name="foregroundColor">Text color</param>
/// <param name="backgroundColor">Background color</param>
/// <param name="image">Alert icon</param>
/// <param name="delay">Delay in miliseconds. Default 2000 miliseconds</param>
/// <param name="alertLocation">Alert location. Default BottomRight</param>
public static void OpenAlert(string message, Color foregroundColor, Color backgroundColor, Bitmap image, int delay = 2000, AlertLocation alertLocation = AlertLocation.BottomRight)
{
BrumAlert alert = new BrumAlert(message, foregroundColor, backgroundColor, image, delay, alertLocation);
}
/// <summary>
/// Open custom alert with pre-set icon
/// </summary>
/// <param name="message">Alert message text</param>
/// <param name="foregroundColor">Text color</param>
/// <param name="backgroundColor">Background color</param>
/// <param name="type">Alert type for pre-set image</param>
/// <param name="delay">Delay in miliseconds. Default 2000 miliseconds</param>
/// <param name="alertLocation"Alert location. Default BottomRight></param>
public static void OpenAlert(string message, Color foregroundColor, Color backgroundColor, AlertType type, int delay = 2000, AlertLocation alertLocation = AlertLocation.BottomRight)
{
BrumAlert alert = new BrumAlert(message, foregroundColor, backgroundColor, type, delay, alertLocation);
}
}
public enum AlertType
{
Success,
Info,
Warning,
Error
}
public enum AlertLocation
{
TopRight,
TopMiddle,
TopLeft,
BottomRight,
BottomMiddle,
BottomLeft
}
}