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
|
// Interface IFadingMessageBox.cs ================================================
using System;
namespace Cyrons
{
public interface IFadingMessageBox
{
void ShowAndFade(string message, string title, double TimeToFade);
void ShowAndFade(string message, string title, double TimeToFade,
FadingMessageBox.FadingMessageBoxIcon icon);
}
}
// FadingMessageBox.cs ========================================================
#region Gebrauchsbeispiel
/*
private void button_OpenMyMessagebox_Click(object sender, EventArgs e)
{
IFadingMessageBox fmb = new FadingMessageBox();
fmb.ShowAndFade("Made by Cyron", "Hinweis", 1.0,
FadingMessageBox.FadingMessageBoxIcon.Information);
}
*/
#endregion
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
namespace Cyrons
{
/// <summary>
/// Stellt eine Messagebox dar,
/// die sich automatisch nach einer
/// vorgegebenen Zeit auflöst.
/// </summary>
public partial class FadingMessageBox : Form, IFadingMessageBox
{
public FadingMessageBox()
{
InitializeComponent();
}
/// <summary>
/// Verhindert die mehrfache Ausführung eines Threads,
/// während die MessageBox noch aktiv ist.
/// </summary>
private static volatile bool isAlive;
#region Properties
private static string _message;
protected static string Message
{
get
{
return _message;
}
set
{
_message = value;
}
}
private static string _title;
protected static string Title
{
get
{
return _title;
}
set
{
_title = value;
}
}
private static double _fadingTime;
protected static double FadingTime
{
get
{
return _fadingTime;
}
set
{
_fadingTime = value;
}
}
private static FadingMessageBoxIcon _fmbIcon;
protected static FadingMessageBoxIcon FmbIcon
{
get
{
return _fmbIcon;
}
set
{
_fmbIcon = value;
}
}
#endregion
/// <summary>
/// Stellt eine Messagebox dar, die sich automatisch nach einer
/// vorgegebenen Zeit auflöst.
/// </summary>
/// <param name="message">Nachrichtentext</param>
/// <param name="TimeToFade">Zeit in Sekunden, die die Messagebox sichtbar sein soll,
/// bevor sie ausblendet.</param>
void IFadingMessageBox.ShowAndFade(string message, string title, double timeToFade)
{
if(!isAlive)
{
Message = message;
Title = title;
FadingTime = timeToFade;
Thread bgThread = new Thread(new ThreadStart(FadeNoIcon));
bgThread.Name = "ShowAndFadeWithoutIcon";
bgThread.Priority = ThreadPriority.BelowNormal;
bgThread.Start();
}
}
/// <summary>
/// Stellt eine Messagebox dar, die sich automatisch nach einer
/// vorgegebenen Zeit auflöst.
/// </summary>
/// <param name="message">Nachrichtentext</param>
/// <param name="TimeToFade">Zeit in Sekunden, die die Messagebox sichtbar sein soll,
/// bevor sie ausblendet.</param>
/// <param name="icon">MessageboxIcon-Enumerator</param>
void IFadingMessageBox.ShowAndFade(string message, string title,
double timeToFade, FadingMessageBoxIcon icon)
{
if(!isAlive)
{
Message = message;
Title = title;
FadingTime = timeToFade;
FmbIcon = icon;
Thread bgThread = new Thread(new ThreadStart(Fade));
bgThread.Name = "ShowAndFadeWithIcon";
bgThread.Priority = ThreadPriority.BelowNormal;
bgThread.Start();
}
}
private void FadeNoIcon()
{
isAlive = true;
try
{
int time = Convert.ToInt32(FadingTime * 1000);
this.Text = Title;
label_Message.Text = Message;
//Autosize-Feature
this.Size = new Size(label_Message.Size.Width + 75, this.Size.Height);
this.Show();
this.BringToFront();
this.Refresh(); // Muß gemacht werden, weil die Form sonst ohne Inhalt erscheint.
System.Threading.Thread.Sleep((time));
for(double i = 100; i > -1; i--)
{
this.Opacity = i / 100; // Die Opazität ist von 1.0 bis 0.0 definiert.
this.Refresh(); // Muß gemacht werden, weil die Form sonst ohne Inhalt erscheint.
}
this.Close();
}
catch(ObjectDisposedException)
{
MessageBox.Show("Konstruktor-Instanzen der FadingMessageBox-Klasse sind unzulässig!",
"FEHLER!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
isAlive = false;
}
private void Fade()
{
isAlive = true;
try
{
int time = Convert.ToInt32(FadingTime * 1000);
this.Text = Title;
label_Message.Text = Message;
//Autosize-Feature
this.Size = new Size(label_Message.Size.Width + 75, this.Size.Height);
InsertIcon(FmbIcon);
this.Show();
this.BringToFront();
this.Refresh(); // Muß gemacht werden, weil die Form sonst ohne Inhalt erscheint.
PlaySystemSound(FmbIcon);
System.Threading.Thread.Sleep(time);
for(double i = 100; i > -1; i--)
{
this.Opacity = i / 100; // Die Transparenz ist von 1.0 bis 0.0 definiert.
this.Refresh(); // Muß gemacht werden, weil die Form sonst ohne Inhalt erscheint.
}
this.Close();
}
catch(ObjectDisposedException)
{
MessageBox.Show("Konstruktor-Instanzen der FadingMessageBox-Klasse sind unzulässig!",
"FEHLER!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
isAlive = false;
}
#region Comment
/* Es gibt noch mehr Messagebox-Icons,
* aber es sind doppelte:
* Asterisk = Information
* Hand = Error
* Exclamation = Warning
*/
#endregion
/// <summary>
/// Setzt ein System-Icon in die Messagebox.
/// </summary>
/// <param name="icon">Icon-Enumerator</param>
private void InsertIcon(FadingMessageBoxIcon icon)
{
switch(icon)
{
case FadingMessageBoxIcon.Error:
pictureBox1.Size = new Size(SystemIcons.Error.Width,
SystemIcons.Error.Height);
pictureBox1.Image = SystemIcons.Error.ToBitmap();
break;
case FadingMessageBoxIcon.Information:
pictureBox1.Size = new Size(SystemIcons.Information.Width,
SystemIcons.Information.Height);
pictureBox1.Image = SystemIcons.Information.ToBitmap();
break;
case FadingMessageBoxIcon.Question:
pictureBox1.Size = new Size(SystemIcons.Question.Width,
SystemIcons.Question.Height);
pictureBox1.Image = SystemIcons.Question.ToBitmap();
break;
case FadingMessageBoxIcon.Warning:
pictureBox1.Size = new Size(SystemIcons.Warning.Width,
SystemIcons.Warning.Height);
pictureBox1.Image = SystemIcons.Warning.ToBitmap();
break;
}
}
/// <summary>
/// Spielt einen Systemsound ab, abhängig vom gewählten System-Icon.
/// </summary>
/// <param name="icon">Icon-Enumerator</param>
private void PlaySystemSound(FadingMessageBoxIcon icon)
{
switch(icon)
{
case FadingMessageBoxIcon.Error:
System.Media.SystemSounds.Hand.Play();
break;
case FadingMessageBoxIcon.Information:
System.Media.SystemSounds.Asterisk.Play();
break;
case FadingMessageBoxIcon.Question:
System.Media.SystemSounds.Question.Play();
break;
case FadingMessageBoxIcon.Warning:
System.Media.SystemSounds.Beep.Play();
break;
}
}
/// <summary>
/// Stellt verschiedene System-Icons zur Verfügung.
/// </summary>
public enum FadingMessageBoxIcon
{
Error = 1,
Information,
Question,
Warning
}
}
}
// FadingMessageBox.Designer.cs ===============================================
namespace Cyrons
{
partial class FadingMessageBox
{
/// <summary>
/// Erforderliche Designervariable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Verwendete Ressourcen bereinigen.
/// </summary>
/// <param name="disposing">True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.</param>
protected override void Dispose(bool disposing)
{
if(disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Vom Windows Form-Designer generierter Code
/// <summary>
/// Erforderliche Methode für die Designerunterstützung.
/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
/// </summary>
private void InitializeComponent()
{
this.label_Message = new System.Windows.Forms.Label();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// label_Message
//
this.label_Message.AutoSize = true;
this.label_Message.Location = new System.Drawing.Point(62, 37);
this.label_Message.Name = "label_Message";
this.label_Message.Size = new System.Drawing.Size(78, 13);
this.label_Message.TabIndex = 0;
this.label_Message.Text = "label_Message";
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(12, 28);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(32, 32);
this.pictureBox1.TabIndex = 1;
this.pictureBox1.TabStop = false;
//
// FadingMessageBox
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(147, 92);
this.ControlBox = false;
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.label_Message);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "FadingMessageBox";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "FadingMessageBox";
this.TopMost = true;
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label_Message;
private System.Windows.Forms.PictureBox pictureBox1;
}
}
|