Toasts – Blame information for rev 9
?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; |
||
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 | |||
7 | office | 116 | /// <summary> |
117 | /// Clean up any resources being used. |
||
118 | /// </summary> |
||
119 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
||
120 | protected override void Dispose(bool disposing) |
||
121 | { |
||
122 | if (disposing && (components != null)) |
||
123 | { |
||
124 | components.Dispose(); |
||
125 | } |
||
126 | base.Dispose(disposing); |
||
127 | } |
||
128 | |||
1 | office | 129 | #endregion |
130 | |||
131 | #region Private Delegates, Events, Enums, Properties, Indexers and Fields |
||
132 | |||
133 | private readonly FormAnimator _animator; |
||
134 | |||
135 | private bool _allowFocus; |
||
136 | |||
137 | private IntPtr _currentForegroundWindow; |
||
138 | |||
8 | office | 139 | private byte[] _sound; |
140 | |||
1 | office | 141 | #endregion |
142 | |||
143 | #region Event Handlers |
||
144 | |||
9 | office | 145 | private void Notification_Load(object sender, EventArgs e) |
1 | office | 146 | { |
8 | office | 147 | // Play the sound. |
148 | if (_sound == null) |
||
149 | { |
||
9 | office | 150 | using (var memoryStream = new MemoryStream()) |
151 | { |
||
152 | Properties.Resources.normal.CopyTo(memoryStream); |
||
153 | |||
154 | memoryStream.Position = 0L; |
||
155 | |||
156 | _sound = memoryStream.ToArray(); |
||
157 | } |
||
8 | office | 158 | } |
159 | |||
160 | using (var memoryStream = new MemoryStream(_sound)) |
||
161 | { |
||
162 | using (var player = new SoundPlayer(memoryStream)) |
||
163 | { |
||
164 | player.Play(); |
||
165 | } |
||
166 | } |
||
167 | |||
1 | office | 168 | // Display the form just above the system tray. |
169 | Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - Width, |
||
170 | Screen.PrimaryScreen.WorkingArea.Height - Height); |
||
171 | |||
172 | // Move each open form upwards to make room for this one |
||
4 | office | 173 | foreach (var openForm in OpenNotifications) |
174 | { |
||
175 | if (openForm.InvokeRequired) |
||
176 | { |
||
177 | Action action = delegate { openForm.Top -= Height; }; |
||
178 | openForm.Invoke(action); |
||
7 | office | 179 | |
180 | continue; |
||
4 | office | 181 | } |
1 | office | 182 | |
4 | office | 183 | openForm.Top -= Height; |
184 | } |
||
185 | |||
1 | office | 186 | OpenNotifications.Add(this); |
187 | lifeTimer.Start(); |
||
188 | } |
||
189 | |||
190 | private void Notification_Activated(object sender, EventArgs e) |
||
191 | { |
||
192 | // Prevent the form taking focus when it is initially shown |
||
193 | if (!_allowFocus) |
||
7 | office | 194 | { |
1 | office | 195 | // Activate the window that previously had focus |
196 | NativeMethods.SetForegroundWindow(_currentForegroundWindow); |
||
7 | office | 197 | } |
1 | office | 198 | } |
199 | |||
200 | private void Notification_Shown(object sender, EventArgs e) |
||
201 | { |
||
202 | // Once the animation has completed the form can receive focus |
||
203 | _allowFocus = true; |
||
204 | |||
205 | // Close the form by sliding down. |
||
206 | _animator.Duration = 0; |
||
207 | _animator.Direction = FormAnimator.AnimationDirection.Down; |
||
208 | } |
||
209 | |||
210 | private void Notification_FormClosed(object sender, FormClosedEventArgs e) |
||
211 | { |
||
212 | // Move down any open forms above this one |
||
213 | foreach (var openForm in OpenNotifications) |
||
214 | { |
||
215 | if (openForm == this) |
||
216 | // Remaining forms are below this one |
||
217 | break; |
||
218 | |||
7 | office | 219 | if (openForm.InvokeRequired) |
220 | { |
||
221 | Action action = delegate { openForm.Top += Height; }; |
||
222 | openForm.Invoke(action); |
||
223 | |||
224 | continue; |
||
225 | } |
||
1 | office | 226 | } |
227 | |||
228 | OpenNotifications.Remove(this); |
||
229 | } |
||
230 | |||
231 | private void LifeTimer_Tick(object sender, EventArgs e) |
||
232 | { |
||
233 | Close(); |
||
234 | } |
||
235 | |||
236 | private void Notification_Click(object sender, EventArgs e) |
||
237 | { |
||
238 | Close(); |
||
239 | } |
||
240 | |||
241 | private void LabelTitle_Click(object sender, EventArgs e) |
||
242 | { |
||
243 | Close(); |
||
244 | } |
||
245 | |||
246 | private void LabelRO_Click(object sender, EventArgs e) |
||
247 | { |
||
248 | Close(); |
||
249 | } |
||
250 | |||
251 | #endregion |
||
252 | } |
||
253 | } |