Toasts – Blame information for rev 10

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 // =====COPYRIGHT=====
2 // Code originally retrieved from http://www.vbforums.com/showthread.php?t=547778 - no license information supplied
3 // =====COPYRIGHT=====
4  
5 using System;
6 using System.Collections.Generic;
7 using System.Drawing;
8 office 8 using System.IO;
9 using System.Media;
10 using System.Reflection;
11 using System.Threading.Tasks;
1 office 12 using System.Windows.Forms;
13  
14 namespace Toasts
15 {
2 office 16 public partial class ToastForm : Form
1 office 17 {
18 #region Static Fields and Constants
19  
2 office 20 private static readonly List<ToastForm> OpenNotifications = new List<ToastForm>();
1 office 21  
22 #endregion
23  
2 office 24 #region Public Methods
25  
26 /// <summary>
27 /// Displays the form
28 /// </summary>
29 /// <remarks>
30 /// Required to allow the form to determine the current foreground window before being displayed
31 /// </remarks>
32 public new void Show()
33 {
34 // Determine the current foreground window so it can be reactivated each time this form tries to get the focus
35 _currentForegroundWindow = NativeMethods.GetForegroundWindow();
36  
37 base.Show();
38 }
39  
40 #endregion
41  
1 office 42 #region Constructors, Destructors and Finalizers
43  
8 office 44 private ToastForm()
7 office 45 {
46 InitializeComponent();
47 }
8 office 48  
1 office 49 /// <summary>
50 /// </summary>
51 /// <param name="title"></param>
52 /// <param name="body"></param>
53 /// <param name="duration"></param>
54 /// <param name="image"></param>
55 /// <param name="animation"></param>
56 /// <param name="direction"></param>
8 office 57 public ToastForm(string title, string body, int duration, Image logo, Image background, byte[] sound,
7 office 58 FormAnimator.AnimationMethod animation, FormAnimator.AnimationDirection direction) : this()
1 office 59 {
60 lifeTimer.Interval = duration;
61 labelTitle.Text = title;
62 labelBody.Text = body;
63 imageBox.Image = logo;
64 BackgroundImage = background;
65  
8 office 66 _sound = sound;
67  
1 office 68 _animator = new FormAnimator(this, animation, direction, 500);
69  
70 Region = Region.FromHrgn(NativeMethods.CreateRoundRectRgn(0, 0, Width - 5, Height - 5, 20, 20));
71 }
72  
8 office 73 public ToastForm(string title, string body, int duration, Image logo, byte[] sound,
7 office 74 FormAnimator.AnimationMethod animation, FormAnimator.AnimationDirection direction) : this()
2 office 75 {
76 lifeTimer.Interval = duration;
77 labelTitle.Text = title;
78 labelBody.Text = body;
79 imageBox.Image = logo;
1 office 80  
8 office 81 _sound = sound;
82  
2 office 83 _animator = new FormAnimator(this, animation, direction, 500);
84  
85 Region = Region.FromHrgn(NativeMethods.CreateRoundRectRgn(0, 0, Width - 5, Height - 5, 20, 20));
86 }
87  
8 office 88 public ToastForm(string title, string body, int duration, Image logo, byte[] sound) : this()
1 office 89 {
2 office 90 lifeTimer.Interval = duration;
91 labelTitle.Text = title;
92 labelBody.Text = body;
93 imageBox.Image = logo;
94  
8 office 95 _sound = sound;
96  
2 office 97 _animator = new FormAnimator(this, FormAnimator.AnimationMethod.Slide, FormAnimator.AnimationDirection.Up,
98 500);
99  
100 Region = Region.FromHrgn(NativeMethods.CreateRoundRectRgn(0, 0, Width - 5, Height - 5, 20, 20));
1 office 101 }
102  
9 office 103 public ToastForm(string title, string body, int duration, Image logo) : this()
104 {
105 lifeTimer.Interval = duration;
106 labelTitle.Text = title;
107 labelBody.Text = body;
108 imageBox.Image = logo;
109  
110 _animator = new FormAnimator(this, FormAnimator.AnimationMethod.Slide, FormAnimator.AnimationDirection.Up,
111 500);
112  
113 Region = Region.FromHrgn(NativeMethods.CreateRoundRectRgn(0, 0, Width - 5, Height - 5, 20, 20));
114 }
115  
10 office 116 public ToastForm(string title, string body, int duration, Icon logo) : this()
117 {
118 lifeTimer.Interval = duration;
119 labelTitle.Text = title;
120 labelBody.Text = body;
121 imageBox.Image = logo.ToBitmap();
122  
123 _animator = new FormAnimator(this, FormAnimator.AnimationMethod.Slide, FormAnimator.AnimationDirection.Up,
124 500);
125  
126 Region = Region.FromHrgn(NativeMethods.CreateRoundRectRgn(0, 0, Width - 5, Height - 5, 20, 20));
127 }
128  
7 office 129 /// <summary>
130 /// Clean up any resources being used.
131 /// </summary>
132 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
133 protected override void Dispose(bool disposing)
134 {
135 if (disposing && (components != null))
136 {
137 components.Dispose();
138 }
139 base.Dispose(disposing);
140 }
141  
1 office 142 #endregion
143  
144 #region Private Delegates, Events, Enums, Properties, Indexers and Fields
145  
146 private readonly FormAnimator _animator;
147  
148 private bool _allowFocus;
149  
150 private IntPtr _currentForegroundWindow;
151  
8 office 152 private byte[] _sound;
153  
1 office 154 #endregion
155  
156 #region Event Handlers
157  
9 office 158 private void Notification_Load(object sender, EventArgs e)
1 office 159 {
8 office 160 // Play the sound.
161 if (_sound == null)
162 {
9 office 163 using (var memoryStream = new MemoryStream())
164 {
165 Properties.Resources.normal.CopyTo(memoryStream);
166  
167 memoryStream.Position = 0L;
168  
169 _sound = memoryStream.ToArray();
170 }
8 office 171 }
172  
173 using (var memoryStream = new MemoryStream(_sound))
174 {
175 using (var player = new SoundPlayer(memoryStream))
176 {
177 player.Play();
178 }
179 }
180  
1 office 181 // Display the form just above the system tray.
182 Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - Width,
183 Screen.PrimaryScreen.WorkingArea.Height - Height);
184  
185 // Move each open form upwards to make room for this one
4 office 186 foreach (var openForm in OpenNotifications)
187 {
188 if (openForm.InvokeRequired)
189 {
190 Action action = delegate { openForm.Top -= Height; };
191 openForm.Invoke(action);
7 office 192  
193 continue;
4 office 194 }
1 office 195  
4 office 196 openForm.Top -= Height;
197 }
198  
1 office 199 OpenNotifications.Add(this);
200 lifeTimer.Start();
201 }
202  
203 private void Notification_Activated(object sender, EventArgs e)
204 {
205 // Prevent the form taking focus when it is initially shown
206 if (!_allowFocus)
7 office 207 {
1 office 208 // Activate the window that previously had focus
209 NativeMethods.SetForegroundWindow(_currentForegroundWindow);
7 office 210 }
1 office 211 }
212  
213 private void Notification_Shown(object sender, EventArgs e)
214 {
215 // Once the animation has completed the form can receive focus
216 _allowFocus = true;
217  
218 // Close the form by sliding down.
219 _animator.Duration = 0;
220 _animator.Direction = FormAnimator.AnimationDirection.Down;
221 }
222  
223 private void Notification_FormClosed(object sender, FormClosedEventArgs e)
224 {
225 // Move down any open forms above this one
226 foreach (var openForm in OpenNotifications)
227 {
228 if (openForm == this)
229 // Remaining forms are below this one
230 break;
231  
7 office 232 if (openForm.InvokeRequired)
233 {
234 Action action = delegate { openForm.Top += Height; };
235 openForm.Invoke(action);
236  
237 continue;
238 }
1 office 239 }
240  
241 OpenNotifications.Remove(this);
242 }
243  
244 private void LifeTimer_Tick(object sender, EventArgs e)
245 {
246 Close();
247 }
248  
249 private void Notification_Click(object sender, EventArgs e)
250 {
251 Close();
252 }
253  
254 private void LabelTitle_Click(object sender, EventArgs e)
255 {
256 Close();
257 }
258  
259 private void LabelRO_Click(object sender, EventArgs e)
260 {
261 Close();
262 }
263  
264 #endregion
265 }
266 }