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