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